Skip to content

Experiment

behavysis_pipeline.pipeline.experiment.Experiment

Behavysis Pipeline class for a single experiment.

Encompasses the entire process including: - Raw mp4 file import. - mp4 file formatting (px and fps). - DLC keypoints inference. - Feature wrangling (start time detection, more features like average body position). - Interpretable behaviour results. - Other quantitative analysis.

Parameters:

Name Type Description Default
name str

description

required
root_dir str

description

required

Raises:

Type Description
ValueError

ValueError: root_dir does not exist or name does not exist in the root_dir folder.

Source code in behavysis_pipeline/pipeline/experiment.py
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
class Experiment:
    """
    Behavysis Pipeline class for a single experiment.

    Encompasses the entire process including:
    - Raw mp4 file import.
    - mp4 file formatting (px and fps).
    - DLC keypoints inference.
    - Feature wrangling (start time detection, more features like average body position).
    - Interpretable behaviour results.
    - Other quantitative analysis.

    Parameters
    ----------
    name : str
        _description_
    root_dir : str
        _description_

    Raises
    ------
    ValueError
        ValueError: `root_dir` does not exist or `name` does not exist in the `root_dir` folder.
    """

    def __init__(self, name: str, root_dir: str) -> None:
        """
        Make a Experiment instance.
        """
        # Assertion: root_dir mus† exist
        if not os.path.isdir(root_dir):
            raise ValueError(
                f'Cannot find the project folder named "{root_dir}".\n'
                + "Please specify a folder that exists."
            )
        # Assertion: name must correspond to at least one file in root_dir
        file_exists_ls = [
            os.path.isfile(os.path.join(root_dir, f.value, f"{name}{FILE_EXTS[f]}"))
            for f in Folders
        ]
        if not np.any(file_exists_ls):
            raise ValueError(
                f'No files named "{name}" exist in "{root_dir}".\n'
                + f'Please specify a file that exists in "{root_dir}", in one of the'
                + " following folder WITH the correct file extension name:\n"
                + "    - "
                + "\n    - ".join(DFIOMixin.enum_to_list(Folders))
            )
        self.name = name
        self.root_dir = os.path.abspath(root_dir)

    #####################################################################
    #               GET/CHECK FILEPATH METHODS
    #####################################################################

    def get_fp(self, folder_str: str) -> str:
        """
        Returns the experiment's file path from the given folder.

        Parameters
        ----------
        folder_str : str
            The folder to return the experiment document's filepath for.

        Returns
        -------
        str
            The experiment document's filepath.

        Raises
        ------
        ValueError
            ValueError: Folder name is not valid. Refer to FOLDERS constant for valid folder names.
        """
        # Getting folder enum from string
        folder = next((f for f in Folders if folder_str == f.value), None)
        # Assertion: The given folder name must be valid
        if not folder:
            raise ValueError(
                f'"{folder_str}" is not a valid experiment folder name.\n'
                + "Please only specify one of the following folders:\n"
                + "    - "
                + "\n    - ".join([f.value for f in Folders])
            )
        # Getting experiment filepath for given folder
        fp = os.path.join(
            self.root_dir, folder.value, f"{self.name}{FILE_EXTS[folder]}"
        )
        # Making a folder if it does not exist
        os.makedirs(os.path.split(fp)[0], exist_ok=True)
        # Returning filepath
        return fp

    #####################################################################
    #               EXPERIMENT PROCESSING SCAFFOLD METHODS
    #####################################################################

    def _process_scaffold(
        self,
        funcs: tuple[Callable, ...],
        *args: Any,
        **kwargs: Any,
    ) -> dict[str, str]:
        """
        All processing runs through here.
        This method ensures that the stdout and diagnostics dict are correctly generated.

        Parameters
        ----------
        funcs : tuple[Callable, ...]
            List of functions.

        Returns
        -------
        dict[str, str]
            Diagnostics dictionary, with description of each function's outcome.

        Notes
        -----
        Each func in `funcs` is called in the form:
        ```
        func(*args, **kwargs)
        ```
        """
        logging.info(f"Processing experiment: {self.name}")
        # Setting up diagnostics dict
        dd = {"experiment": self.name}
        # Running functions and saving outcome to diagnostics dict
        for f in funcs:
            # Running each func and saving outcome
            try:
                dd[f.__name__] = f(*args, **kwargs)
                dd[f.__name__] += f"SUCCESS: {DiagnosticsMixin.success_msg()}\n"
            except Exception as e:
                dd[f.__name__] = f"ERROR: {e}"
            logging.info(f"{f.__name__}: {dd[f.__name__]}")
        logging.info(STR_DIV)
        return dd

    #####################################################################
    #                        CONFIG FILE METHODS
    #####################################################################

    def update_configs(self, default_configs_fp: str, overwrite: str) -> dict:
        """
        Initialises the JSON config files with the given configurations in `configs`.
        It can be specified whether or not to overwrite existing configuration values.

        Parameters
        ----------
        default_configs_fp : str
            The JSON configs filepath to add/overwrite to the experiment's current configs file.
        overwrite : {"set", "reset"}
            Specifies how to overwrite existing configurations.
            If `add`, only parameters in `configs` not already in the config files are added.
            If `set`, all parameters in `configs` are set in the config files (overwriting).
            If `reset`, the config files are completely replaced by `configs`.

        Returns
        -------
        dict
            Diagnostics dictionary, with description of each function's outcome.
        """
        return self._process_scaffold(
            (UpdateConfigs.update_configs,),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
            default_configs_fp=default_configs_fp,
            overwrite=overwrite,
        )

    #####################################################################
    #                    FORMATTING VIDEO METHODS
    #####################################################################

    def format_vid(self, funcs: tuple[Callable, ...], overwrite: bool) -> dict:
        """
        Formats the video with ffmpeg to fit the formatted configs (e.g. fps and resolution_px).
        Once the formatted video is produced, the configs dict and *configs.json file are
        updated with the formatted video's metadata.

        Parameters
        ----------
        funcs : tuple[Callable, ...]
            _description_
        overwrite : bool
            Whether to overwrite the output file (if it exists).

        Returns
        -------
        dict
            Diagnostics dictionary, with description of each function's outcome.

        Notes
        -----
        Can call any methods from `FormatVid`.
        """
        return self._process_scaffold(
            funcs,
            in_fp=self.get_fp(Folders.RAW_VID.value),
            out_fp=self.get_fp(Folders.FORMATTED_VID.value),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
            overwrite=overwrite,
        )

    #####################################################################
    #                        DLC METHODS
    #####################################################################

    def run_dlc(self, gputouse: int, overwrite: bool) -> dict:
        """
        Run the DLC model on the formatted video to generate a DLC annotated video
        and DLC h5 file for all experiments.

        Parameters
        ----------
        gputouse : int
            _description_
        overwrite : bool
            Whether to overwrite the output file (if it exists).

        Returns
        -------
        dict
            Diagnostics dictionary, with description of each function's outcome.

        Notes
        -----
        Can call any methods from `RunDLC`.
        """
        return self._process_scaffold(
            (RunDLC.ma_dlc_analyse_single,),
            in_fp=self.get_fp(Folders.FORMATTED_VID.value),
            out_fp=self.get_fp(Folders.DLC.value),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
            temp_dir=os.path.join(self.root_dir, TEMP_DIR),
            gputouse=gputouse,
            overwrite=overwrite,
        )

    def calculate_params(self, funcs: tuple[Callable, ...]) -> dict:
        """
        A pipeline to calculate the parameters of the raw DLC file, which will
        assist in preprocessing the DLC data.

        Parameters
        ----------
        funcs : Tuple[Callable, ...]
            _description_

        Returns
        -------
        Dict
            Diagnostics dictionary, with description of each function's outcome.

        Notes
        -----
        Can call any methods from `CalculateParams`.
        """
        return self._process_scaffold(
            funcs,
            dlc_fp=self.get_fp(Folders.DLC.value),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
        )

    def preprocess(self, funcs: tuple[Callable, ...], overwrite: bool) -> dict:
        """
        A preprocessing pipeline method to convert raw DLC data into preprocessed
        DLC data that is ready for ML analysis.
        All functs passed in must have the format func(df, dict) -> df. Possible funcs
        are given in preprocessing.py
        The preprocessed data is saved to the project's preprocessed folder.

        Parameters
        ----------
        funcs : tuple[Callable, ...]
            _description_
        overwrite : bool
            Whether to overwrite the output file (if it exists).

        Returns
        -------
        dict
            Diagnostics dictionary, with description of each function's outcome.

        Notes
        -----
        Can call any methods from `Preprocess`.
        """
        # Exporting 3_dlc df to 4_preprocessed folder
        dd = self._process_scaffold(
            (Export.feather_2_feather,),
            src_fp=self.get_fp(Folders.DLC.value),
            out_fp=self.get_fp(Folders.PREPROCESSED.value),
            overwrite=overwrite,
        )
        # If there is an error, OR warning (indicates not to ovewrite), then return early
        res = dd["feather_2_feather"]
        if res.startswith("ERROR") or res.startswith("WARNING"):
            return dd
        # Feeding through preprocessing functions
        return self._process_scaffold(
            funcs,
            in_fp=self.get_fp(Folders.PREPROCESSED.value),
            out_fp=self.get_fp(Folders.PREPROCESSED.value),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
            overwrite=True,
        )

    #####################################################################
    #                 SIMBA BEHAVIOUR CLASSIFICATION METHODS
    #####################################################################

    def extract_features(self, overwrite: bool) -> dict:
        """
        Extracts features from the preprocessed dlc file to generate many more features.
        This dataframe of derived features will be input for a ML classifier to detect
        particularly trained behaviours.

        Parameters
        ----------
        overwrite : bool
            Whether to overwrite the output file (if it exists).

        Returns
        -------
        dict
            Diagnostics dictionary, with description of each function's outcome.
        """
        return self._process_scaffold(
            (ExtractFeatures.extract_features,),
            dlc_fp=self.get_fp(Folders.PREPROCESSED.value),
            out_fp=self.get_fp(Folders.FEATURES_EXTRACTED.value),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
            temp_dir=os.path.join(self.root_dir, TEMP_DIR),
            overwrite=overwrite,
        )

    def classify_behaviours(self, overwrite: bool) -> dict:
        """
        Given model config files in the BehavClassifier format, generates beahviour predidctions
        on the given extracted features dataframe.

        Parameters
        ----------
        overwrite : bool
            Whether to overwrite the output file (if it exists).

        Returns
        -------
        dict
            Diagnostics dictionary, with description of each function's outcome.
        """
        return self._process_scaffold(
            (ClassifyBehaviours.classify_behaviours,),
            features_fp=self.get_fp(Folders.FEATURES_EXTRACTED.value),
            out_fp=self.get_fp(Folders.PREDICTED_BEHAVS.value),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
            overwrite=overwrite,
        )

    def export_behaviours(self, overwrite: bool) -> dict:
        """
        _summary_

        Parameters
        ----------
        overwrite : bool
            _description_

        Returns
        -------
        dict
            _description_
        """
        # Exporting 6_predicted_behavs df to 7_scored_behavs folder
        return self._process_scaffold(
            (Export.predbehav_2_scoredbehav,),
            src_fp=self.get_fp(Folders.PREDICTED_BEHAVS.value),
            out_fp=self.get_fp(Folders.SCORED_BEHAVS.value),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
            overwrite=overwrite,
        )

    #####################################################################
    #                     SIMPLE ANALYSIS METHODS
    #####################################################################

    def analyse(self, funcs: tuple[Callable, ...]) -> dict:
        """
        An ML pipeline method to analyse the preprocessed DLC data.
        Possible funcs are given in analysis.py.
        The preprocessed data is saved to the project's analysis folder.

        Parameters
        ----------
        funcs : tuple[Callable, ...]
            _description_

        Returns
        -------
        dict
            Diagnostics dictionary, with description of each function's outcome.

        Notes
        -----
        Can call any methods from `Analyse`.
        """
        return self._process_scaffold(
            funcs,
            dlc_fp=self.get_fp(Folders.PREPROCESSED.value),
            analysis_dir=os.path.join(self.root_dir, ANALYSIS_DIR),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
        )

    def behav_analyse(self) -> dict:
        """
        An ML pipeline method to analyse the preprocessed DLC data.
        Possible funcs are given in analysis.py.
        The preprocessed data is saved to the project's analysis folder.

        Returns
        -------
        dict
            Diagnostics dictionary, with description of each function's outcome.

        Notes
        -----
        Can call any methods from `Analyse`.
        """
        return self._process_scaffold(
            (BehavAnalyse.behav_analysis,),
            behavs_fp=self.get_fp(Folders.SCORED_BEHAVS.value),
            analysis_dir=os.path.join(self.root_dir, ANALYSIS_DIR),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
        )

    #####################################################################
    #           EVALUATING DLC ANALYSIS AND BEHAV CLASSIFICATION
    #####################################################################

    def export_feather(self, in_dir: str, out_dir: str, overwrite: bool) -> dict:
        """
        _summary_

        Parameters
        ----------
        in_dir : str
            _description_
        out_dir : str
            _description_

        Returns
        -------
        dict
            _description_
        """
        return self._process_scaffold(
            (Export.feather_2_csv,),
            in_fp=self.get_fp(in_dir),
            out_fp=os.path.join(out_dir, f"{self.name}.csv"),
            overwrite=overwrite,
        )

    def evaluate(self, funcs, overwrite: bool) -> dict:
        """
        Evaluating preprocessed DLC data and scored_behavs data.

        Parameters
        ----------
        funcs : _type_
            _description_
        overwrite : bool
            Whether to overwrite the output file (if it exists).

        Returns
        -------
        dict
            Diagnostics dictionary, with description of each function's outcome.
        """
        return self._process_scaffold(
            funcs,
            vid_fp=self.get_fp(Folders.FORMATTED_VID.value),
            dlc_fp=self.get_fp(Folders.PREPROCESSED.value),
            behavs_fp=self.get_fp(Folders.SCORED_BEHAVS.value),
            out_dir=os.path.join(self.root_dir, EVALUATE_DIR),
            configs_fp=self.get_fp(Folders.CONFIGS.value),
            overwrite=overwrite,
        )

