Skip to content

Commit 74f018f

Browse files
committed
add custom region param and reformat with black
1 parent dab26e9 commit 74f018f

15 files changed

+350
-184
lines changed

PyNutil/io/file_operations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def save_analysis_output(
2121
atlas_name=None,
2222
atlas_path=None,
2323
label_path=None,
24-
settings_file=None
24+
settings_file=None,
2525
):
2626
"""
2727
Save the analysis output to the specified folder.

PyNutil/main.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def __init__(
3232
atlas_name=None,
3333
atlas_path=None,
3434
label_path=None,
35+
custom_region_path=None,
3536
settings_file=None,
3637
):
3738
"""
@@ -51,6 +52,8 @@ def __init__(
5152
The path to the custom atlas volume file, only specify if you don't want to use brainglobe (default is None).
5253
label_path : str, optional
5354
The path to the custom atlas label file, only specify if you don't want to use brainglobe (default is None).
55+
custom_region_path : str, optional
56+
The path to a custom region id file. This can be found
5457
settings_file : str, optional
5558
The path to the settings JSON file. This file contains the above parameters and is used for automation (default is None).
5659
@@ -69,6 +72,8 @@ def __init__(
6972
segmentation_folder = settings["segmentation_folder"]
7073
alignment_json = settings["alignment_json"]
7174
colour = settings["colour"]
75+
if "custom_region_path" in settings:
76+
custom_region_path = settings["custom_region_path"]
7277
if "atlas_path" in settings and "label_path" in settings:
7378
atlas_path = settings["atlas_path"]
7479
label_path = settings["label_path"]
@@ -83,7 +88,14 @@ def __init__(
8388
self.alignment_json = alignment_json
8489
self.colour = colour
8590
self.atlas_name = atlas_name
86-
91+
if custom_region_path:
92+
custom_regions_rgb, custom_regions_ids = open_custom_region_file(
93+
custom_region_path
94+
)
95+
else:
96+
custom_regions_rgb, custom_regions_ids = None, None
97+
self.custom_regions_rgb = custom_regions_rgb
98+
self.custom_regions_ids = custom_regions_ids
8799
if (atlas_path or label_path) and atlas_name:
88100
raise ValueError(
89101
"Please specify either atlas_path and label_path or atlas_name. Atlas and label paths are only used for loading custom atlases."
@@ -217,12 +229,11 @@ def save_analysis(self, output_folder):
217229
segmentation_folder=self.segmentation_folder,
218230
alignment_json=self.alignment_json,
219231
colour=self.colour,
220-
atlas_name=getattr(self, 'atlas_name', None),
221-
atlas_path=getattr(self, 'atlas_path', None),
222-
label_path=getattr(self, 'label_path', None),
223-
settings_file=getattr(self, 'settings_file', None)
232+
atlas_name=getattr(self, "atlas_name", None),
233+
atlas_path=getattr(self, "atlas_path", None),
234+
label_path=getattr(self, "label_path", None),
235+
settings_file=getattr(self, "settings_file", None),
224236
)
225237
print(f"Saved output to {output_folder}")
226238
except Exception as e:
227239
raise ValueError(f"Error saving analysis: {e}")
228-

PyNutil/processing/coordinate_extraction.py

+24-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ def get_centroids_and_area(segmentation, pixel_cut_off=0):
4545
return centroids, area, coords
4646

4747

48-
4948
def create_threads(
5049
segmentations,
5150
slices,
@@ -144,8 +143,8 @@ def folder_to_atlas_space(
144143
slices = load_visualign_json(quint_alignment)
145144
segmentations = get_segmentations(folder)
146145
flat_files, flat_file_nrs = get_flat_files(folder, use_flat)
147-
points_list, centroids_list, region_areas_list, centroids_labels, points_labels = initialize_lists(
148-
len(segmentations)
146+
points_list, centroids_list, region_areas_list, centroids_labels, points_labels = (
147+
initialize_lists(len(segmentations))
149148
)
150149
threads = create_threads(
151150
segmentations,
@@ -165,8 +164,8 @@ def folder_to_atlas_space(
165164
use_flat,
166165
)
167166
start_and_join_threads(threads)
168-
points, centroids, points_labels, centroids_labels, points_len, centroids_len = process_results(
169-
points_list, centroids_list, points_labels, centroids_labels
167+
points, centroids, points_labels, centroids_labels, points_len, centroids_len = (
168+
process_results(points_list, centroids_list, points_labels, centroids_labels)
170169
)
171170
return (
172171
points,
@@ -180,9 +179,6 @@ def folder_to_atlas_space(
180179
)
181180

182181

183-
184-
185-
186182
def initialize_lists(length):
187183
"""
188184
Initializes lists for storing points, centroids, and region areas.
@@ -195,8 +191,8 @@ def initialize_lists(length):
195191
"""
196192
points_list = [np.array([])] * length
197193
centroids_list = [np.array([])] * length
198-
centroids_labels = [np.array([])] * length
199-
points_labels = [np.array([])] * length
194+
centroids_labels = [np.array([])] * length
195+
points_labels = [np.array([])] * length
200196

201197
region_areas_list = [
202198
pd.DataFrame(
@@ -213,7 +209,13 @@ def initialize_lists(length):
213209
}
214210
)
215211
] * length
216-
return points_list, centroids_list, region_areas_list, centroids_labels, points_labels
212+
return (
213+
points_list,
214+
centroids_list,
215+
region_areas_list,
216+
centroids_labels,
217+
points_labels,
218+
)
217219

218220

219221
def create_threads(
@@ -427,11 +429,16 @@ def segmentation_to_atlas_space(
427429
)
428430
scaled_y, scaled_x = get_scaled_pixels(segmentation, pixel_id, y_scale, x_scale)
429431
if scaled_x is not None:
430-
per_point_labels = atlas_map[np.round(scaled_x).astype(int), np.round(scaled_y).astype(int)]
432+
per_point_labels = atlas_map[
433+
np.round(scaled_x).astype(int), np.round(scaled_y).astype(int)
434+
]
431435
else:
432436
per_point_labels = np.array([])
433437
if scaled_centroidsX is not None:
434-
per_centroid_labels = atlas_map[np.round(scaled_centroidsX).astype(int), np.round(scaled_centroidsY).astype(int)]
438+
per_centroid_labels = atlas_map[
439+
np.round(scaled_centroidsX).astype(int),
440+
np.round(scaled_centroidsY).astype(int),
441+
]
435442
else:
436443
per_centroid_labels = np.array([])
437444

@@ -458,9 +465,12 @@ def segmentation_to_atlas_space(
458465
points_list[index] = np.array(points if points is not None else [])
459466
centroids_list[index] = np.array(centroids if centroids is not None else [])
460467
region_areas_list[index] = region_areas
461-
centroids_labels[index] = np.array(per_centroid_labels if centroids is not None else [])
468+
centroids_labels[index] = np.array(
469+
per_centroid_labels if centroids is not None else []
470+
)
462471
points_labels[index] = np.array(per_point_labels if points is not None else [])
463472

473+
464474
def get_triangulation(slice_dict, reg_width, reg_height, non_linear):
465475
"""
466476
Gets the triangulation for the slice.

PyNutil/processing/counting_and_load.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def pixel_count_per_region(
7979
counts_per_label, columns=["idx", "pixel_count", "object_count"]
8080
)
8181
for clc, lcc in zip(counted_labels_centroids, label_counts_centroids):
82-
df_counts_per_label.loc[df_counts_per_label['idx'] == clc, 'object_count'] = lcc
82+
df_counts_per_label.loc[df_counts_per_label["idx"] == clc, "object_count"] = lcc
8383
new_rows = []
8484
for index, row in df_counts_per_label.iterrows():
8585
mask = df_label_colours["idx"] == row["idx"].astype(int)

PyNutil/processing/utils.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,7 @@ def process_results(points_list, centroids_list, points_labels, centroids_labels
218218
centroids for centroids in centroids_list if None not in centroids
219219
]
220220
points_labels = [pl for pl in points_labels if None not in pl]
221-
centroids_labels = [
222-
cl for cl in centroids_labels if None not in cl
223-
]
221+
centroids_labels = [cl for cl in centroids_labels if None not in cl]
224222
if len(points_list) == 0:
225223
points = np.array([])
226224
points_labels = np.array([])

demos/basic_example.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
# Configuration
88
script_dir = os.path.dirname(os.path.abspath(__file__))
9-
segmentation_folder = os.path.join(script_dir, "../tests/test_data/nonlinear_allen_mouse/")
10-
alignment_json = os.path.join(script_dir, "../tests/test_data/nonlinear_allen_mouse/alignment.json")
9+
segmentation_folder = os.path.join(
10+
script_dir, "../tests/test_data/nonlinear_allen_mouse/"
11+
)
12+
alignment_json = os.path.join(
13+
script_dir, "../tests/test_data/nonlinear_allen_mouse/alignment.json"
14+
)
1115
colour = [0, 0, 0]
1216
atlas_name = "allen_mouse_25um"
1317
output_folder = "../test_result/bg_test"

demos/basic_example_custom_atlas.py

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
label_path=os.path.join(
3434
script_dir, "../tests/test_data/allen_mouse_2017_atlas//allen2017_colours.csv"
3535
),
36-
3736
)
3837
pnt.get_coordinates(object_cutoff=0, use_flat=False)
3938
pnt.quantify_coordinates()

0 commit comments

Comments
 (0)