Skip to content

Commit

Permalink
Merge pull request #474 from oist/unique-output-dir
Browse files Browse the repository at this point in the history
[#429] outputディレクトリのユニーク化
  • Loading branch information
emuemuJP authored Apr 24, 2023
2 parents 6bfe14a + 0cc5a13 commit bbb3322
Show file tree
Hide file tree
Showing 21 changed files with 112 additions and 93 deletions.
8 changes: 4 additions & 4 deletions optinist/api/dataclass/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class ImageData(BaseData):
def __init__(self, data, file_name='image'):
def __init__(self, data, output_dir=DIRPATH.OUTPUT_DIR, file_name='image'):
super().__init__(file_name)

self.json_path = None
Expand All @@ -23,7 +23,7 @@ def __init__(self, data, file_name='image'):
elif isinstance(data, list) and isinstance(data[0], str):
self.path = data
else:
_dir = join_filepath([DIRPATH.OUTPUT_DIR, "tiff", file_name])
_dir = join_filepath([output_dir, "tiff", file_name])
create_directory(_dir)

_path = join_filepath([_dir, f'{file_name}.tif'])
Expand All @@ -49,12 +49,12 @@ def save_json(self, json_dir):


class RoiData(BaseData):
def __init__(self, data, file_name='roi'):
def __init__(self, data, output_dir=DIRPATH.OUTPUT_DIR, file_name='roi'):
super().__init__(file_name)

images = create_images_list(data)

_dir = join_filepath([DIRPATH.OUTPUT_DIR, "tiff", file_name])
_dir = join_filepath([output_dir, "tiff", file_name])
create_directory(_dir)
self.path = join_filepath([_dir, f'{file_name}.tif'])
tifffile.imsave(self.path, images)
Expand Down
5 changes: 3 additions & 2 deletions optinist/api/rules/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def run(cls, __rule: Rule, last_output):
output_info = cls.execute_function(
__rule.path,
__rule.params,
os.path.dirname(__rule.output),
input_info
)

Expand Down Expand Up @@ -90,13 +91,13 @@ def save_all_nwb(cls, save_path, all_nwbfile):
)

@classmethod
def execute_function(cls, path, params, input_info):
def execute_function(cls, path, params, output_dir, input_info):
wrapper = cls.dict2leaf(
wrapper_dict,
path.split('/')
)
func = copy.deepcopy(wrapper["function"])
output_info = func(params=params, **input_info)
output_info = func(params=params, output_dir=output_dir, **input_info)
del func
gc.collect()

Expand Down
2 changes: 1 addition & 1 deletion optinist/routers/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
ACCEPT_CSV_EXT = [".csv"]
ACCEPT_HDF5_EXT = [".hdf5", ".nwb", ".HDF5", ".NWB"]

NOT_DISPLAY_ARGS_LIST = ['params', 'nwbfile']
NOT_DISPLAY_ARGS_LIST = ['params', 'output_dir', 'nwbfile']
15 changes: 8 additions & 7 deletions optinist/wrappers/caiman_wrapper/cnmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ def get_roi(A, thr, thr_method, swap_dim, dims):


def caiman_cnmf(
images: ImageData,
params: dict=None
) -> dict(fluorescence=FluoData, iscell=IscellData):
images: ImageData,
output_dir: str,
params: dict = None
) -> dict(fluorescence=FluoData, iscell=IscellData):
from caiman import local_correlations, stop_server
from caiman.paths import memmap_frames_filename
from caiman.mmapping import prepare_shape
Expand Down Expand Up @@ -209,12 +210,12 @@ def caiman_cnmf(
])

info = {
'images': ImageData(np.array(Cn * 255, dtype=np.uint8), file_name='images'),
'images': ImageData(np.array(Cn * 255, dtype=np.uint8), output_dir=output_dir, file_name='images'),
'fluorescence': FluoData(fluorescence, file_name='fluorescence'),
'iscell': IscellData(iscell, file_name='iscell'),
'all_roi': RoiData(all_roi, file_name='all_roi'),
'cell_roi': RoiData(cell_roi, file_name='cell_roi'),
'non_cell_roi': RoiData(non_cell_roi, file_name='non_cell_roi'),
'all_roi': RoiData(all_roi, output_dir=output_dir, file_name='all_roi'),
'cell_roi': RoiData(cell_roi, output_dir=output_dir, file_name='cell_roi'),
'non_cell_roi': RoiData(non_cell_roi, output_dir=output_dir, file_name='non_cell_roi'),
'nwbfile': nwbfile
}

Expand Down
13 changes: 7 additions & 6 deletions optinist/wrappers/caiman_wrapper/motion_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@


