Skip to content

artifact.py

ArtifactDetectionSelection

Bases: SpyglassMixin, Manual

Source code in src/spyglass/spikesorting/v1/artifact.py
@schema
class ArtifactDetectionSelection(SpyglassMixin, dj.Manual):
    definition = """
    # Processed recording and artifact detection parameters. Use `insert_selection` method to insert new rows.
    artifact_id: uuid
    ---
    -> SpikeSortingRecording
    -> ArtifactDetectionParameters
    """

    @classmethod
    def insert_selection(cls, key: dict):
        """Insert a row into ArtifactDetectionSelection with an
        automatically generated unique artifact ID as the sole primary key.

        Parameters
        ----------
        key : dict
            primary key of SpikeSortingRecording and ArtifactDetectionParameters

        Returns
        -------
        artifact_id : str
            the unique artifact ID serving as primary key for ArtifactDetectionSelection
        """
        query = cls & key
        if query:
            logger.warn("Similar row(s) already inserted.")
            return query.fetch(as_dict=True)
        key["artifact_id"] = uuid.uuid4()
        cls.insert1(key, skip_duplicates=True)
        return key

insert_selection(key) classmethod

Insert a row into ArtifactDetectionSelection with an automatically generated unique artifact ID as the sole primary key.

Parameters:

Name Type Description Default
key dict

primary key of SpikeSortingRecording and ArtifactDetectionParameters

required

Returns:

Name Type Description
artifact_id str

the unique artifact ID serving as primary key for ArtifactDetectionSelection

Source code in src/spyglass/spikesorting/v1/artifact.py
@classmethod
def insert_selection(cls, key: dict):
    """Insert a row into ArtifactDetectionSelection with an
    automatically generated unique artifact ID as the sole primary key.

    Parameters
    ----------
    key : dict
        primary key of SpikeSortingRecording and ArtifactDetectionParameters

    Returns
    -------
    artifact_id : str
        the unique artifact ID serving as primary key for ArtifactDetectionSelection
    """
    query = cls & key
    if query:
        logger.warn("Similar row(s) already inserted.")
        return query.fetch(as_dict=True)
    key["artifact_id"] = uuid.uuid4()
    cls.insert1(key, skip_duplicates=True)
    return key

merge_intervals(intervals)

Takes a list of intervals each of which is [start_time, stop_time] and takes union over intervals that are intersecting

Parameters:

Name Type Description Default
intervals _type_

description

required

Returns:

Type Description
_type_

description

Source code in src/spyglass/spikesorting/v1/artifact.py
def merge_intervals(intervals):
    """Takes a list of intervals each of which is [start_time, stop_time]
    and takes union over intervals that are intersecting

    Parameters
    ----------
    intervals : _type_
        _description_

    Returns
    -------
    _type_
        _description_
    """
    if len(intervals) == 0:
        return []

    # Sort the intervals based on their start times
    intervals.sort(key=lambda x: x[0])

    merged = [intervals[0]]

    for i in range(1, len(intervals)):
        current_start, current_stop = intervals[i]
        last_merged_start, last_merged_stop = merged[-1]

        if current_start <= last_merged_stop:
            # Overlapping intervals, merge them
            merged[-1] = [
                last_merged_start,
                max(last_merged_stop, current_stop),
            ]
        else:
            # Non-overlapping intervals, add the current one to the list
            merged.append([current_start, current_stop])

    return np.asarray(merged)