Skip to content

Commit 59acfb9

Browse files
add new files to file info and collect window summary
1 parent c5c48ef commit 59acfb9

File tree

2 files changed

+128
-2
lines changed

2 files changed

+128
-2
lines changed

ephys/GUI/datacontainer.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
from ephys.classes.experiment_objects import ExpData
3+
4+
5+
class DataContainer:
6+
def __init__(self, data: ExpData):
7+
self.data = data
8+
9+
def get_data(self) -> ExpData:
10+
return self.data
11+
12+
def set_data(self, new_data: ExpData) -> None:
13+
self.data = new_data
14+
15+
def new_files_check(self, file_path: str | list[str]) -> list[str]:
16+
# Implement duplicate checking logic here
17+
existing_file_path = self.data.meta_data.get_file_path()
18+
new_files = np.array(file_path)[
19+
np.invert(np.isin(file_path, existing_file_path))
20+
]
21+
if new_files.size == 0:
22+
print("No new files to add.")
23+
else:
24+
print(f"New files detected: {new_files}")
25+
return new_files.tolist()
26+
27+
def add_file(self, file_path: str | list[str]) -> None:
28+
new_files = self.new_files_check(file_path)
29+
self.data.add_file(new_files)
30+
31+
def __repr__(self) -> str:
32+
return f"DataContainer(data={self.data})"

ephys/classes/experiment_objects.py

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os
66
import struct
7+
from typing import Any
78

89
from datetime import datetime, timedelta
910
import matplotlib.style as mplstyle
@@ -366,9 +367,16 @@ def add_file(
366367
"""
367368
if isinstance(file_path, str):
368369
file_path = [file_path]
369-
for file in file_path:
370+
371+
# Check for duplicates
372+
existing_file_path = self.meta_data.get_file_path()
373+
new_files = np.array(file_path)[
374+
np.invert(np.isin(file_path, existing_file_path))
375+
].tolist()
376+
377+
for file in new_files:
370378
self.protocols.append(Trace(file))
371-
self.meta_data.add_file_info(file_path, experimenter)
379+
self.meta_data.add_file_info(new_files, experimenter)
372380
if sort:
373381
self.sort_by_date()
374382

@@ -425,5 +433,91 @@ def meta_data_summary(self, to_dataframe: bool = True) -> dict | pd.DataFrame:
425433
return pd.DataFrame(summary_dict)
426434
return summary_dict
427435

436+
def get_window_summary(self, remove_duplicates: bool = True) -> pd.DataFrame:
437+
"""
438+
Returns a summary of the window information for each protocol.
439+
"""
440+
pd_collection = []
441+
meta_df = self.meta_data_summary()
442+
exp_start = np.array(
443+
[
444+
protocol.rec_datetime.timestamp()
445+
for protocol in self.protocols
446+
if protocol.rec_datetime is not None
447+
]
448+
).min()
449+
for i, protocol in enumerate(self.protocols):
450+
protocol: Trace
451+
df = protocol.window_summary.to_dataframe()
452+
df["file_name"] = meta_df["file_name"][i]
453+
if protocol.rec_datetime is not None:
454+
df["exp_time"] = (
455+
df["time"] + protocol.rec_datetime.timestamp() - exp_start
456+
)
457+
df["protocol"] = protocol
458+
pd_collection.append(df)
459+
df_out = pd.concat(pd_collection, ignore_index=True)
460+
if remove_duplicates:
461+
df_out.drop_duplicates(inplace=True)
462+
return df_out
463+
464+
def window_function(
465+
self,
466+
window: list | tuple | None = None,
467+
channels: Any = None,
468+
signal_type: Any = None,
469+
rec_type: str = "",
470+
function: str = "mean",
471+
label: str = "",
472+
sweep_subset: Any = None,
473+
return_output: bool = False,
474+
plot_individual: bool = False,
475+
) -> None:
476+
for protocol in self.protocols:
477+
protocol: Trace
478+
protocol.window_function(
479+
window=window,
480+
channels=channels,
481+
signal_type=signal_type,
482+
rec_type=rec_type,
483+
function=function,
484+
label=label,
485+
sweep_subset=sweep_subset,
486+
return_output=return_output,
487+
plot=plot_individual,
488+
)
489+
490+
def label_diff(
491+
self, labels: list | None = None, new_name: str = "", time_label: str = ""
492+
) -> None:
493+
for protocol in self.protocols:
494+
protocol: Trace
495+
protocol.window_summary.label_diff(
496+
labels=labels, new_name=new_name, time_label=time_label
497+
)
498+
499+
def label_ratio(
500+
self, labels: list | None = None, new_name: str = "", time_label: str = ""
501+
) -> None:
502+
for protocol in self.protocols:
503+
protocol: Trace
504+
protocol.window_summary.label_ratio(
505+
labels=labels, new_name=new_name, time_label=time_label
506+
)
507+
508+
def plot_summary(
509+
self,
510+
align_onset: bool = False,
511+
**kwargs,
512+
) -> None:
513+
from ephys.classes.window_functions import FunctionOutput
514+
515+
df_complete: pd.DataFrame = self.get_window_summary()
516+
summary_output = FunctionOutput()
517+
df_complete = df_complete.drop(columns=["time"], errors="ignore")
518+
df_complete = df_complete.rename(columns={"exp_time": "time"})
519+
summary_output.from_dataframe(df=df_complete)
520+
summary_output.plot(plot_trace=False, align_onset=align_onset, **kwargs)
521+
428522
# TODO: get summary outputs for the experiment
429523
# TODO: get summary plots for the experiment

0 commit comments

Comments
 (0)