Package: blechpy
See thefull documentationhere.blechpyInstallationUsageDatasetsStarting wit a raw datasetCreate datasetInitialize ParametersBasic ProcessingViewing a DatasetLoading an existing datasetImport processed dataset into dataset frameworkExperimentsCreating an experimentEditing recordingsHeld unit detectionTable of contents generated with markdown-tocblechpyThis is a package to extract, process and analyze electrophysiology data recorded with Intan or OpenEphys recording systems. This package is customized to store experiment and analysis metadata for the BLECh Lab (Katz lab) @ Brandeis University, but can readily be used and customized for other labs.RequirementsOperating system:Currently, blechpy is only developed and validated to work properly on Linux operating systems. It is possible to use blechpy on Mac, but some GUI features may not work properly. It is not possible to use blechpy on Windows.Virtual environmentsBecause blechpy depends on a very specific mix of package versions, it is required to install blechpy in a virtual environment.We highly recommend using miniconda to handle your virtual environments. You can download miniconda here:https://docs.conda.io/en/latest/miniconda.htmlHardwareWe recommend using a computer with at least 32gb of ram and a muti-core processor. The more cores and memory, the better. Memory usage scales with core usage--memory needed to run without overflow errors as you increase the number of cores used. It is possible to run memory-intensive functions with fewer cores to avoid overflow errors, but this will increase processing time. It is also possible to re-run memory-intensive functions after an overflow error, and the function will pick up where it left off.DataRight now this pipeline is only compatible with recordings done with Intan's 'one file per channel' or 'one file per signal type' recordings settings.Installation + maintainanceInstallationCreate a miniconda environment with:condacreate-nblechpypython==3.7.13
condaactivateblechpyNow you can install the package with pip:pipinstallblechpyActivationOnce you have installed blechpy, you will need to perform some steps to "activate" blechpy, whenever you want to use it.open up a bash terminal (control+alt+t on ubuntu)activate your miniconda environment with the following command:condaactivateblechpystart an ipython console by typing into the terminalipythonYou will now be in an ipython console. Now, import blechpy. Simply type:importblechpyNow, you can use blechpy functions in your ipython console.UpdatingTo update blechpy, open up a bash terminal and type:condaactivateblechpy#activate your blechpy virtual environmentpipinstallblechpy-U#install updated version of blechpyTroubleshooting Segmentation Fault: Only applies if you are using Ubuntu version 20.XX LTSIf your operating system is Ubuntu version 20.XX LTS, "import blechpy" may throw a "segmentation fault" error. This is because numba version 0.48 available via pip-install is corrupted. You can fix this issue by reinstalling numba via conda, by entering the following command in your bash terminal:condainstallnumba=0.48.0Blechpy Overviewblechpy handles experimental metadata using data_objects which are tied to a directory encompassing some level of data. Existing types of data_objects include:datasetobject for a single recording sessionto create a dataset, you will need to have recording files from a single recording in its own "dataset folder". The path to this folder is the "recording directory"The dataset processing pipleine creates 2 critical files that will live alongside your recording files in the dataset folder: the .h5 file and the .p file. The .h5 file contains the actual processed data, along with some metadata. The .p file contains additional critical metadata.code lives in blechpy/datastructures/dataset.pyexperimentobject encompasing an ordered set of recordings from a single animalindividual recordings must first be processed as datasetsto create an experiment, you will need to have all the dataset folders from a single animal in its own "experiment folder". The path to this folder is the "experiment directory"code lives in blechpy/datastructures/experiment.pyprojectobject that can encompass multiple experiments & data groups and allow analysis or group differencesto create a project, you will need to have all the experiment folders from a single project in its own "project folder". The path to this folder is the "project directory"code lives in blechpy/datastructures/project.pyHMMHandlerobject that can be used to set up and run hidden markov model analysis on a datasetHMMHandler objects are created on the level of the dataset. You will need to have a fully processed dataset to create an HMMHandler object from it.Dataset Processing (start here if you have a raw recording)Basic dataset processing pipeline:With a brand newshinydataset, the most basic recommended data extraction workflow would be:dat=blechpy.dataset('/path/to/data/dir/')#create dataset object. Path to data dir should be your recording directory# IMPORTANT: only run blechpy.dataset ONCE on a dataset, unless you want to overwrite the existing dataset and your preprocessing# to load an existing dataset, use dat = blechpy.load_dataset('/path/to/data/dir/') insteaddat.initParams(data_quality='hp')# follow GUI prompts.dat.extract_data()# Extracts raw data into HDF5 storedat.create_trial_list()# Creates table of digital input triggersdat.mark_dead_channels()# View traces and label electrodes as dead, or just pass list of dead channelsdat.common_average_reference()# Use common average referencing on data. Repalces raw with referenced data in HDF5 storedat.detect_spikes()# Detect spikes in data. Replaces raw data with spike data in HDF5 storedat.blech_clust_run(umap=True)# Cluster data using GMMdat.sort_spikes(electrode_number)# Split, merge and label clusters as units. Follow GUI prompts. Perform this for every electrodedat.post_sorting()#run this after you finish sorting all electrodesdat.make_PSTH_plots()#optional: make PSTH plots for all unitsdat.make_raster_plots()#optional: make raster plots for all unitstroubleshooting common error:It is common to get the following error after running the functionsdat.detect_spikes()ordat.blech_clust_run():TerminatedWorkerError:Aworkerprocessmanagedbytheexecutorwasunexpectedlyterminated.ThiscouldbecausedbyasegmentationfaultwhilecallingthefunctionorbyanexcessivememoryusagecausingtheOperatingSystemtokilltheworker.If you encounter this error, simply re-run the function which caused the error, and it will pick-up where it left off.Occasionally, you may have to do this several times before the function completes.The reason for this error is that these functions multi-process across channels, but underlying libraries like scipy also parallelize their own operations. This makes it impossible for the program to know how much memory will be used to automatically constrain the number of multi-processes used.Explainers and useful substitutions:blechpy.dataset(): make a NEW datasetblechpy.dataset() makes a NEW dataset or to OVERWRITES an existing dataset. DO NOT use it on an existing dataset unless you want to overwrite the existing dataset and lose you preprocessing progress with it.dat=blechpy.dataset('path/to/recording/directory')# replace quoted text with the filepath to the folder where your recording files are# ordat=blechpy.dataset()# for user interface to select directoryThis will create a new dataset object and setup basic file paths. You should only do this when starting data processing for the first time. If you use it on a processed dataset, it will get overwritten.blechpy.load_dataset(): LOAD an existing datasetIf you already have a dataset and want to pick up where you left off, use blechpy.load_dataset() instead of blechpy.dataset().dat=blechpy.load_dataset('/path/to/recording/directory')# create dataset object# ordat=blechpy.load_dataset()# for user interface to select directory# ordat=blechpy.load_dataset('path/to/dataset/save/file.p')initParams(): initialize parametersdat.initParams()Initalizes all analysis parameters with a series of prompts.
See prompts for optional keyword params.
Primarily setups parameters for:Flattening Port & Channel in Electrode designationsCommon average referencingLabelling areas of electrodesLabelling digital inputs & outputsLabelling dead electrodesClustering parametersSpike array creationPSTH creationPalatability/Identity Responsiveness calculationsInitial parameters are pulled from default json files in the dio subpackage.
Parameters for a dataset are written to json files in aparametersfolder in the recording directory.useful presets:dat.initParams(data_quality='noisy')# alternative: less strict clustering parametersdat.initParams(car_keyword='2site_OE64')# automatically map channels to hirose-connector 64ch OEPS EIB in 2-site implantationdat.initParams(car_keyword='bilateral64')# automatically map channels to omnetics-connector 64ch EIB in 2-site implantationdat.initParams(shell=True)# alternative: bypass GUI interface in favor of shell interface, useful if working over SSH or GUI is broken#remember that you can chain any combination of valid keyword arguments together, eg.:dat.initParams(data_quality='hp',car_keyword='bilateral64',shell=True)mark_dead_channels(): mark dead channels for exclusion from common average refrencing and clusteringdat.mark_dead_channels()# opens GUI to view traces and label dead channelsMarking dead channels is critical for good common average referencing, since dead channels typically have a signal that differs a lot from the "true" average voltage at the electrode tips.HIGHLY RECOMMENDED preset:If you already know your dead channels a-priori, you can pass them to mark_dead_channels() as a list of integers:dat.mark_dead_channels([deadchannelindices])# dead channel indices eg. : [1,2,3]blech_clust_run(): run clusteringblech_clust_run's keywords can change the clustering algorithm and/or parametersdat.blech_clust_run(data_quality='noisy')# alternative: re-run clustering with less strict parametersdat.blech_clust_run(umap=True)# alternative: cluster with UMAP instead of GMM, improves clusteringdat.blech_clust_run()# default uses PCA instead of UMAP, which is faster, but lower quality clusteringOther useful functions:dat._change_root() for moving a dataset:If you want to move a dataset folder, it is critical you perform the following steps:move the dataset folder to the desired locationcopy the new dataset folder directory (right click on folder, select copy)in the ipython console, run the following commands:new_directory='path/to/new/dataset/folder'# You can paste the directory by right clicking and selecting 'paste filename'dat=blechpy.load_dataset(new_directory)# load the datasetdat._change_root(new_directory)# change the root directory of the dataset to the new directorydat.save()# save the new directory to the dataset fileChecking processing progress:dat.processing_statusCan provide an overview of basic data extraction and processing steps that need to be taken.Viewing a DatasetExperiments can be easily viewed wih:print(dat)A summary can also be exported to a text with:dat.export_to_text()Import processed dataset into dataset framework (in development)dat=blechpy.port_in_dataset()# ordat=blechpy.port_in_dataset('/path/to/recording/directory')ExperimentsCreating an experimentexp=blechpy.experiment('/path/to/dir/encasing/recordings')# orexp=blechpy.experiment()This will initalize an experiment with all recording folders within the chosen directory.Editing recordingsexp.add_recording('/path/to/new/recording/dir/')# Add recordingexp.remove_recording('rec_label')# remove a recording dirRecordings are assigned labels when added to the experiment that can be used to easily reference exerpiments.Held unit detectionexp.detect_held_units()Uses raw waveforms from sorted units to determine if units can be confidently classified as "held". Results are stored in exp.held_units as a pandas DataFrame.
This also creates plots and exports data to a created directory:
/path/to/experiment/experiment-name_analysisAnalysisTheblechpy.analysismodule has a lot of useful tools for analyzing your data.
Most notable is theblechpy.analysis.poissonHMMmodule which will allow fitting of the HMM models to your data. See tutorials.