__init__(name, root_dir)

Make a Experiment instance.

Source code in behavysis_pipeline/pipeline/experiment.py
def __init__(self, name: str, root_dir: str) -> None:
    """
    Make a Experiment instance.
    """
    # Assertion: root_dir mus† exist
    if not os.path.isdir(root_dir):
        raise ValueError(
            f'Cannot find the project folder named "{root_dir}".\n'
            + "Please specify a folder that exists."
        )
    # Assertion: name must correspond to at least one file in root_dir
    file_exists_ls = [
        os.path.isfile(os.path.join(root_dir, f.value, f"{name}{FILE_EXTS[f]}"))
        for f in Folders
    ]
    if not np.any(file_exists_ls):
        raise ValueError(
            f'No files named "{name}" exist in "{root_dir}".\n'
            + f'Please specify a file that exists in "{root_dir}", in one of the'
            + " following folder WITH the correct file extension name:\n"
            + "    - "
            + "\n    - ".join(DFIOMixin.enum_to_list(Folders))
        )
    self.name = name
    self.root_dir = os.path.abspath(root_dir)

analyse(funcs)

An ML pipeline method to analyse the preprocessed DLC data. Possible funcs are given in analysis.py. The preprocessed data is saved to the project's analysis folder.

Parameters:

Name Type Description Default
funcs tuple[Callable, ...]

