From 37d72264e5d5ac817647fca8c285e9f5696c6b5c Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Thu, 29 Feb 2024 01:54:55 +0100 Subject: [PATCH] Add files via upload --- olivia_data_plotter.py | 63 +++++++++++++++++ stage_4_process.py | 149 ++++++++++++++++++++++++----------------- 2 files changed, 150 insertions(+), 62 deletions(-) create mode 100644 olivia_data_plotter.py diff --git a/olivia_data_plotter.py b/olivia_data_plotter.py new file mode 100644 index 0000000..9ad50e0 --- /dev/null +++ b/olivia_data_plotter.py @@ -0,0 +1,63 @@ +import numpy as np +import matplotlib.pyplot as plt +import os +from functions.create_logger import create_logger +from functions.load_config import load_config + +from functions.get_trials import get_trials +import h5py # type: ignore + +control_file_handle = h5py.File("ROI_control_49.mat", "r") +control_roi = (np.array(control_file_handle["roi"]).T) > 0 +control_file_handle.close() +control_roi = control_roi.reshape((control_roi.shape[0] * control_roi.shape[1])) + +s_darken_file_handle = h5py.File("ROI_sDarken_49.mat", "r") +s_darken_roi = (np.array(s_darken_file_handle["roi"]).T) > 0 +s_darken_file_handle.close() +s_darken_roi = s_darken_roi.reshape((s_darken_roi.shape[0] * s_darken_roi.shape[1])) + +mylogger = create_logger( + save_logging_messages=True, display_logging_messages=True, log_stage_name="test_xxx" +) +config = load_config(mylogger=mylogger) + +experiment_id: int = 1 + +raw_data_path: str = os.path.join( + config["basic_path"], + config["recoding_data"], + config["mouse_identifier"], + config["raw_path"], +) + +data_path: str = "output" + +trails = get_trials(path=raw_data_path, experiment_id=experiment_id) +for i in range(0, trails.shape[0]): + trial_id = int(trails[i]) + experiment_name: str = f"Exp{experiment_id:03d}_Trial{trial_id:03d}" + mylogger.info(f"Loading files for {experiment_name}") + + data = np.load(os.path.join(data_path, f"{experiment_name}_ratio_sequence.npz")) + if i == 0: + ratio_sequence = data["ratio_sequence"] + else: + ratio_sequence += data["ratio_sequence"] + +ratio_sequence /= float(trails.shape[0]) + +ratio_sequence = ratio_sequence.reshape( + (ratio_sequence.shape[0] * ratio_sequence.shape[1], ratio_sequence.shape[2]) +) + +control = ratio_sequence[control_roi, :].mean(axis=0) +s_darken = ratio_sequence[s_darken_roi, :].mean(axis=0) + +t = np.arange(0, control.shape[0]) / 100.0 + +plt.plot(t, control, label="control") +plt.plot(t, s_darken, label="sDarken") +plt.legend() +plt.xlabel("Time [sec]") +plt.show() diff --git a/stage_4_process.py b/stage_4_process.py index c0569d4..05abaea 100644 --- a/stage_4_process.py +++ b/stage_4_process.py @@ -708,68 +708,77 @@ def process_trial( mylogger.info("Move time dimensions of data to the last dimension") data = data.movedim(1, -1) - mylogger.info("Regression Acceptor") - mylogger.info(f"Target: {config['target_camera_acceptor']}") - mylogger.info( - f"Regressors: constant, linear and {config['regressor_cameras_acceptor']}" - ) - target_id: int = config["required_order"].index(config["target_camera_acceptor"]) - regressor_id: list[int] = [] - for i in range(0, len(config["regressor_cameras_acceptor"])): - regressor_id.append( - config["required_order"].index(config["regressor_cameras_acceptor"][i]) + dual_signal_mode: bool = True + if len(config["target_camera_acceptor"]) > 0: + mylogger.info("Regression Acceptor") + mylogger.info(f"Target: {config['target_camera_acceptor']}") + mylogger.info( + f"Regressors: constant, linear and {config['regressor_cameras_acceptor']}" + ) + target_id: int = config["required_order"].index( + config["target_camera_acceptor"] + ) + regressor_id: list[int] = [] + for i in range(0, len(config["regressor_cameras_acceptor"])): + regressor_id.append( + config["required_order"].index(config["regressor_cameras_acceptor"][i]) + ) + + data_acceptor, coefficients_acceptor = regression( + mylogger=mylogger, + target_camera_id=target_id, + regressor_camera_ids=regressor_id, + mask=mask_negative, + data=data, + data_filtered=data_filtered, + first_none_ramp_frame=int(config["skip_frames_in_the_beginning"]), ) - data_acceptor, coefficients_acceptor = regression( - mylogger=mylogger, - target_camera_id=target_id, - regressor_camera_ids=regressor_id, - mask=mask_negative, - data=data, - data_filtered=data_filtered, - first_none_ramp_frame=int(config["skip_frames_in_the_beginning"]), - ) + if config["save_regression_coefficients"]: + temp_path = os.path.join( + config["export_path"], experiment_name + "_coefficients_acceptor.npy" + ) + mylogger.info(f"Save acceptor coefficients to {temp_path}") + np.save(temp_path, coefficients_acceptor.cpu()) + del coefficients_acceptor - if config["save_regression_coefficients"]: - temp_path = os.path.join( - config["export_path"], experiment_name + "_coefficients_acceptor.npy" + mylogger.info("-==- Done -==-") + else: + dual_signal_mode = False + + if len(config["target_camera_donor"]) > 0: + mylogger.info("Regression Donor") + mylogger.info(f"Target: {config['target_camera_donor']}") + mylogger.info( + f"Regressors: constant, linear and {config['regressor_cameras_donor']}" ) - mylogger.info(f"Save acceptor coefficients to {temp_path}") - np.save(temp_path, coefficients_acceptor.cpu()) - del coefficients_acceptor + target_id = config["required_order"].index(config["target_camera_donor"]) + regressor_id = [] + for i in range(0, len(config["regressor_cameras_donor"])): + regressor_id.append( + config["required_order"].index(config["regressor_cameras_donor"][i]) + ) - mylogger.info("-==- Done -==-") - - mylogger.info("Regression Donor") - mylogger.info(f"Target: {config['target_camera_donor']}") - mylogger.info( - f"Regressors: constant, linear and {config['regressor_cameras_donor']}" - ) - target_id = config["required_order"].index(config["target_camera_donor"]) - regressor_id = [] - for i in range(0, len(config["regressor_cameras_donor"])): - regressor_id.append( - config["required_order"].index(config["regressor_cameras_donor"][i]) + data_donor, coefficients_donor = regression( + mylogger=mylogger, + target_camera_id=target_id, + regressor_camera_ids=regressor_id, + mask=mask_negative, + data=data, + data_filtered=data_filtered, + first_none_ramp_frame=int(config["skip_frames_in_the_beginning"]), ) - data_donor, coefficients_donor = regression( - mylogger=mylogger, - target_camera_id=target_id, - regressor_camera_ids=regressor_id, - mask=mask_negative, - data=data, - data_filtered=data_filtered, - first_none_ramp_frame=int(config["skip_frames_in_the_beginning"]), - ) - - if config["save_regression_coefficients"]: - temp_path = os.path.join( - config["export_path"], experiment_name + "_coefficients_donor.npy" - ) - mylogger.info(f"Save acceptor donor to {temp_path}") - np.save(temp_path, coefficients_donor.cpu()) - del coefficients_donor - mylogger.info("-==- Done -==-") + if config["save_regression_coefficients"]: + temp_path = os.path.join( + config["export_path"], experiment_name + "_coefficients_donor.npy" + ) + mylogger.info(f"Save acceptor donor to {temp_path}") + np.save(temp_path, coefficients_donor.cpu()) + del coefficients_donor + mylogger.info("-==- Done -==-") + else: + dual_signal_mode = False del data del data_filtered @@ -783,14 +792,21 @@ def process_trial( mylogger.info(f"CUDA memory: {free_mem//1024} MByte") mylogger.info("Calculate ratio sequence") - if config["classical_ratio_mode"]: - mylogger.info("via acceptor / donor") - ratio_sequence: torch.Tensor = data_acceptor / data_donor - mylogger.info("via / mean over time") - ratio_sequence /= ratio_sequence.mean(dim=-1, keepdim=True) + if dual_signal_mode: + if config["classical_ratio_mode"]: + mylogger.info("via acceptor / donor") + ratio_sequence: torch.Tensor = data_acceptor / data_donor + mylogger.info("via / mean over time") + ratio_sequence /= ratio_sequence.mean(dim=-1, keepdim=True) + else: + mylogger.info("via 1.0 + acceptor - donor") + ratio_sequence = 1.0 + data_acceptor - data_donor else: - mylogger.info("via 1.0 + acceptor - donor") - ratio_sequence = 1.0 + data_acceptor - data_donor + mylogger.info("mono signal model") + if len(config["target_camera_donor"]) > 0: + ratio_sequence = data_donor.clone() + else: + ratio_sequence = data_acceptor.clone() mylogger.info("Remove nan") ratio_sequence = torch.nan_to_num(ratio_sequence, nan=0.0) @@ -884,6 +900,15 @@ if (config["save_as_python"] is False) and (config["save_as_matlab"] is False): mylogger.info("ERROR: STOP!!!") exit() +if (len(config["target_camera_donor"]) == 0) and ( + len(config["target_camera_acceptor"]) == 0 +): + mylogger.info( + "Configure at least target_camera_donor or target_camera_acceptor correctly." + ) + mylogger.info("ERROR: STOP!!!") + exit() + device = get_torch_device(mylogger, config["force_to_cpu"]) mylogger.info(f"Create directory {config['export_path']} in the case it does not exist")