def caiman_mc(
image: ImageData,
params: dict=None
) -> dict(mc_images=ImageData):
image: ImageData,
output_dir: str,
params: dict = None
) -> dict(mc_images=ImageData):
import numpy as np
from caiman import load, save_memmap, load_memmap, stop_server
from caiman.source_extraction.cnmf.params import CNMFParams
Expand Down Expand Up @@ -55,7 +56,7 @@ def caiman_mc(
xy_trans_data = (np.array(mc.x_shifts_els), np.array(mc.y_shifts_els)) \
if params['pw_rigid'] else np.array(mc.shifts_rig)

mc_images = ImageData(images, file_name='mc_images')
mc_images = ImageData(images, output_dir=output_dir, file_name='mc_images')

nwbfile = {}
nwbfile[NWBDATASET.MOTION_CORRECTION] = {
Expand All @@ -67,8 +68,8 @@ def caiman_mc(

info = {
'mc_images': mc_images,
'meanImg': ImageData(meanImg, file_name='meanImg'),
'rois': RoiData(rois, file_name='rois'),
'meanImg': ImageData(meanImg, output_dir=output_dir, file_name='meanImg'),
'rois': RoiData(rois, output_dir=output_dir, file_name='rois'),
'nwbfile': nwbfile,
}

Expand Down
6 changes: 4 additions & 2 deletions optinist/wrappers/lccd_wrapper/lccd_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@


def lccd_detect(
mc_images: ImageData, params: dict = None
mc_images: ImageData,
output_dir: str,
params: dict = None
) -> dict(fluorescence=FluoData, cell_roi=RoiData):
from .lccd_python.lccd import LCCD

Expand Down Expand Up @@ -40,7 +42,7 @@ def lccd_detect(
timeseries_dff[i, k] = (timeseries[i, k] - f0) / f0

info = {
'rois': RoiData(np.nanmax(roi_list, axis=0), file_name='cell_roi'),
'rois': RoiData(np.nanmax(roi_list, axis=0), output_dir=output_dir, file_name='cell_roi'),
'fluorescence': FluoData(timeseries, file_name='fluorescence'),
'dff': FluoData(timeseries_dff, file_name='dff'),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@


def cell_grouping(
neural_data: TimeSeriesData,
nwbfile: NWBFile=None,
params: dict=None
) -> dict():
neural_data: TimeSeriesData,
nwbfile: NWBFile=None,
output_dir: str,
params: dict = None
) -> dict():
import numpy as np

neural_data = neural_data.data
Expand Down
11 changes: 6 additions & 5 deletions optinist/wrappers/optinist_wrapper/basic_neural_analysis/eta.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ def calc_trigger_average(neural_data, trigger_idx, start_time, end_time):


def ETA(
neural_data: FluoData,
behaviors_data: BehaviorData,
iscell: IscellData=None,
params: dict=None
) -> dict(mean=TimeSeriesData):
neural_data: FluoData,
behaviors_data: BehaviorData,
output_dir: str,
iscell: IscellData = None,
params: dict = None
) -> dict(mean=TimeSeriesData):
import numpy as np

neural_data = neural_data.data
Expand Down
11 changes: 6 additions & 5 deletions optinist/wrappers/optinist_wrapper/dimension_reduction/cca.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from optinist.api.nwb.nwb import NWBDATASET

def CCA(
neural_data: FluoData,
behaviors_data: BehaviorData,
iscell: IscellData=None,
params: dict=None
) -> dict():
neural_data: FluoData,
behaviors_data: BehaviorData,
output_dir: str,
iscell: IscellData = None,
params: dict = None
) -> dict():

from sklearn.cross_decomposition import CCA

Expand Down
9 changes: 5 additions & 4 deletions optinist/wrappers/optinist_wrapper/dimension_reduction/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from optinist.api.nwb.nwb import NWBDATASET

def PCA(
neural_data: FluoData,
iscell: IscellData=None,
params: dict=None
) -> dict():
neural_data: FluoData,
output_dir: str,
iscell: IscellData = None,
params: dict = None
) -> dict():

# modules specific to function
from sklearn.decomposition import PCA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from optinist.api.nwb.nwb import NWBDATASET

def TSNE(
neural_data: FluoData,
iscell: IscellData=None,
params: dict=None
) -> dict():
neural_data: FluoData,
output_dir: str,
iscell: IscellData = None,
params: dict = None
) -> dict():

from sklearn.manifold import TSNE

Expand Down
11 changes: 6 additions & 5 deletions optinist/wrappers/optinist_wrapper/neural_decoding/glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
from optinist.api.nwb.nwb import NWBDATASET