description

required

Returns:

Type Description
dict

Diagnostics dictionary, with description of each function's outcome.

Notes

Can call any methods from Analyse.

Source code in behavysis_pipeline/pipeline/experiment.py
def analyse(self, funcs: tuple[Callable, ...]) -> dict:
    """
    An ML pipeline method to analyse the preprocessed DLC data.
    Possible funcs are given in analysis.py.
    The preprocessed data is saved to the project's analysis folder.

    Parameters
    ----------
    funcs : tuple[Callable, ...]
        _description_

    Returns
    -------
    dict
        Diagnostics dictionary, with description of each function's outcome.

    Notes
    -----
    Can call any methods from `Analyse`.
    """
    return self._process_scaffold(
        funcs,
        dlc_fp=self.get_fp(Folders.PREPROCESSED.value),
        analysis_dir=os.path.join(self.root_dir, ANALYSIS_DIR),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
    )

behav_analyse()

An ML pipeline method to analyse the preprocessed DLC data. Possible funcs are given in analysis.py. The preprocessed data is saved to the project's analysis folder.

Returns:

Type Description
dict

Diagnostics dictionary, with description of each function's outcome.

Notes

Can call any methods from Analyse.

Source code in behavysis_pipeline/pipeline/experiment.py
def behav_analyse(self) -> dict:
    """
    An ML pipeline method to analyse the preprocessed DLC data.
    Possible funcs are given in analysis.py.
    The preprocessed data is saved to the project's analysis folder.

    Returns
    -------
    dict
        Diagnostics dictionary, with description of each function's outcome.

    Notes
    -----
    Can call any methods from `Analyse`.
    """
    return self._process_scaffold(
        (BehavAnalyse.behav_analysis,),
        behavs_fp=self.get_fp(Folders.SCORED_BEHAVS.value),
        analysis_dir=os.path.join(self.root_dir, ANALYSIS_DIR),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
    )

