Skip to content

Commit

Permalink
refactor: replace bottleneck with numpy (bn->np)
Browse files Browse the repository at this point in the history
  • Loading branch information
afermg committed Dec 6, 2024
1 parent 1a1f0af commit aee326c
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/agora/io/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from functools import cached_property, lru_cache
from pathlib import Path

import bottleneck as bn
import numpy as np
import h5py
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -109,7 +109,7 @@ def get_retained(df, cutoff):
Cells must be present for at least cutoff fraction of the total number
of time points.
"""
return df.loc[bn.nansum(df.notna(), axis=1) > df.shape[1] * cutoff]
return df.loc[np.nansum(df.notna(), axis=1) > df.shape[1] * cutoff]

@property
def channels(self) -> t.Collection[str]:
Expand Down
4 changes: 2 additions & 2 deletions src/agora/utils/kymograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ def filt_cluster(


def cluster_kymograph(kymograph: pd.DataFrame, n: int = 2):
import bottleneck as bn
import numpy as np
from sklearn.cluster import KMeans

# Normalise according to mean value of signal
X = (
kymograph.divide(bn.nanmean(kymograph, axis=1), axis=0)
kymograph.divide(np.nanmean(kymograph, axis=1), axis=0)
.dropna(axis=1)
.values
)
Expand Down
3 changes: 1 addition & 2 deletions src/extraction/core/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pathlib import Path

import aliby.global_settings as global_settings
import bottleneck as bn
import h5py
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -613,7 +612,7 @@ def get_imgs_background_subtract(self, tree, tiles, bgs):
# move Z to last column to allow subtraction
lambda img, bgs: np.moveaxis(img, 0, -1)
# median of background over all pixels for each Z section
- bn.median(img[:, bgs], axis=1),
- np.median(img[:, bgs], axis=1),
img[ch],
bgs,
)
Expand Down
7 changes: 3 additions & 4 deletions src/extraction/core/functions/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import math
import typing as t

import bottleneck as bn
import numpy as np
from scipy import ndimage

Expand Down Expand Up @@ -98,7 +97,7 @@ def median(cell_mask, trap_image) -> int:
Segmentation mask for the cell.
trap_image: 2d array
"""
return bn.median(trap_image[cell_mask])
return np.median(trap_image[cell_mask])


def max2p5pc(cell_mask, trap_image) -> float:
Expand All @@ -116,7 +115,7 @@ def max2p5pc(cell_mask, trap_image) -> float:
n_top = int(np.ceil(npixels * 0.025))
# sort pixels in cell and find highest 2.5%
pixels = trap_image[cell_mask]
top_values = bn.partition(pixels, len(pixels) - n_top)[-n_top:]
top_values = np.partition(pixels, len(pixels) - n_top)[-n_top:]
# find mean of these highest pixels
return np.mean(top_values)

Expand All @@ -137,7 +136,7 @@ def max5px_median(cell_mask, trap_image) -> float:
# sort pixels in cell
pixels = trap_image[cell_mask]
if len(pixels) > 5:
top_values = bn.partition(pixels, len(pixels) - 5)[-5:]
top_values = np.partition(pixels, len(pixels) - 5)[-5:]
# find mean of five brightest pixels
max5px = np.mean(top_values)
med = np.median(pixels)
Expand Down
3 changes: 1 addition & 2 deletions src/extraction/core/functions/distributors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import typing as t

import bottleneck as bn
import numpy as np


Expand Down Expand Up @@ -40,7 +39,7 @@ def reduce_z(trap_image: np.ndarray, fun: t.Callable, axis: int = 0):
if (
hasattr(fun, "__module__") and fun.__module__[:10] == "bottleneck"
): # Bottleneck type
return getattr(bn.reduce, fun.__name__)(trap_image, axis=axis)
return getattr(np.reduce, fun.__name__)(trap_image, axis=axis)
elif isinstance(fun, np.ufunc):
# optimise the reduction function if possible
return fun.reduce(trap_image, axis=axis)
Expand Down
9 changes: 4 additions & 5 deletions src/extraction/core/functions/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np
from cp_measure.bulk import get_all_measurements
from skimage.measure import regionprops_table
import bottleneck as bn

from extraction.core.functions import cell, trap
from extraction.core.functions.custom import localisation
Expand Down Expand Up @@ -152,11 +151,11 @@ def load_redfuns(
Functions to perform the reduction.
"""
RED_FUNS = {
"max": bn.nanmax,
"mean": bn.nanmean,
"median": bn.nanmedian,
"max": np.nanmax,
"mean": np.nanmean,
"median": np.nanmedian,
"div0": div0,
"add": bn.nansum,
"add": np.nansum,
"None": None,
}
if additional_reducers is not None:
Expand Down

0 comments on commit aee326c

Please sign in to comment.