Skip to content

Instantly share code, notes, and snippets.

@fschwar4
fschwar4 / increase_large.py
Created May 1, 2025 22:01
Numpy overhead for array loading with different file systems?
import gc
import os
import subprocess
import time
import memray
import numpy as np
import numpy.lib.format
from datetime import datetime as dt
@fschwar4
fschwar4 / calcium_correction.py
Last active January 25, 2025 10:45
lab value transformations
import pandas as pd
## CALCIUM CORRECTION (based on Albumin)
# cave: check the units of Calcium and Albumin
# Calcium is given in mmol/l; Albumin in g/l
# equation following Payne 1973; https://doi.org/10.1136/bmj.4.5893.643
# Adjusted calcium = calcium - albumin + 4.0 (if albumin in g/dL and calcium in mg/dL)
# Calcium conversions: 1 mg/dL = 0.25 mmol/L; 1 mmol/L = 4 mg/dL
# Calcium corrected [mmol/l] = Calcium measured [mmol/l] – (0,025 x Albumin [g/l]) + 1
@fschwar4
fschwar4 / extrac_mcs_raw_rec.py
Last active April 10, 2024 07:49
Copy out only a small part of the original raw data from a MCS recording. Preserves all meta data.
import h5py as h5
file_path = 'data.h5'
stream_path = '/Data/Recording_0/AnalogStream/Stream_'
# set boundaries for data extraction
start_s = 0 # start second
end_s = 10 # end second
@fschwar4
fschwar4 / addLoggingLevel.py
Created January 12, 2024 08:21
Creating Custom/Unique Logger-Level for Python
def addLoggingLevel(levelName, levelNum, methodName=None):
"""Comprehensively adds a new logging level to the `logging` module and the
currently configured logging class.
`levelName` becomes an attribute of the `logging` module with the value
`levelNum`. `methodName` becomes a convenience method for both `logging`
itself and the class returned by `logging.getLoggerClass()` (usually just
`logging.Logger`). If `methodName` is not specified, `levelName.lower()` is
used.
@fschwar4
fschwar4 / git_meta.py
Created December 19, 2023 20:06
add git information for analysis metadata
from typing import Dict, Optional
from pathlib import Path
class MyClass:
@staticmethod
def get_git_infos() -> Dict[str, Optional[str]]:
"""Extract relevant git information for reproducibility.
This function should help to increase reproducibility of the results,
since it is than known which code version was used for the respective
@fschwar4
fschwar4 / sinc_interpolation.py
Last active April 15, 2024 11:22
Fast Python implementation of Whittaker–Shannon / sinc / bandlimited interpolation.
import numpy as np
from numpy.typing import NDArray
def sinc_interpolation(x: NDArray, s: NDArray, u: NDArray) -> NDArray:
"""Whittaker–Shannon or sinc or bandlimited interpolation.
Args:
x (NDArray): signal to be interpolated, can be 1D or 2D
s (NDArray): time points of x (*s* for *samples*)
u (NDArray): time points of y (*u* for *upsampled*)
@fschwar4
fschwar4 / coefficient_of_variation.py
Last active June 24, 2023 14:58
Calculate the local variation (Lv) as proposed in Shinomot et al. 2009. The Lv should be more robust than the coefficient of variation (Cv).
import numpy as np
def cv(spike_train: np.ndarray) -> float:
"""Calculate coefficient of variation (Cv) of the interspike intervals.
$$\displaystyle{Cv = \frac{\sigma_{ISI}}{\mu_{ISI}}}$$
Args:
spike_train (np.ndarray): Spike train.