calculate_params(funcs)

A pipeline to calculate the parameters of the raw DLC file, which will assist in preprocessing the DLC data.

Parameters:

Name Type Description Default
funcs Tuple[Callable, ...]

description

required

Returns:

Type Description
Dict

Diagnostics dictionary, with description of each function's outcome.

Notes

Can call any methods from CalculateParams.

Source code in behavysis_pipeline/pipeline/experiment.py
def calculate_params(self, funcs: tuple[Callable, ...]) -> dict:
    """
    A pipeline to calculate the parameters of the raw DLC file, which will
    assist in preprocessing the DLC data.

    Parameters
    ----------
    funcs : Tuple[Callable, ...]
        _description_

    Returns
    -------
    Dict
        Diagnostics dictionary, with description of each function's outcome.

    Notes
    -----
    Can call any methods from `CalculateParams`.
    """
    return self._process_scaffold(
        funcs,
        dlc_fp=self.get_fp(Folders.DLC.value),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
    )

classify_behaviours(overwrite)

Given model config files in the BehavClassifier format, generates beahviour predidctions on the given extracted features dataframe.

Parameters:

Name Type Description Default
overwrite bool

Whether to overwrite the output file (if it exists).

required

Returns:

Type Description
dict

Diagnostics dictionary, with description of each function's outcome.

