Skip to content

Commit

Permalink
Merge pull request #571 from arayabrain/feature/refactor-edit-roi-code
Browse files Browse the repository at this point in the history
Refactoring edit roi codes
  • Loading branch information
itutu-tienday authored Jan 27, 2025
2 parents e531faa + 3439ad7 commit 13d8ad8
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions studio/app/optinist/core/nwb/nwb_creater.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,54 @@

class NWBCreater:
@classmethod
def acquisition(cls, config):
nwbfile = NWBFile(
def acquisition(cls, config: dict):
new_nwbfile = NWBFile(
session_description=config["session_description"],
identifier=config["identifier"],
experiment_description=config["experiment_description"],
session_start_time=datetime.now(tzlocal()),
)

# 顕微鏡情報を登録
device = nwbfile.create_device(
# Create Device (microscope information)
device = new_nwbfile.create_device(
name=config["device"]["name"],
description=config["device"]["description"],
manufacturer=config["device"]["manufacturer"],
)

# 光チャネルを登録
# Create OpticalChannel
optical_channel = OpticalChannel(
name=config["optical_channel"]["name"],
description=config["optical_channel"]["description"],
emission_lambda=float(config["optical_channel"]["emission_lambda"]),
)

# imaging planeを追加
imaging_plane = nwbfile.create_imaging_plane(
# Create Imaging Plane
imaging_plane = new_nwbfile.create_imaging_plane(
name=config["imaging_plane"]["name"],
description=config["imaging_plane"]["description"],
optical_channel=optical_channel, # 光チャネル
device=device, # 電極デバイス
imaging_rate=float(config["imaging_plane"]["imaging_rate"]), # 画像の比率Hz
optical_channel=optical_channel, # Optical Channel
device=device, # Electrode Device
imaging_rate=float(
config["imaging_plane"]["imaging_rate"]
), # Image Ratio Hz
excitation_lambda=float(
config["imaging_plane"]["excitation_lambda"]
), # 励起(れいき)波長
indicator=config["imaging_plane"]["indicator"], # カルシウムインジケーター
), # Excitation Wavelength
indicator=config["imaging_plane"]["indicator"], # Calcium Indicator
location=config["imaging_plane"]["location"],
)

# using internal data. this data will be stored inside the NWB file
# Using internal data. this data will be stored inside the NWB file
if (
NWBDATASET.IMAGE_SERIES in config
and "external_file" in config[NWBDATASET.IMAGE_SERIES]
):
external_file = config[NWBDATASET.IMAGE_SERIES]["external_file"]
image_path = external_file.path
try:
image_path = external_file.path
except AttributeError:
image_path = external_file

starting_frames = (
config[NWBDATASET.IMAGE_SERIES]["starting_frame"]
Expand All @@ -84,6 +89,7 @@ def acquisition(cls, config):
elif isinstance(image_path, str):
image_path = [image_path]

# Create Acquisition
image_series = TwoPhotonSeries(
name="TwoPhotonSeries",
starting_frame=starting_frames,
Expand All @@ -94,16 +100,16 @@ def acquisition(cls, config):
unit="normalized amplitude",
data=external_file.data if save_raw_image_to_nwb else None,
)
nwbfile.add_acquisition(image_series)
new_nwbfile.add_acquisition(image_series)

nwbfile.create_processing_module(
new_nwbfile.create_processing_module(
name="ophys", description="optical physiology processed data"
)
new_nwbfile.create_processing_module(name="optinist", description="description")

nwbfile.create_processing_module(name="optinist", description="description")
cls.ophys(nwbfile)
cls.ophys(new_nwbfile)

return nwbfile
return new_nwbfile

@classmethod
def ophys(cls, nwbfile):
Expand All @@ -123,7 +129,7 @@ def ophys(cls, nwbfile):
return nwbfile

@classmethod
def add_plane_segmentation(cls, nwbfile, function_id):
def plane_segmentation(cls, nwbfile, function_id):
image_seg = nwbfile.processing["ophys"].data_interfaces["ImageSegmentation"]
if "TwoPhotonSeries" in nwbfile.acquisition:
reference_images = nwbfile.acquisition["TwoPhotonSeries"]
Expand Down Expand Up @@ -189,7 +195,7 @@ def motion_correction(cls, nwbfile, function_id, mc_data, xy_trans_data):

@classmethod
def roi(cls, nwbfile, function_id, roi_list):
nwbfile = cls.add_plane_segmentation(nwbfile, function_id)
nwbfile = cls.plane_segmentation(nwbfile, function_id)
image_seg = nwbfile.processing["ophys"].data_interfaces["ImageSegmentation"]
plane_seg = image_seg.plane_segmentations[function_id]

Expand Down Expand Up @@ -284,14 +290,15 @@ def postprocess(cls, nwbfile, function_id, data):
return nwbfile

@classmethod
def reaqcuisition(cls, nwbfile):
def re_acquisition(cls, nwbfile: NWBFile):
new_nwbfile = NWBFile(
session_description=nwbfile.session_description,
identifier=nwbfile.identifier,
experiment_description=nwbfile.experiment_description,
session_start_time=nwbfile.session_start_time,
)

# Create Device (microscope information)
devices = []
for key in nwbfile.devices.keys():
device = new_nwbfile.create_device(
Expand All @@ -301,6 +308,7 @@ def reaqcuisition(cls, nwbfile):
)
devices.append(device)

# Create OpticalChannel
old_optical_channel = nwbfile.imaging_planes["ImagingPlane"].optical_channel[0]
optical_channels = []
for old_optical_channel in nwbfile.imaging_planes[
Expand All @@ -313,26 +321,28 @@ def reaqcuisition(cls, nwbfile):
)
optical_channels.append(optical_channel)

# Create Imaging Plane
imaging_planes = []
for key in nwbfile.imaging_planes.keys():
imaging_plane = new_nwbfile.create_imaging_plane(
name=nwbfile.imaging_planes[key].name,
description=nwbfile.imaging_planes[key].description,
optical_channel=optical_channels, # 光チャネル
device=devices[0], # 電極デバイス
optical_channel=optical_channels, # Optical Channel
device=devices[0], # Electrode Device
imaging_rate=float(
nwbfile.imaging_planes["ImagingPlane"].imaging_rate
), # 画像の比率Hz
), # Image Ratio Hz
excitation_lambda=float(
nwbfile.imaging_planes["ImagingPlane"].excitation_lambda
), # 励起(れいき)波長
), # Excitation Wavelength
indicator=nwbfile.imaging_planes[
"ImagingPlane"
].indicator, # カルシウムインディケーター
].indicator, # Calcium Indicator
location=nwbfile.imaging_planes["ImagingPlane"].location,
)
imaging_planes.append(imaging_plane)

# Create Acquisition
image_series = TwoPhotonSeries(
name="TwoPhotonSeries",
starting_frame=nwbfile.acquisition["TwoPhotonSeries"].starting_frame,
Expand All @@ -342,14 +352,15 @@ def reaqcuisition(cls, nwbfile):
rate=nwbfile.acquisition["TwoPhotonSeries"].rate,
unit=nwbfile.acquisition["TwoPhotonSeries"].unit,
)

new_nwbfile.add_acquisition(image_series)

new_nwbfile.create_processing_module(
name="ophys", description="optical physiology processed data"
)
new_nwbfile.create_processing_module(name="optinist", description="description")

cls.ophys(new_nwbfile)

return new_nwbfile


Expand Down Expand Up @@ -432,7 +443,7 @@ def overwrite_nwb(config, save_path, nwb_file_name):
with NWBHDF5IO(nwb_path, "r") as io:
nwbfile = io.read()
# acquisition を元ファイルから作成する
new_nwbfile = NWBCreater.reaqcuisition(nwbfile)
new_nwbfile = NWBCreater.re_acquisition(nwbfile)
new_nwbfile = set_nwbconfig(new_nwbfile, config)

with NWBHDF5IO(tmp_nwb_path, "w") as io:
Expand Down

0 comments on commit 13d8ad8

Please sign in to comment.