|
| 1 | +import os |
| 2 | +import zipfile |
1 | 3 | from pathlib import Path |
2 | 4 | from typing import List |
3 | | -from skimage import io |
| 5 | + |
4 | 6 | import numpy as np |
5 | 7 | import pandas as pd |
6 | 8 | import pooch |
7 | | -import os |
8 | | -import zipfile |
9 | | -from pathlib import Path |
| 9 | +from skimage import io |
| 10 | + |
10 | 11 | from napari_clusters_plotter import __version__ |
11 | 12 |
|
12 | 13 | # parse version |
13 | | -if 'dev' in __version__: |
| 14 | +if "dev" in __version__: |
14 | 15 | from packaging.version import parse |
| 16 | + |
15 | 17 | major, minor, patch = parse(__version__).release |
16 | 18 | version = f"{major}.{minor}.{patch-1}" |
17 | 19 | else: |
|
20 | 22 | DATA_REGISTRY = pooch.create( |
21 | 23 | path=pooch.os_cache("napari-clusters-plotter"), |
22 | 24 | base_url=f"https://github.com/biapol/napari-clusters-plotter/releases/download/v{version}/", |
23 | | - registry={"sample_data.zip": "sha256:d21889252cc439b32dacbfb2d4085057da1fe28e3c35f94fee1487804cfe9615"}, |
| 25 | + registry={ |
| 26 | + "sample_data.zip": "sha256:d21889252cc439b32dacbfb2d4085057da1fe28e3c35f94fee1487804cfe9615" |
| 27 | + }, |
24 | 28 | ) |
25 | 29 |
|
| 30 | + |
26 | 31 | def load_image(fname): |
27 | 32 | zip_path = DATA_REGISTRY.fetch("sample_data.zip") |
28 | 33 |
|
29 | 34 | # check if has been unzipped before |
30 | 35 | if not os.path.exists(zip_path.split(".zip")[0]): |
31 | | - with zipfile.ZipFile(zip_path, 'r') as z: |
| 36 | + with zipfile.ZipFile(zip_path, "r") as z: |
32 | 37 | z.extractall(zip_path.split(".zip")[0]) |
33 | 38 |
|
34 | 39 | fname = os.path.join(zip_path.split(".zip")[0], fname) |
35 | 40 | image = io.imread(fname) |
36 | 41 |
|
37 | 42 | return image |
38 | 43 |
|
| 44 | + |
39 | 45 | def load_tabular(fname, **kwargs): |
40 | 46 | zip_path = DATA_REGISTRY.fetch("sample_data.zip") |
41 | 47 |
|
42 | 48 | # check if has been unzipped before |
43 | 49 | if not os.path.exists(zip_path.split(".zip")[0]): |
44 | | - with zipfile.ZipFile(zip_path, 'r') as z: |
| 50 | + with zipfile.ZipFile(zip_path, "r") as z: |
45 | 51 | z.extractall(zip_path.split(".zip")[0]) |
46 | 52 |
|
47 | 53 | fname = os.path.join(zip_path.split(".zip")[0], fname) |
48 | 54 | data = pd.read_csv(fname, **kwargs) |
49 | 55 | return data |
50 | 56 |
|
| 57 | + |
51 | 58 | def load_registry(): |
52 | 59 | zip_path = DATA_REGISTRY.fetch("sample_data.zip") |
53 | 60 |
|
54 | 61 | # check if has been unzipped before |
55 | 62 | if not os.path.exists(zip_path.split(".zip")[0]): |
56 | | - with zipfile.ZipFile(zip_path, 'r') as z: |
| 63 | + with zipfile.ZipFile(zip_path, "r") as z: |
57 | 64 | z.extractall(zip_path.split(".zip")[0]) |
58 | 65 |
|
59 | | - fname = os.path.join(zip_path.split(".zip")[0], "sample_data/data_registry.txt") |
60 | | - registry = pd.read_csv(fname, sep=': sha256:', header=None) |
61 | | - registry.columns = ['file', 'hash'] |
| 66 | + fname = os.path.join( |
| 67 | + zip_path.split(".zip")[0], "sample_data/data_registry.txt" |
| 68 | + ) |
| 69 | + registry = pd.read_csv(fname, sep=": sha256:", header=None) |
| 70 | + registry.columns = ["file", "hash"] |
62 | 71 | return registry |
63 | 72 |
|
| 73 | + |
64 | 74 | def skan_skeleton() -> List["LayerData"]: # noqa: F821 |
65 | 75 |
|
66 | 76 | df_paths = load_tabular("shapes_skeleton/all_paths.csv") |
67 | | - df_features = load_tabular("shapes_skeleton/skeleton_features.csv", index_col="Unnamed: 0") |
| 77 | + df_features = load_tabular( |
| 78 | + "shapes_skeleton/skeleton_features.csv", index_col="Unnamed: 0" |
| 79 | + ) |
68 | 80 |
|
69 | 81 | # skeleton_id column should be categorical |
70 | 82 | categorical_columns = [ |
@@ -109,12 +121,13 @@ def skan_skeleton() -> List["LayerData"]: # noqa: F821 |
109 | 121 |
|
110 | 122 |
|
111 | 123 | def tgmm_mini_dataset() -> List["LayerData"]: # noqa: F821 |
112 | | - |
| 124 | + |
113 | 125 | features = load_tabular( |
114 | 126 | "tracking_data/tgmm-mini-spot.csv", |
115 | 127 | skiprows=[1, 2], |
116 | 128 | low_memory=False, |
117 | | - encoding="utf-8") |
| 129 | + encoding="utf-8", |
| 130 | + ) |
118 | 131 | data = load_tabular("tracking_data/tgmm-mini-tracks-layer-data.csv") |
119 | 132 |
|
120 | 133 | categorical_columns = [ |
@@ -154,8 +167,12 @@ def bbbc_1_dataset() -> List["LayerData"]: # noqa: F821 |
154 | 167 | # read data registry file |
155 | 168 | registry = load_registry() |
156 | 169 |
|
157 | | - registry_bbby1 = registry[registry['file'].str.contains("BBBC007_v1_images")] |
158 | | - tif_files = registry_bbby1[registry_bbby1['file'].str.endswith(".tif")]['file'].to_list() |
| 170 | + registry_bbby1 = registry[ |
| 171 | + registry["file"].str.contains("BBBC007_v1_images") |
| 172 | + ] |
| 173 | + tif_files = registry_bbby1[registry_bbby1["file"].str.endswith(".tif")][ |
| 174 | + "file" |
| 175 | + ].to_list() |
159 | 176 | raw_images = [f for f in tif_files if "labels" not in f] |
160 | 177 |
|
161 | 178 | n_rows = np.ceil(np.sqrt(len(raw_images))) |
@@ -211,8 +228,14 @@ def bbbc_1_dataset() -> List["LayerData"]: # noqa: F821 |
211 | 228 |
|
212 | 229 |
|
213 | 230 | def cells3d_curvatures() -> List["LayerData"]: # noqa: F821 |
214 | | - vertices = load_tabular("cells3d/vertices.txt", sep=' ', header=None).to_numpy() |
215 | | - faces = load_tabular("cells3d/faces.txt", sep=' ', header=None).to_numpy().astype(int) |
| 231 | + vertices = load_tabular( |
| 232 | + "cells3d/vertices.txt", sep=" ", header=None |
| 233 | + ).to_numpy() |
| 234 | + faces = ( |
| 235 | + load_tabular("cells3d/faces.txt", sep=" ", header=None) |
| 236 | + .to_numpy() |
| 237 | + .astype(int) |
| 238 | + ) |
216 | 239 | hks = load_tabular("cells3d/signature.csv") |
217 | 240 | nuclei = load_image("cells3d/nucleus.tif") |
218 | 241 |
|
@@ -242,7 +265,9 @@ def granule_compression_vectors() -> List["LayerData"]: # noqa: F821 |
242 | 265 | import numpy as np |
243 | 266 | from napari.utils import notifications |
244 | 267 |
|
245 | | - features = load_tabular("compression_vectors/granular_compression_test.csv") |
| 268 | + features = load_tabular( |
| 269 | + "compression_vectors/granular_compression_test.csv" |
| 270 | + ) |
246 | 271 | features["iterations"] = features["iterations"].astype("category") |
247 | 272 | features["returnStatus"] = features["returnStatus"].astype("category") |
248 | 273 | features["Label"] = features["Label"].astype("category") |
|
0 commit comments