Source code in behavysis_pipeline/pipeline/experiment.py
def classify_behaviours(self, overwrite: bool) -> dict:
    """
    Given model config files in the BehavClassifier format, generates beahviour predidctions
    on the given extracted features dataframe.

    Parameters
    ----------
    overwrite : bool
        Whether to overwrite the output file (if it exists).

    Returns
    -------
    dict
        Diagnostics dictionary, with description of each function's outcome.
    """
    return self._process_scaffold(
        (ClassifyBehaviours.classify_behaviours,),
        features_fp=self.get_fp(Folders.FEATURES_EXTRACTED.value),
        out_fp=self.get_fp(Folders.PREDICTED_BEHAVS.value),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
        overwrite=overwrite,
    )

evaluate(funcs, overwrite)

Evaluating preprocessed DLC data and scored_behavs data.

Parameters:

Name Type Description Default
funcs _type_

description

required
overwrite bool

Whether to overwrite the output file (if it exists).

required

Returns:

Type Description
dict

Diagnostics dictionary, with description of each function's outcome.

Source code in behavysis_pipeline/pipeline/experiment.py
def evaluate(self, funcs, overwrite: bool) -> dict:
    """
    Evaluating preprocessed DLC data and scored_behavs data.

    Parameters
    ----------
    funcs : _type_
        _description_
    overwrite : bool
        Whether to overwrite the output file (if it exists).

    Returns
    -------
    dict
        Diagnostics dictionary, with description of each function's outcome.
    """
    return self._process_scaffold(
        funcs,
        vid_fp=self.get_fp(Folders.FORMATTED_VID.value),
        dlc_fp=self.get_fp(Folders.PREPROCESSED.value),
        behavs_fp=self.get_fp(Folders.SCORED_BEHAVS.value),
        out_dir=os.path.join(self.root_dir, EVALUATE_DIR),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
        overwrite=overwrite,
    )

