Skip to content

Commit

Permalink
Add working snakemake rule for preprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraporta committed Dec 4, 2024
1 parent dd44529 commit 4947e31
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,22 @@ rule setup:
writing_path="/ceph/margrie/laura/cimaut/",
output: "setup_output.txt"
shell: "python calcium_imaging_automation/core/rules/setup.py {input.datasets_path} {input.writing_path} --folder_read_pattern '2*' --file_read_pattern 'rotation_00001.tif' --file_read_pattern '*.bin' > {output}"

import pandas as pd

paths = pd.read_csv("datasets.csv")

rule all:
input:
expand("preprocess_output_{index}.txt", index=paths["index"])

rule preprocess:
input:
lambda wildcards: paths.loc[int(wildcards.index), "read_dataset_path"],
lambda wildcards: paths.loc[int(wildcards.index), "write_dataset_path"],
output:
"preprocess_output_{index}.txt"
params:
index=lambda wildcards: wildcards.index
script:
"calcium_imaging_automation/core/rules/preprocess.py"
24 changes: 24 additions & 0 deletions calcium_imaging_automation/core/rules/preprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pathlib import Path

from derotation.analysis.metrics import stability_of_most_detected_blob
from derotation.derotate_batch import derotate
from snakemake.script import snakemake

try:
# Input arguments
read_dataset_path = Path(snakemake.input[0])
write_dataset_path = Path(snakemake.input[1])
output = snakemake.output[0]

# os.system("module load miniconda")
# os.system("source activate /nfs/nhome/live/lporta/.conda/envs/cimat")
output_path_dataset = write_dataset_path / "ses-0/funcimg/"

data = derotate(read_dataset_path, output_path_dataset)
metric_measured = stability_of_most_detected_blob(data)
with open(output, "w") as f:
f.write(f"dataset: {read_dataset_path.stem} metric: {metric_measured}")
except Exception as e:
print(e.args)
with open(output, "w") as f:
f.write(str(e.args))
15 changes: 14 additions & 1 deletion calcium_imaging_automation/core/rules/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import shutil
from pathlib import Path

import pandas as pd

from calcium_imaging_automation.core.reader import ReadAquiredData
from calcium_imaging_automation.core.writer import DatashuttleWrapper

Expand All @@ -14,6 +16,7 @@ def setup(raw_data_path, folder_read_pattern, file_read_pattern, output_path):
print("No derivatives folder found")

print(f"Reading data from {raw_data_path}")

reader = ReadAquiredData(
raw_data_path,
folder_read_pattern,
Expand All @@ -25,10 +28,20 @@ def setup(raw_data_path, folder_read_pattern, file_read_pattern, output_path):
print(f"Max of tiffs found: {number_of_tiffs}")

writer = DatashuttleWrapper(output_path)
print(f"Dataset names: {reader.dataset_names}")
writer.create_folders(reader.dataset_names, session_number=number_of_tiffs)
print("Folders created")

datasets = pd.DataFrame(
{
"read_dataset_path": reader.datasets_paths,
"write_dataset_path": [
writer.get_dataset_path(dt.stem)
for dt in reader.datasets_paths
],
}
)
datasets.to_csv("datasets.csv", index=True, index_label="index")


if __name__ == "__main__":
parser = argparse.ArgumentParser(
Expand Down

0 comments on commit 4947e31

Please sign in to comment.