From cf5f39860270bf2c824dfee68ce35ea13e5e43d4 Mon Sep 17 00:00:00 2001 From: Dictus Hugo Date: Fri, 28 Oct 2022 07:56:23 +0200 Subject: [PATCH 1/5] nrrdify markers --- atlas_densities/app/cell_densities.py | 61 +++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/atlas_densities/app/cell_densities.py b/atlas_densities/app/cell_densities.py index f64bfc8..2a5f2b6 100644 --- a/atlas_densities/app/cell_densities.py +++ b/atlas_densities/app/cell_densities.py @@ -94,6 +94,7 @@ "linprog": linprog, } + L = logging.getLogger(__name__) @@ -951,3 +952,63 @@ def inhibitory_neuron_densities( annotation.with_data(volumetric_density).save_nrrd( str(Path(output_dir, f"{cell_type}_density.nrrd")) ) +ID_TO_MARKER = { + 868: 'pvalb', + 1001: 'SST', + 77371835: 'VIP', + 479: 'gad1', + 79556706: 'CNP', + 79591671: 'MBP', + 112202838: 'GFAP', + 79591593: 'S100b', + 1175: 'ALDH1L1', + 75147760: 'TMEM119', + #68161453, 68631984? +} + + +@app.command() +@click.option( + "--input-dir", + type=str, + required=True, + help=("directory containing interpolated markers"), +) +@click.option( + "--output-dir", + type=str, + required=True, + help="directory in which to place marker files", +) +@click.option( + "--reference-nrrd", + type=EXISTING_FILE_PATH, + required=True, + help=("a .nrrd file to use as a reference to create the marker files." + "Can be any .nrrd file from the same atlas") + ) +def nrrdify_markers( + input_dir: str, + output_dir: str, + reference_nrrd: str +): + """Transfer marker files as output by Deep-Atlas to .nrrd format""" + out_path = Path(output_dir) + reference = VoxelData.load_nrrd(reference_nrrd) + moved = set() + for npyfile in Path(input_dir).glob("*.npy"): + experiment_id = int(str(npyfile.name).split('-')[0]) + if experiment_id in moved: + L.warning( + f"More than one file for experiment id {experiment_id}" + f"ignoring {str(npyfile)}") + continue + moved.add(experiment_id) + try: + marker_name = ID_TO_MARKER[experiment_id] + except KeyError: + L.warning( + f"No known marker corresponding to {experiment_id}, skipping") + continue + out_name = str(out_path / (marker_name + '.nrrd')) + reference.with_data(np.load(npyfile)).save_nrrd(out_name) From 798eceef08f49a2bd962a84620f09a9bb072084e Mon Sep 17 00:00:00 2001 From: Dictus Hugo Date: Fri, 28 Oct 2022 14:35:17 +0200 Subject: [PATCH 2/5] Add check of input volume shapes to avoid downstream errors --- atlas_densities/app/combination.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/atlas_densities/app/combination.py b/atlas_densities/app/combination.py index a04f907..949620a 100644 --- a/atlas_densities/app/combination.py +++ b/atlas_densities/app/combination.py @@ -203,6 +203,12 @@ def combine_markers(annotation_path, hierarchy_path, config): ], columns=["gene", "volume"], ) + for gene, volume in volumes[['gene', 'volume']]: + if len(volume.shape) > 3: + raise ValueError( + f"volume of gene {gene} has shape {volume.shape} " + "has too many values per voxel Did you remeber to sum" + " the color channels in the interpolated genes?") combination_data = combination_data.merge(volumes, how="inner", on="gene") glia_intensities = markers_combinator.combine( From b2b2a58474056cd7b46fcb818cde859d3bf8cd8a Mon Sep 17 00:00:00 2001 From: hugs Date: Wed, 1 Feb 2023 11:57:54 +0100 Subject: [PATCH 3/5] revert change to cell_densities (keep change atomic) --- atlas_densities/app/cell_densities.py | 63 --------------------------- 1 file changed, 63 deletions(-) diff --git a/atlas_densities/app/cell_densities.py b/atlas_densities/app/cell_densities.py index d0ac2da..4781296 100644 --- a/atlas_densities/app/cell_densities.py +++ b/atlas_densities/app/cell_densities.py @@ -99,7 +99,6 @@ "linprog": linprog, } - L = logging.getLogger(__name__) @@ -959,7 +958,6 @@ def inhibitory_neuron_densities( ) - @app.command() @click.option( "--annotation-path", @@ -1052,64 +1050,3 @@ def excitatory_split( inhibitory_density, remove_ids, ) - - -ID_TO_MARKER = { - 868: 'pvalb', - 1001: 'SST', - 77371835: 'VIP', - 479: 'gad1', - 79556706: 'CNP', - 79591671: 'MBP', - 112202838: 'GFAP', - 79591593: 'S100b', - 1175: 'ALDH1L1', - 75147760: 'TMEM119', - #68161453, 68631984? -} - -@app.command() -@click.option( - "--input-dir", - type=str, - required=True, - help=("directory containing interpolated markers"), -) -@click.option( - "--output-dir", - type=str, - required=True, - help="directory in which to place marker files", -) -@click.option( - "--reference-nrrd", - type=EXISTING_FILE_PATH, - required=True, - help=("a .nrrd file to use as a reference to create the marker files." - "Can be any .nrrd file from the same atlas") - ) -def nrrdify_markers( - input_dir: str, - output_dir: str, - reference_nrrd: str -): - """Transfer marker files as output by Deep-Atlas to .nrrd format""" - out_path = Path(output_dir) - reference = VoxelData.load_nrrd(reference_nrrd) - moved = set() - for npyfile in Path(input_dir).glob("*.npy"): - experiment_id = int(str(npyfile.name).split('-')[0]) - if experiment_id in moved: - L.warning( - f"More than one file for experiment id {experiment_id}" - f"ignoring {str(npyfile)}") - continue - moved.add(experiment_id) - try: - marker_name = ID_TO_MARKER[experiment_id] - except KeyError: - L.warning( - f"No known marker corresponding to {experiment_id}, skipping") - continue - out_name = str(out_path / (marker_name + '.nrrd')) - reference.with_data(np.load(npyfile)).save_nrrd(out_name) From 4bd5a4fccca0cc634fdfe98193c5cf0f0b28bbc2 Mon Sep 17 00:00:00 2001 From: hugs Date: Wed, 1 Feb 2023 12:07:32 +0100 Subject: [PATCH 4/5] black and isort --- atlas_densities/app/combination.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/atlas_densities/app/combination.py b/atlas_densities/app/combination.py index 949620a..5daa6b3 100644 --- a/atlas_densities/app/combination.py +++ b/atlas_densities/app/combination.py @@ -203,12 +203,13 @@ def combine_markers(annotation_path, hierarchy_path, config): ], columns=["gene", "volume"], ) - for gene, volume in volumes[['gene', 'volume']]: + for gene, volume in volumes[["gene", "volume"]]: if len(volume.shape) > 3: raise ValueError( f"volume of gene {gene} has shape {volume.shape} " "has too many values per voxel Did you remeber to sum" - " the color channels in the interpolated genes?") + " the color channels in the interpolated genes?" + ) combination_data = combination_data.merge(volumes, how="inner", on="gene") glia_intensities = markers_combinator.combine( From ef9d6fe6963be0fb8acc2faceb1da4be37a16c2f Mon Sep 17 00:00:00 2001 From: hugs Date: Wed, 1 Feb 2023 12:11:18 +0100 Subject: [PATCH 5/5] fix mistake in cli --- atlas_densities/app/combination.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atlas_densities/app/combination.py b/atlas_densities/app/combination.py index 5daa6b3..9d3ed58 100644 --- a/atlas_densities/app/combination.py +++ b/atlas_densities/app/combination.py @@ -203,7 +203,7 @@ def combine_markers(annotation_path, hierarchy_path, config): ], columns=["gene", "volume"], ) - for gene, volume in volumes[["gene", "volume"]]: + for gene, volume in volumes[["gene", "volume"]].values: if len(volume.shape) > 3: raise ValueError( f"volume of gene {gene} has shape {volume.shape} "