cortex-lab / phy

phy: interactive visualization and manual spike sorting of large-scale ephys data
BSD 3-Clause "New" or "Revised" License
304 stars 155 forks source link

Programmatically save cluster_info.tsv #1241

Open chris-angeloni opened 6 months ago

chris-angeloni commented 6 months ago

Is there a command to export the cluster info of a sorted recording to a .tsv file without opening the GUI and pressing ctrl-S?

abisi commented 2 months ago

Hello, did you find a way by chance?

chris-angeloni commented 2 months ago

Unfortunately no. Kilosort does create a "cluster_group.tsv" file though, so I load that and cross-reference it to the cluster ids in spike_clusters.npy to create a new "cluster_info.tsv" file. Unfortunately it does not have all of the useful information from phy though (depth, channel, etc), I compute that on my own and add it

chris-angeloni commented 2 months ago
import numpy as np
import pandas as pd
import os

def check_cluster_groups_file(dp):
    clust_ids = get_cluster_ids(dp)
    group_file = os.path.join(dp, 'cluster_group.tsv')
    if os.path.exists(group_file):
        group_info = pd.read_csv(group_file, sep='\t', header=0)
        if 'group' not in group_info.columns:
            # data wasnt curated, rewrite group file
            print(f'data not curated... rewriting {group_file}')
            clust_info = pd.DataFrame()
            clust_info['cluster_id'] = clust_ids
            fixed_ks_labels = [group_info.KSLabel[c] for c in clust_info['cluster_id'] if c in group_info.KSLabel]
            clust_info['group'] = ["unsorted"] * len(clust_ids)
            clust_info['KSLabel'] = fixed_ks_labels
            clust_info.to_csv(os.path.join(dp, "cluster_group.tsv"), sep="\t", index=False)
            return clust_info
        else:
            return group_info

def get_cluster_ids(dp):
    clust = np.load(os.path.join(dp, 'spike_clusters.npy'))
    return np.unique(clust)

My code uses the contents of the rewritten cluster_group file, but you can change the line that saves it to "cluster_info.tsv"

abisi commented 2 months ago

Hi! No worries, thanks a lot for your reply. 😄 In the end I was too lazy to recreate a cluster_info myself so I run a subprocess that opens the Phy GUI, saves and quits. 😅 The following works for me, using the pyautogui package:

# Execute Phy to generate cluster_info table
command = 'conda activate phy2 && phy template-gui params.py && conda deactivate'
process = subprocess.Popen(command,  shell=True, cwd=os.path.join(probe_path, 'kilosort2'))
time.sleep(15) # wait for GUI to load
pyautogui.hotkey('ctrl', 's') # simulate CTRL+S key press to save the file
time.sleep(15) # wait for saving to complete
pyautogui.hotkey('ctrl', 'q') # close GUI
time.sleep(15)
process.terminate() # terminate process

The sleeping times are necessary it seems but could be shortened, potentially.

Have an excellent day, Axel