export_behaviours(overwrite)

summary

Parameters:

Name Type Description Default
overwrite bool

description

required

Returns:

Type Description
dict

description

Source code in behavysis_pipeline/pipeline/experiment.py
def export_behaviours(self, overwrite: bool) -> dict:
    """
    _summary_

    Parameters
    ----------
    overwrite : bool
        _description_

    Returns
    -------
    dict
        _description_
    """
    # Exporting 6_predicted_behavs df to 7_scored_behavs folder
    return self._process_scaffold(
        (Export.predbehav_2_scoredbehav,),
        src_fp=self.get_fp(Folders.PREDICTED_BEHAVS.value),
        out_fp=self.get_fp(Folders.SCORED_BEHAVS.value),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
        overwrite=overwrite,
    )

export_feather(in_dir, out_dir, overwrite)

summary

Parameters:

Name Type Description Default
in_dir str

description

required
out_dir str

description

required

Returns:

Type Description
dict

description

Source code in behavysis_pipeline/pipeline/experiment.py
def export_feather(self, in_dir: str, out_dir: str, overwrite: bool) -> dict:
    """
    _summary_

    Parameters
    ----------
    in_dir : str
        _description_
    out_dir : str
        _description_

    Returns
    -------
    dict
        _description_
    """
    return self._process_scaffold(
        (Export.feather_2_csv,),
        in_fp=self.get_fp(in_dir),
        out_fp=os.path.join(out_dir, f"{self.name}.csv"),
        overwrite=overwrite,
    )

extract_features(overwrite)

Extracts features from the preprocessed dlc file to generate many more features. This dataframe of derived features will be input for a ML classifier to detect particularly trained behaviours.

Parameters:

Name Type Description Default
overwrite bool

Whether to overwrite the output file (if it exists).

required

Returns:

Type Description
dict

Diagnostics dictionary, with description of each function's outcome.

Source code in behavysis_pipeline/pipeline/experiment.py
def extract_features(self, overwrite: bool) -> dict:
    """
    Extracts features from the preprocessed dlc file to generate many more features.
    This dataframe of derived features will be input for a ML classifier to detect
    particularly trained behaviours.

    Parameters
    ----------
    overwrite : bool
        Whether to overwrite the output file (if it exists).

    Returns
    -------
    dict
        Diagnostics dictionary, with description of each function's outcome.
    """
    return self._process_scaffold(
        (ExtractFeatures.extract_features,),
        dlc_fp=self.get_fp(Folders.PREPROCESSED.value),
        out_fp=self.get_fp(Folders.FEATURES_EXTRACTED.value),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
        temp_dir=os.path.join(self.root_dir, TEMP_DIR),
        overwrite=overwrite,
    )

format_vid(funcs, overwrite)