def GLM(
neural_data: FluoData,
behaviors_data: BehaviorData,
iscell: IscellData=None,
params: dict=None
) -> dict():
neural_data: FluoData,
behaviors_data: BehaviorData,
output_dir: str,
iscell: IscellData = None,
params: dict = None
) -> dict():

# modules specific to function
import statsmodels.api as sm
Expand Down
11 changes: 6 additions & 5 deletions optinist/wrappers/optinist_wrapper/neural_decoding/lda.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from optinist.api.nwb.nwb import NWBDATASET

def LDA(
neural_data: FluoData,
behaviors_data: BehaviorData,
iscell: IscellData=None,
params: dict=None
) -> dict():
neural_data: FluoData,
behaviors_data: BehaviorData,
output_dir: str,
iscell: IscellData = None,
params: dict = None
) -> dict():

# modules specific to function
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
Expand Down
11 changes: 6 additions & 5 deletions optinist/wrappers/optinist_wrapper/neural_decoding/svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from optinist.api.nwb.nwb import NWBDATASET

def SVM(
neural_data: FluoData,
behaviors_data: BehaviorData,
iscell: IscellData=None,
params: dict=None
) -> dict():
neural_data: FluoData,
behaviors_data: BehaviorData,
output_dir: str,
iscell: IscellData = None,
params: dict = None
) -> dict():

# modules specific to function
import numpy as np
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@


def correlation(
neural_data: FluoData,
iscell: IscellData=None,
params: dict=None
) -> dict():
neural_data: FluoData,
output_dir: str,
iscell: IscellData = None,
params: dict = None
) -> dict():

neural_data = neural_data.data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@


def cross_correlation(
neural_data: FluoData,
iscell: IscellData=None,
params: dict=None
) -> dict():
neural_data: FluoData,
output_dir: str,
iscell: IscellData = None,
params: dict = None
) -> dict():

import scipy.signal as ss
import itertools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@


def Granger(
neural_data: FluoData,
iscell: IscellData=None,
params: dict=None
) -> dict():
neural_data: FluoData,
output_dir: str,
iscell: IscellData = None,
params: dict = None
) -> dict():

# modules specific to function
# from sklearn.preprocessing import StandardScaler
Expand Down
12 changes: 6 additions & 6 deletions optinist/wrappers/suite2p_wrapper/file_convert.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os

from optinist.api.dataclass.dataclass import *
from optinist.api.dir_path import DIRPATH
from optinist.api.utils.filepath_creater import join_filepath


def suite2p_file_convert(
image: ImageData,
params: dict=None
) -> dict(ops=Suite2pData):
image: ImageData,
output_dir: str,
params: dict = None
) -> dict(ops=Suite2pData):
from suite2p import io, default_ops
print('start suite2_file_convert')

Expand All @@ -24,7 +24,7 @@ def suite2p_file_convert(
db = {
'data_path': data_path_list,
'tiff_list': data_name_list,
'save_path0': DIRPATH.OUTPUT_DIR,
'save_path0': output_dir,
'save_folder': 'suite2p'
}

Expand All @@ -39,7 +39,7 @@ def suite2p_file_convert(
ops = io.tiff_to_binary(ops.copy())

info = {
'meanImg': ImageData(ops['meanImg'], file_name='meanImg'),
'meanImg': ImageData(ops['meanImg'], output_dir=output_dir, file_name='meanImg'),
'ops': Suite2pData(ops, file_name='ops')
}

Expand Down
11 changes: 6 additions & 5 deletions optinist/wrappers/suite2p_wrapper/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@


def suite2p_registration(
ops: Suite2pData,
params: dict=None
) -> dict(ops=Suite2pData):
ops: Suite2pData,
output_dir: str,
params: dict = None
) -> dict(ops=Suite2pData):
from suite2p import registration, default_ops
ops = ops.data
refImg = ops['meanImg']
Expand All @@ -24,8 +25,8 @@ def suite2p_registration(
ops = registration.get_pc_metrics(ops)

info = {
'refImg': ImageData(ops['refImg'], file_name='refImg'),
'meanImgE': ImageData(ops['meanImgE'], file_name='meanImgE'),
'refImg': ImageData(ops['refImg'], output_dir=output_dir, file_name='refImg'),
'meanImgE': ImageData(ops['meanImgE'], output_dir=output_dir, file_name='meanImgE'),
'ops': Suite2pData(ops, file_name='ops'),
}

Expand Down
Loading

0 comments on commit bbb3322

Please sign in to comment.