HSUPipeline / convnwb

A module of helper code for converting data files to NWB.
https://hsupipeline.github.io/convnwb/
MIT License
0 stars 1 forks source link

[MNT] - Warning on scipy.stats.mode #11

Open TomDonoghue opened 1 year ago

TomDonoghue commented 1 year ago

As of scipy version 1.9.0, there is a new 'keepdims' argument in scipy.stats.mode that needs to be set for future-proofing, and will start to fail in scipy version 1.11.0: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mode.html

This relates to the use of scipy.stats.mode here: https://github.com/HSUPipeline/convnwb/blob/main/convnwb/timestamps/align.py#L168

For future scipy usage, this could be updated to: ixis_mode = stats.mode(ixis, keepdims=True)[0][0]

Or, equivalent updates: ixis_mode = stats.mode(ixis, keepdims=True).mode[0] ixis_mode = stats.mode(ixis, keepdims=False).mode

Basically - the fix is easy, but need to decide whether to enforce a specific version of scipy, and/or do a version check.

weijiazh97 commented 1 year ago

sync_behav_out, sync_neural_out = match_pulses(sync_behav, sync_neural, n_pulses=500, start_offset=100) Change start_offset number has a significant effect on the output

TomDonoghue commented 1 year ago

This is now fixed, by adding a version check, so both old and new scipy versions should work.

weijiazh97 commented 1 month ago

def match_sync_pulses(task, n_pulses, start_offset=None, tolerance=0.001): start_offset = None sync_behav = task.sync['process']['behave'] sync_neural = task.sync['process']['neural']

isi_sb = np.diff(sync_behav) / 1000  # Convert to seconds
isi_sn = np.diff(sync_neural)

# List to collect index offsets for matching ISIs
ixis = []

# Define a tolerance for matching ISIs
tolerance = 0.01  # Adjust based on acceptable tolerance

# Iterate through neural sync pulses and collect index offsets for matching ISIs
for ixn, isi in enumerate(isi_sn):
    # Use np.isclose to find if there are any close matches in isi_sb
    matches = np.isclose(isi_sb, isi, atol=tolerance)

    # Check if any matches are found
    if np.any(matches):
        # Get the index of the first match
        ixb = np.where(matches)[0][0]

        # Check if the next ISI exists and also matches
        if ixn + 1 < len(isi_sn) and ixb + 1 < len(isi_sb):
            if np.isclose(isi_sn[ixn + 1], isi_sb[ixb + 1], atol=tolerance):
                # If both current and next ISI match, record the index offset
                ixis.append(ixb - ixn)
                print(f"Match found at {ixn} with offset {ixb - ixn}")
        #else:
            # No next match or reached end of arrays
            #ixis.append(ixb - ixn)
ixis_mode = stats.mode(ixis)            #print(f"Match found at {ixn} with offset {ixb - ixn}")
print(ixis_mode)
if start_offset is not None:
        # Choose sync vector starting at chosen index rather than the beginning
        sync_behav_out = sync_behav[ixis_mode.mode + start_offset : ixis_mode.mode + start_offset + n_pulses-ixis_mode.mode]
        sync_neural_out = sync_neural[start_offset : start_offset + n_pulses-ixis_mode.mode]
else:
        # (STANDARD) Sync vector from beginning with offset accounted for
        sync_behav_out = sync_behav[ixis_mode.mode: ixis_mode.mode + n_pulses-ixis_mode.mode]
        sync_neural_out = sync_neural[0 : n_pulses-ixis_mode.mode]
return sync_behav_out,sync_neural_out
weijiazh97 commented 1 month ago

Convnwb version: 0.4.0-dev

Scipy version: 1.11.1