Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,407 changes: 1,379 additions & 28 deletions cross-registration.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/ext/nbsplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def is_hv_init(cell):
def split_and_parse(app):
for src_nb, out_dir in app.config.nbsplit_dict.items():
if not os.path.exists(src_nb):
warnings.warn("notebook not found: {}".format(src_nb), RuntimeWarning)
warnings.warn(f"notebook not found: {src_nb}", RuntimeWarning)
continue
srcpath = os.path.split(src_nb)[0]
outpath = os.path.join(os.path.abspath(app.srcdir), out_dir)
Expand All @@ -33,7 +33,7 @@ def split_and_parse(app):
if os.path.exists(img_path):
shutil.copytree(img_path, os.path.join(outpath, "img"))
# read source notebook
with open(src_nb, mode="r") as notebook:
with open(src_nb) as notebook:
jnote = json.load(notebook)
cells = jnote["cells"]
# split and process cells
Expand All @@ -46,7 +46,7 @@ def split_and_parse(app):
hv_init["execution_count"] = None
# write notebooks
for isplit, start in enumerate(split):
fname = "notebook_{}.ipynb".format(isplit)
fname = f"notebook_{isplit}.ipynb"
meta = jnote["metadata"].copy()
meta["name"] = fname
try:
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _as_gufunc(pyfunc):
# -- Project information -----------------------------------------------------

project = "MiniAn"
copyright = "2018-%s, MiniAn Developers" % datetime.datetime.now().year
copyright = f"2018-{datetime.datetime.now().year}, MiniAn Developers"


# -- General configuration ---------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion minian/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import dask as da
import os
from importlib.metadata import version
from .utilities import custom_arr_optimize, custom_delay_optimize

__version__ = "1.2.1"
try:
__version__ = version("minian")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't sure why version was defined in 4 different places, so reduced it to only two (not sure what that committizen tool does)

except:
__version__ = "0.0.0"

