Skip to content

Commit

Permalink
WIP: saving images 🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraporta committed Nov 8, 2024
1 parent 9c49db1 commit 831ed71
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 26 deletions.
10 changes: 7 additions & 3 deletions calcium_imaging_automation/core/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ def __init__(
def get_folders_first_layer(self, file_path: Path) -> List[Path]:
return list(file_path.glob(self.folder_read_pattern))

def get_files_paths_by_format(self, folder: Path, filetype="tif") -> List[Path]:
def get_files_paths_by_format(
self, folder: Path, filetype="tif"
) -> List[Path]:
return list(folder.rglob(filetype))

def total_objects_by_format(self, folder: Path) -> dict:
return {
filetype.split(".")[-1]: len(self.get_files_paths_by_format(folder, filetype))
filetype.split(".")[-1]: len(
self.get_files_paths_by_format(folder, filetype)
)
for filetype in self.file_read_pattern
}

Expand Down
26 changes: 24 additions & 2 deletions calcium_imaging_automation/core/writer.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from pathlib import Path
from typing import List
from typing import Dict, List

import numpy as np
from datashuttle.configs.config_class import Configs
from datashuttle.utils import folders
from PIL import Image


class DatashuttleWrapper:
def __init__(self, output_path: Path) -> None:
# This is supposed to run in the cluster and have direct access
# to the central storages
self.output_path = output_path
self.datashuttle_cfg = Configs(
project_name=output_path.name,
file_path=output_path,
Expand All @@ -20,7 +23,8 @@ def __init__(self, output_path: Path) -> None:
)

def create_folders(self, dataset_names: List[str], session_number) -> None:
folders.create_folder_trees(
# all_paths is a dictionary with keys: sub, ses
self.all_paths: Dict[str, List[Path]] = folders.create_folder_trees(
cfg=self.datashuttle_cfg,
top_level_folder="derivatives",
sub_names=[
Expand All @@ -30,3 +34,21 @@ def create_folders(self, dataset_names: List[str], session_number) -> None:
ses_names=[f"ses-{i}" for i in range(session_number)],
datatype="funcimg",
)

def get_dataset_path(self, dataset_name: str) -> Path:
print((self.output_path / "derivatives"))
return next(
(self.output_path / "derivatives").glob(f"*{dataset_name}*"), None
)

def save_image(
self,
image: np.ndarray,
run_id: int,
dataset_name: str,
session_number: int,
filename: str,
) -> None:
path = self.get_dataset_path(dataset_name)
image = Image.fromarray(image)
image.save(path / f"ses-{session_number}" / f"{filename}-{run_id}")
16 changes: 9 additions & 7 deletions examples/debugging.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from example_usage import main
from pathlib import Path
from pathlib import Path

from example_usage import main

main(
raw_data_path = Path('/nfs/winstor/margrie/SimonWeiler/RawData/Invivo_imaging/3photon_rotation/shared/'),
output_path = Path('/ceph/margrie/laura/cimaut/'),
folder_read_pattern = '2*',
file_read_pattern = ['rotation_00001.tif', '*.bin']
)
raw_data_path=Path(
"/nfs/winstor/margrie/SimonWeiler/RawData/Invivo_imaging/3photon_rotation/shared/"
),
output_path=Path("/ceph/margrie/laura/cimaut/"),
folder_read_pattern="2*",
file_read_pattern=["rotation_00001.tif", "*.bin"],
)
34 changes: 21 additions & 13 deletions examples/example_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import logging
from pathlib import Path
from typing import List
import wandb

import numpy as np

import wandb
from calcium_imaging_automation.core.reader import ReadAllPathsInFolder
from calcium_imaging_automation.core.writer import DatashuttleWrapper

Expand Down Expand Up @@ -35,7 +36,6 @@ def main(
wandb.init(project="example_usage")
run_id = wandb.run.id


# --- Read folders and files ---
reader = ReadAllPathsInFolder(
raw_data_path,
Expand All @@ -48,12 +48,10 @@ def main(
number_of_tiffs = reader.max_session_number(filetype="tif")
logging.info(f"Max of tiffs found: {number_of_tiffs}")


# --- Write folders and files ---
writer = DatashuttleWrapper(output_path)
writer.create_folders(reader.dataset_names, session_number=number_of_tiffs)


for dataset in reader.datasets_paths:
dataset = dataset.stem
for session in range(1, number_of_tiffs + 1):
Expand All @@ -63,15 +61,25 @@ def main(
data = np.random.rand(100, 100)
metric_measured = np.random.rand()

wandb.log({
"dataset": dataset,
"session": session,
"metric_measured": metric_measured,
"image": wandb.Image(data),
"run_id": run_id
})

wandb.finish()
wandb.log(
{
"dataset": dataset,
"session": session,
"metric_measured": metric_measured,
"run_id": run_id,
}
)

# save image in session folder
writer.save_image(
image=data,
run_id=run_id,
dataset_name=dataset,
session_number=session,
filename="image",
)

wandb.finish()
logging.info("Pipeline finished.")


Expand Down
1 change: 0 additions & 1 deletion examples/example_usage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ python examples/example_usage.py \
--folder_read_pattern '2*' \
--file_read_pattern 'rotation_00001.tif' \
--file_read_pattern '*.bin' \

0 comments on commit 831ed71

Please sign in to comment.