Coverage for sleapyfaces/structs.py: 93%
42 statements
« prev ^ index » next coverage.py v7.0.2, created at 2023-01-03 12:07 -0800
« prev ^ index » next coverage.py v7.0.2, created at 2023-01-03 12:07 -0800
1from dataclasses import dataclass
2from os import PathLike
3import os
4import glob
5import pandas as pd
8@dataclass(slots=True)
9class File:
10 """A structured file object that contains the base path and filename of a file.
12 Class Attributes:
13 file_str: the location of the file.
15 Instance Attributes:
16 file: the location of the file.
17 filename: the name of the file.
18 basepath: the base path of the file.
19 iPath: the ith base path of the file path. (i.e. iPath(1) returns the second to last path in the file path.)
20 """
22 get_glob: bool
23 file: str | PathLike[str]
24 basepath: str | PathLike[str]
25 filename: str
27 def __init__(self, basepath: str | PathLike[str], filename: str, get_glob=False):
28 self.basepath = basepath
29 self.filename = filename
30 self.get_glob = get_glob
31 if self.get_glob:
32 self.file = glob.glob(os.path.join(self.basepath, self.filename))[0]
33 else:
34 self.file = os.path.join(self.basepath, self.filename)
36 def iPath(self, i: int) -> str:
37 """Returns the ith path in the file path."""
38 return "/".join(self.file.split("/")[:-i])
41@dataclass(slots=True)
42class FileConstructor:
44 """Takes in the base paths and filenames of the experimental data and returns them as a structured object.
46 Args:
47 DAQFile (File): The location of the DAQ data file.
48 SLEAPFile (File): The location of the SLEAP analysis file.
49 BehFile (File): The location of the behavioral metadata file.
50 VideoFile (File): The location of the video file.
52 Attributes:
53 daq (File): The location of the DAQ file as a structured File object.
54 sleap (File): The location of the SLEAP analysis file as a structured File object.
55 beh (File): The location of the behavioral metadata file as a structured File object.
56 video (File): The location of the video file as a structured File object.
57 """
59 daq: File
60 sleap: File
61 beh: File
62 video: File
64 def __init__(
65 self, daq_file: File, sleap_file: File, beh_file: File, video_file: File
66 ) -> None:
67 self.daq = daq_file
68 self.sleap = sleap_file
69 self.beh = beh_file
70 self.video = video_file
73@dataclass(slots=True)
74class CustomColumn:
75 """Builds an annotation column for the base dataframe.
77 Attrs:
78 ColumnTitle (str): The title of the column.
79 ColumnData (str | int | float | bool): The data to be added to the column.
80 Column (pd.Series): The column to be added to the base dataframe.
81 """
83 ColumnTitle: str
84 ColumnData: str | int | float | bool
85 Column: pd.Series
87 def __init__(self, ColumnTitle: str, ColumnData: str | int | float | bool):
88 self.ColumnTitle = ColumnTitle
89 self.ColumnData = ColumnData
91 def buildColumn(self, length: int) -> None:
92 """Initializes a column of a given length.
94 Args:
95 length (int): The length of the column to be built.
97 Initializes Attributes:
98 Column (pd.DataFrame): The initialized column at a given length.
99 """
100 self.Column = [self.ColumnData] * length
101 self.Column = pd.DataFrame(self.Column, columns=[self.ColumnTitle])