|
4 | 4 |
|
5 | 5 | import os |
6 | 6 | import struct |
| 7 | +from typing import Any |
7 | 8 |
|
8 | 9 | from datetime import datetime, timedelta |
9 | 10 | import matplotlib.style as mplstyle |
@@ -366,9 +367,16 @@ def add_file( |
366 | 367 | """ |
367 | 368 | if isinstance(file_path, str): |
368 | 369 | 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: |
370 | 378 | 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) |
372 | 380 | if sort: |
373 | 381 | self.sort_by_date() |
374 | 382 |
|
@@ -425,5 +433,91 @@ def meta_data_summary(self, to_dataframe: bool = True) -> dict | pd.DataFrame: |
425 | 433 | return pd.DataFrame(summary_dict) |
426 | 434 | return summary_dict |
427 | 435 |
|
| 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 | + |
428 | 522 | # TODO: get summary outputs for the experiment |
429 | 523 | # TODO: get summary plots for the experiment |
0 commit comments