da.config.set(
array_optimize=custom_arr_optimize, delayed_optimize=custom_delay_optimize
Expand All @@ -28,3 +32,4 @@
# os.environ["LD_PRELOAD"] = "~/.conda/envs/minian-dev/lib/libjemalloc.so"
# alternatively one can limit the malloc pool, which is the default for minian
os.environ["MALLOC_MMAP_THRESHOLD_"] = "16384"

44 changes: 22 additions & 22 deletions minian/cnmf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import functools as fct
import os
import warnings
from typing import List, Optional, Tuple, Union
from typing import Optional, Union

import cv2
import cvxpy as cvx
Expand Down Expand Up @@ -235,7 +235,7 @@ def update_spatial(
normalize=True,
size_thres=(9, None),
in_memory=False,
) -> Tuple[xr.DataArray, xr.DataArray, xr.DataArray, xr.DataArray]:
) -> tuple[xr.DataArray, xr.DataArray, xr.DataArray, xr.DataArray]:
"""
Update spatial components given the input data and temporal dynamic for each
cell.
Expand Down Expand Up @@ -387,7 +387,7 @@ def update_spatial(
f=f_in,
)
else:
cur_blk = darr.array(sparse.zeros((cur_sub.shape)))
cur_blk = darr.array(sparse.zeros(cur_sub.shape))
A_new[hblk, wblk, 0] = cur_blk
A_new = darr.block(A_new.tolist())
else:
Expand Down Expand Up @@ -437,7 +437,7 @@ def update_spatial(
)
else:
mask = (A_new.sum(["height", "width"]) > 0).compute()
print("{} out of {} units dropped".format(len(mask) - mask.sum().values, len(mask)))
print(f"{len(mask) - mask.sum().values} out of {len(mask)} units dropped")
A_new = A_new.sel(unit_id=mask)
if normalize:
norm_fac = A_new.max(["height", "width"]).compute()
Expand Down Expand Up @@ -673,7 +673,7 @@ def update_temporal(
post_scal=False,
scs_fallback=False,
concurrent_update=False,
) -> Tuple[
) -> tuple[
xr.DataArray, xr.DataArray, xr.DataArray, xr.DataArray, xr.DataArray, xr.DataArray
]:
"""
Expand Down Expand Up @@ -889,10 +889,10 @@ def update_temporal(
# issue a warning if expected memory demand is larger than 1G
if mem_demand > 1e3:
warnings.warn(
"{} cells will be updated togeter, "
"which takes roughly {} MB of memory. "
f"{cur_YrA.shape[0]} cells will be updated togeter, "
f"which takes roughly {mem_demand} MB of memory. "
"Consider merging the units "
"or changing jac_thres".format(cur_YrA.shape[0], mem_demand)
"or changing jac_thres"
)
if not warm_start:
cur_C = None
Expand Down Expand Up @@ -992,7 +992,7 @@ def update_temporal(
int_ds["g"],
)
mask = (S_new.sum("frame") > 0).compute()
print("{} out of {} units dropped".format((~mask).sum().values, len(Ymask)))
print(f"{(~mask).sum().values} out of {len(Ymask)} units dropped")
C_new, S_new, b0_new, c0_new, g = (
C_new[mask],
S_new[mask],
Expand Down Expand Up @@ -1098,7 +1098,7 @@ def update_temporal_block(
med_wd=None,
concurrent=False,
**kwargs
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
"""
Update temporal components given residule traces of a group of cells.

Expand Down Expand Up @@ -1203,7 +1203,7 @@ def update_temporal_block(

def update_temporal_cvxpy(
y: np.ndarray, g: np.ndarray, sn: np.ndarray, A=None, bseg=None, **kwargs
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
"""
Solve the temporal update optimization problem using `cvxpy`

Expand Down Expand Up @@ -1378,7 +1378,7 @@ def update_temporal_cvxpy(
raise ValueError
except (cvx.SolverError, ValueError):
warnings.warn(
"problem status is {}, returning zero".format(prob.status),
f"problem status is {prob.status}, returning zero",
RuntimeWarning,
)
return [np.zeros(c.shape, dtype=float)] * 4
Expand All @@ -1395,10 +1395,10 @@ def update_temporal_cvxpy(
def unit_merge(
A: xr.DataArray,
C: xr.DataArray,
add_list: Optional[List[xr.DataArray]] = None,
add_list: Optional[list[xr.DataArray]] = None,
thres_corr=0.9,
noise_freq: Optional[float] = None,
) -> Tuple[xr.DataArray, xr.DataArray, Optional[List[xr.DataArray]]]:
) -> tuple[xr.DataArray, xr.DataArray, Optional[list[xr.DataArray]]]:
"""
Merge cells given spatial footprints and temporal components

Expand All @@ -1416,8 +1416,8 @@ def unit_merge(
Spatial footprints of the cells.
C : xr.DataArray
Temporal component of cells.
add_list : List[xr.DataArray], optional
List of additional variables to be merged. By default `None`.
add_list : list[xr.DataArray], optional
list of additional variables to be merged. By default `None`.
thres_corr : float, optional
The threshold of correlation. Any pair of spatially overlapping cells
with correlation higher than this threshold will be transitively grouped
Expand All @@ -1433,8 +1433,8 @@ def unit_merge(
Merged spatial footprints of cells.
C_merge : xr.DataArray
Merged temporal components of cells.
add_list : List[xr.DataArray], optional
List of additional merged variables. Only returned if input `add_list`
add_list : list[xr.DataArray], optional
list of additional merged variables. Only returned if input `add_list`
is not `None`.
"""
print("computing spatial overlap")
Expand Down Expand Up @@ -1799,7 +1799,7 @@ def construct_comput(edf, pxs):
npxs.append(len(pixels))
pixels = set()
eg_ls = []
print("pixel recompute ratio: {}".format(sum(npxs) / G.number_of_nodes()))
print(f"pixel recompute ratio: {sum(npxs) / G.number_of_nodes()}")
print("computing correlations")
corr_ls = da.compute(corr_ls)[0]
corr = pd.Series(np.concatenate(corr_ls), index=np.concatenate(idx_ls), name="corr")
Expand Down Expand Up @@ -1847,7 +1847,7 @@ def adj_corr(
)


def adj_list(G: nx.Graph) -> List[np.ndarray]:
def adj_list(G: nx.Graph) -> list[np.ndarray]:
"""
Generate adjacency list representation from graph.

Expand All @@ -1858,7 +1858,7 @@ def adj_list(G: nx.Graph) -> List[np.ndarray]:

Returns
-------
adj_ls : List[np.ndarray]
adj_ls : list[np.ndarray]
The adjacency list representation of graph.
"""
gdict = nx.to_dict_of_dicts(G)
Expand Down Expand Up @@ -1936,7 +1936,7 @@ def idx_corr(X: np.ndarray, ridx: np.ndarray, cidx: np.ndarray) -> np.ndarray:

def update_background(
Y: xr.DataArray, A: xr.DataArray, C: xr.DataArray, b: xr.DataArray = None
) -> Tuple[xr.DataArray, xr.DataArray]:
) -> tuple[xr.DataArray, xr.DataArray]:
"""
Update background terms given spatial and temporal components of cells.

Expand Down
2 changes: 1 addition & 1 deletion minian/cross_registration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import itertools as itt
from typing import Iterable
from collections.abc import Iterable

import dask as da
import networkx as nx
Expand Down
8 changes: 4 additions & 4 deletions minian/initialization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import functools as fct
import itertools as itt
import os
from typing import Optional, Tuple, Union
from typing import Optional, Union

import cv2
import dask as da
Expand Down Expand Up @@ -192,7 +192,7 @@ def gmm_refine(
n_components=2,
valid_components=1,
mean_mask=True,
) -> Tuple[pd.DataFrame, xr.DataArray, GaussianMixture]:
) -> tuple[pd.DataFrame, xr.DataArray, GaussianMixture]:
"""
Filter seeds by fitting a GMM to peak-to-peak values.

Expand Down Expand Up @@ -282,7 +282,7 @@ def pnr_refine(
thres: Union[float, str] = 1.5,
q=(0.1, 99.9),
med_wnd: Optional[int] = None,
) -> Tuple[pd.DataFrame, xr.DataArray, Optional[GaussianMixture]]:
) -> tuple[pd.DataFrame, xr.DataArray, Optional[GaussianMixture]]:
"""
Filter seeds by thresholding peak-to-noise ratio.

Expand Down Expand Up @@ -389,7 +389,7 @@ def ptp_q(a: np.ndarray, q: tuple) -> float:
a : np.ndarray
Input array.
q : tuple
Tuple specifying low and high percentile values.
tuple specifying low and high percentile values.

Returns
-------
Expand Down
8 changes: 6 additions & 2 deletions minian/install.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import os
import requests
from importlib.metadata import version

NOTEBOOK_FILES = [
"pipeline.ipynb",
Expand All @@ -13,7 +14,10 @@
DEMO_FILES = [f"demo_movies/msCam{i}.avi" for i in range(1, 11)] + [
f"demo_data/session{i}/minian.nc" for i in range(1, 3)
]
VERSION = "1.2.1"
try:
VERSION = version("minian")
except:
VERSION = "0.0.0"


def _get_file(filename: str, version: str):
Expand Down Expand Up @@ -57,7 +61,7 @@ def main():
"-v",
action="store",
default=VERSION,
help="Git repo branch or tag name, default {}".format(VERSION),
help=f"Git repo branch or tag name, default {VERSION}",
)
args = parser.parse_args()

Expand Down
Loading