Formats the video with ffmpeg to fit the formatted configs (e.g. fps and resolution_px). Once the formatted video is produced, the configs dict and *configs.json file are updated with the formatted video's metadata.

Parameters:

Name Type Description Default
funcs tuple[Callable, ...]

description

required
overwrite bool

Whether to overwrite the output file (if it exists).

required

Returns:

Type Description
dict

Diagnostics dictionary, with description of each function's outcome.

Notes

Can call any methods from FormatVid.

Source code in behavysis_pipeline/pipeline/experiment.py
def format_vid(self, funcs: tuple[Callable, ...], overwrite: bool) -> dict:
    """
    Formats the video with ffmpeg to fit the formatted configs (e.g. fps and resolution_px).
    Once the formatted video is produced, the configs dict and *configs.json file are
    updated with the formatted video's metadata.

    Parameters
    ----------
    funcs : tuple[Callable, ...]
        _description_
    overwrite : bool
        Whether to overwrite the output file (if it exists).

    Returns
    -------
    dict
        Diagnostics dictionary, with description of each function's outcome.

    Notes
    -----
    Can call any methods from `FormatVid`.
    """
    return self._process_scaffold(
        funcs,
        in_fp=self.get_fp(Folders.RAW_VID.value),
        out_fp=self.get_fp(Folders.FORMATTED_VID.value),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
        overwrite=overwrite,
    )

get_fp(folder_str)

Returns the experiment's file path from the given folder.

Parameters:

Name Type Description Default
folder_str str

The folder to return the experiment document's filepath for.

required

Returns:

Type Description
str

The experiment document's filepath.

Raises:

Type Description
ValueError

ValueError: Folder name is not valid. Refer to FOLDERS constant for valid folder names.

Source code in behavysis_pipeline/pipeline/experiment.py
def get_fp(self, folder_str: str) -> str:
    """
    Returns the experiment's file path from the given folder.

    Parameters
    ----------
    folder_str : str
        The folder to return the experiment document's filepath for.

    Returns
    -------
    str
        The experiment document's filepath.

    Raises
    ------
    ValueError
        ValueError: Folder name is not valid. Refer to FOLDERS constant for valid folder names.
    """
    # Getting folder enum from string
    folder = next((f for f in Folders if folder_str == f.value), None)
    # Assertion: The given folder name must be valid
    if not folder:
        raise ValueError(
            f'"{folder_str}" is not a valid experiment folder name.\n'
            + "Please only specify one of the following folders:\n"
            + "    - "
            + "\n    - ".join([f.value for f in Folders])
        )
    # Getting experiment filepath for given folder
    fp = os.path.join(
        self.root_dir, folder.value, f"{self.name}{FILE_EXTS[folder]}"
    )
    # Making a folder if it does not exist
    os.makedirs(os.path.split(fp)[0], exist_ok=True)
    # Returning filepath
    return fp

preprocess(funcs, overwrite)

A preprocessing pipeline method to convert raw DLC data into preprocessed DLC data that is ready for ML analysis. All functs passed in must have the format func(df, dict) -> df. Possible funcs are given in preprocessing.py The preprocessed data is saved to the project's preprocessed folder.

Parameters:

Name Type Description Default
funcs tuple[Callable, ...]

description

required
overwrite bool

Whether to overwrite the output file (if it exists).

required

Returns:

Type Description
dict

Diagnostics dictionary, with description of each function's outcome.

Notes

Can call any methods from Preprocess.

