[main] adding convertion scripts

This commit is contained in:
wendtalexander 2025-10-07 16:06:58 +02:00
parent f34cbc6cd2
commit 1af6fa4f7b

View File

@ -7,6 +7,8 @@ from IPython import embed
from rich.console import Console
from oephys2nix.logging import setup_logging
from oephys2nix.stimulus_recreation import StimulusToNix
from oephys2nix.tonix import RawToNix
app = typer.Typer()
log = logging.getLogger(__name__)
@ -25,30 +27,25 @@ def main(
readable=True,
resolve_path=True,
),
ttl: bool = typer.Option(
default=True, help="For recordings that did not have a ttl pulse"
),
ttl: bool = typer.Option(default=True, help="For recordings that did not have a ttl pulse"),
overwrite: bool = typer.Option(default=True, help="Overwrites nix file"),
):
"""
Combines open ephys data with relacs data from data_path to a new nix file
"""
debug: bool = typer.Option(default=True, help="Shows more information and plots the results"),
) -> None:
"""Combines open ephys data with relacs data from data_path to a new nix file."""
log.info(f"Selected data_path is {data_path}")
open_ephys_data_paths = list(Path(data_path).rglob("*open-ephys"))
relacs_data_paths = list(Path(data_path).rglob("*relacs"))
relacs_data_paths = list(Path(data_path).rglob("*relacs/*.nix"))
if not len(open_ephys_data_paths) == len(relacs_data_paths):
if len(open_ephys_data_paths) != len(relacs_data_paths):
log.error(
f"Missmatch in data directory found open-ephys data {len(open_ephys_data_paths)} and relacs data {len(relacs_data_paths)}"
f"Missmatch in data directory found open-ephys data {len(open_ephys_data_paths)} "
f"and relacs data {len(relacs_data_paths)}"
)
log.error("Please check if both are present")
raise typer.Exit()
for open_ephys, relacs in zip(
open_ephys_data_paths, relacs_data_paths, strict=True
):
nix_file_name = relacs.name.split("_")[0] + "-recording.nix"
for open_ephys, relacs in zip(open_ephys_data_paths, relacs_data_paths, strict=True):
nix_file_name = relacs.parent.name.split("_")[0] + "-recording.nix"
console.print(
f"Converting [bold cyan]{open_ephys.name}[/bold cyan] and "
@ -56,20 +53,38 @@ def main(
f"[green]{nix_file_name}[/green]"
)
nix_path = relacs.parent / nix_file_name
nix_path = relacs.parent.parent / nix_file_name
if nix_path.exists and nix_path.is_file():
if overwrite:
log.warning("Overwriting nix file")
nix_files = nixio.File(str(nix_file_name), nixio.FileMode.Overwrite)
nix_file = nixio.File(str(nix_path), nixio.FileMode.Overwrite)
else:
log.error(
"Converted nix file already exits, and Overwrite is not enabled"
)
log.error("Converted nix file already exits, and Overwrite is not enabled")
raise typer.Exit()
else:
log.debug("Creating nix file")
nix_files = nixio.File(str(nix_file_name), nixio.FileMode.Overwrite)
nix_file = nixio.File(str(nix_path), nixio.FileMode.Overwrite)
nix_file.close()
log.info("Appending raw data from relacs and open-ephys to new nix-file")
to_nix = RawToNix(open_ephys, str(relacs), str(nix_path))
to_nix.append_section()
to_nix.append_fish_lines()
to_nix.append_relacs_lines()
to_nix.append_raw_data()
to_nix.close()
log.info("Finished!")
log.info("Starting with stimulus recreation")
stim = StimulusToNix(open_ephys, str(relacs), str(nix_path))
stim.create_repros_automatically()
stim.print_table()
stim.checks()
if debug:
stim.plot_stimulus()
if __name__ == "__main__":