Source code in behavysis_pipeline/pipeline/experiment.py
def preprocess(self, funcs: tuple[Callable, ...], overwrite: bool) -> dict:
    """
    A preprocessing pipeline method to convert raw DLC data into preprocessed
    DLC data that is ready for ML analysis.
    All functs passed in must have the format func(df, dict) -> df. Possible funcs
    are given in preprocessing.py
    The preprocessed data is saved to the project's preprocessed folder.

    Parameters
    ----------
    funcs : tuple[Callable, ...]
        _description_
    overwrite : bool
        Whether to overwrite the output file (if it exists).

    Returns
    -------
    dict
        Diagnostics dictionary, with description of each function's outcome.

    Notes
    -----
    Can call any methods from `Preprocess`.
    """
    # Exporting 3_dlc df to 4_preprocessed folder
    dd = self._process_scaffold(
        (Export.feather_2_feather,),
        src_fp=self.get_fp(Folders.DLC.value),
        out_fp=self.get_fp(Folders.PREPROCESSED.value),
        overwrite=overwrite,
    )
    # If there is an error, OR warning (indicates not to ovewrite), then return early
    res = dd["feather_2_feather"]
    if res.startswith("ERROR") or res.startswith("WARNING"):
        return dd
    # Feeding through preprocessing functions
    return self._process_scaffold(
        funcs,
        in_fp=self.get_fp(Folders.PREPROCESSED.value),
        out_fp=self.get_fp(Folders.PREPROCESSED.value),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
        overwrite=True,
    )

run_dlc(gputouse, overwrite)

Run the DLC model on the formatted video to generate a DLC annotated video and DLC h5 file for all experiments.

Parameters:

Name Type Description Default
gputouse int

description

required
overwrite bool

Whether to overwrite the output file (if it exists).

required

Returns:

Type Description
dict

Diagnostics dictionary, with description of each function's outcome.

Notes

Can call any methods from RunDLC.

Source code in behavysis_pipeline/pipeline/experiment.py
def run_dlc(self, gputouse: int, overwrite: bool) -> dict:
    """
    Run the DLC model on the formatted video to generate a DLC annotated video
    and DLC h5 file for all experiments.

    Parameters
    ----------
    gputouse : int
        _description_
    overwrite : bool
        Whether to overwrite the output file (if it exists).

    Returns
    -------
    dict
        Diagnostics dictionary, with description of each function's outcome.

    Notes
    -----
    Can call any methods from `RunDLC`.
    """
    return self._process_scaffold(
        (RunDLC.ma_dlc_analyse_single,),
        in_fp=self.get_fp(Folders.FORMATTED_VID.value),
        out_fp=self.get_fp(Folders.DLC.value),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
        temp_dir=os.path.join(self.root_dir, TEMP_DIR),
        gputouse=gputouse,
        overwrite=overwrite,
    )

update_configs(default_configs_fp, overwrite)

Initialises the JSON config files with the given configurations in configs. It can be specified whether or not to overwrite existing configuration values.

Parameters:

Name Type Description Default
default_configs_fp str

The JSON configs filepath to add/overwrite to the experiment's current configs file.

required
overwrite ('set', 'reset')

Specifies how to overwrite existing configurations. If add, only parameters in configs not already in the config files are added. If set, all parameters in configs are set in the config files (overwriting). If reset, the config files are completely replaced by configs.

"set"

Returns:

Type Description
dict

Diagnostics dictionary, with description of each function's outcome.

Source code in behavysis_pipeline/pipeline/experiment.py
def update_configs(self, default_configs_fp: str, overwrite: str) -> dict:
    """
    Initialises the JSON config files with the given configurations in `configs`.
    It can be specified whether or not to overwrite existing configuration values.

    Parameters
    ----------
    default_configs_fp : str
        The JSON configs filepath to add/overwrite to the experiment's current configs file.
    overwrite : {"set", "reset"}
        Specifies how to overwrite existing configurations.
        If `add`, only parameters in `configs` not already in the config files are added.
        If `set`, all parameters in `configs` are set in the config files (overwriting).
        If `reset`, the config files are completely replaced by `configs`.

    Returns
    -------
    dict
        Diagnostics dictionary, with description of each function's outcome.
    """
    return self._process_scaffold(
        (UpdateConfigs.update_configs,),
        configs_fp=self.get_fp(Folders.CONFIGS.value),
        default_configs_fp=default_configs_fp,
        overwrite=overwrite,
    )