diff --git a/Documents/.ipynb_checkpoints/Working with a Neo object-checkpoint.ipynb b/Documents/.ipynb_checkpoints/Working with a Neo object-checkpoint.ipynb deleted file mode 100644 index cc6f0726621cf367dbb7ed24ab2ab341dcb48321..0000000000000000000000000000000000000000 --- a/Documents/.ipynb_checkpoints/Working with a Neo object-checkpoint.ipynb +++ /dev/null @@ -1,453 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "ename": "ImportError", - "evalue": "No module named 'elephant'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m<ipython-input-2-801d98b34a28>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mneo\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mBlock\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mSegment\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mneo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mio\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mBlackrockIO\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0melephant\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msignal_processing\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mbutter\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mreachgraspio\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mreachgraspio\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mImportError\u001b[0m: No module named 'elephant'" - ] - } - ], - "source": [ - "import sys\n", - "print(sys.path)\n", - "#sys.path.insert(0, '/home/denker/Projects/toolboxes/py/python-neo')\n", - "#sys.path.insert(0, '/home/denker/Projects/toolboxes/py/elephant')\n", - "sys.path.insert(0, '/home/denker/Projects/scientific_data_ms_package/code')\n", - "\n", - "import os\n", - "\n", - "import numpy as np\n", - "import matplotlib.pyplot as plt\n", - "\n", - "import quantities as pq\n", - "\n", - "from neo import Block, Segment\n", - "from neo.io import BlackrockIO\n", - "from elephant.signal_processing import butter\n", - "\n", - "from reachgraspio import reachgraspio\n", - "from neo_utils import add_epoch, cut_segment_by_epoch, get_events\n", - "\n", - "# Specify the path to the recording session to load, eg, '/home/user/l101210-001'\n", - "session_name = '/home/denker/SciDataPubDatasets/DataNikos2/i140703-001'\n", - "odml_dir = '/home/denker/SciDataPubDatasets/MetaDataNikos2/odMLfiles'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Open the session for reading" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "#session = BlackrockIO(session_name)\n", - "session = reachgraspio.ReachGraspIO(session_name, odml_directory=odml_dir)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Read the first 300s of data (time series at 1000Hz (ns2) and 30kHz (ns6)\n", - "scaled to units of voltage, sorted spike trains, spike waveforms and events)\n", - "from electrode 62 of the recording session and return it as a Neo Block. The\n", - "time shift of the ns2 signal (LFP) induced by the online filter is\n", - "automatically corrected for by a heuristic factor stored in the metadata\n", - "(correct_filter_shifts=True)." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "data_block = session.read_block(\n", - " nsx_to_load='all',\n", - " n_starts=None, n_stops=300 * pq.s,\n", - " channels=[62], units='all',\n", - " load_events=True, load_waveforms=True, scaling='voltage')\n", - "\n", - "# Access the single Segment of the data block, reaching up to 300s.\n", - "assert len(data_block.segments) == 1\n", - "data_segment = data_block.segments[0]" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Block: Reachgrasp Recording Data Block\n", - "{'arraygrid_col_num': 10, 'project_name': u'reach-to-grasp', 'arraygrids_tot_num': 1, 'avail_file_set': ['ccf', 'odml', 'nev', 'ns2', 'ns6'], 'subject_name': u'monkey_N', 'electrodes_pitch': array(400.0) * um, 'rec_pauses': False, 'subject_birthday': datetime.date(2008, 5, 17), 'project_type': u'electrophysiology', 'avail_nsx': [2, 6], 'avail_electrode_ids': [-1, 81, 83, 85, 88, 90, 92, 93, 96, -1, 79, 80, 84, 86, 87, 89, 91, 94, 63, 95, 77, 78, 82, 49, 53, 55, 57, 59, 61, 32, 75, 76, 45, 47, 51, 56, 58, 60, 64, 30, 73, 74, 41, 43, 44, 46, 52, 62, 31, 28, 71, 72, 39, 40, 42, 50, 54, 21, 29, 26, 69, 70, 37, 38, 48, 15, 19, 25, 27, 24, 67, 68, 35, 36, 5, 17, 13, 23, 20, 22, 65, 66, 33, 34, 7, 9, 11, 12, 16, 18, -1, 2, 1, 3, 4, 6, 8, 10, 14, -1], 'electrodes_tot_num': 100, 'setup_location': u'Inst. de Neurosciences de la Timone (INT), UMR 7289, CNRS - Aix Marseille Univ., Marseille, France', 'project_subtype': u'motor behavior', 'taskdesigns': [u'TwoCues'], 'array_serialnum': u'8596-001139', 'avail_nev': True, 'nb_segments': 1, 'subject_activehand': u'left', 'avail_ccf': True, 'connector_type': u'CerePort', 'arraygrid_row_num': 10, 'conditions': [1], 'subject_gender': u'male', 'avail_sif': False}\n", - "\n", - "Segment: Segment 0\n", - "{'condition': 1}\n", - "\n", - "A Spiketrain: \n", - "[ 16471. 21599. 58494. 65345. 76005. 90368. 110354. 112677.\n", - " 112969. 113317.] (1.0/30000 * s)\n", - "[ 549.03333333 719.96666667 1949.8 2178.16666667 2533.5\n", - " 3012.26666667 3678.46666667 3755.9 3765.63333333 3777.23333333] ms\n", - "[ 16471. 21599. 58494. 65345. 76005. 90368. 110354. 112677.\n", - " 112969. 113317.]\n", - "0.0 (1.0/30000 * s)\n", - "9000000.0 (1.0/30000 * s)\n", - "{'channel_id': 62, 'noise': True, 'electrode_reject_IFC': False, 'sua': False, 'electrode_reject_HFC': False, 'mua': False, 'connector_aligned_id': 48, 'electrode_reject_LFC': False, 'unit_id': 0}\n", - "\n", - "An AnalogSignal: \n", - "[-2.1 -1.1 -0.1 0.9 1.9 2.9 3.9 4.9 5.9 6.9] (1.0/1000.0*s)\n", - "[[ 42.25]\n", - " [ 5.5 ]\n", - " [ -5.25]\n", - " [ 2. ]\n", - " [ 3.5 ]\n", - " [ -4.5 ]\n", - " [ -6.25]\n", - " [ -2. ]\n", - " [ -4. ]\n", - " [ -8.5 ]] uV\n", - "{'filter_lo_pass_freq': array(250.0) * Hz, 'filter_shift_correction': array(2.1) * ms, 'electrode_reject_HFC': False, 'filter_lo_pass_order': 4, 'filter_hi_pass_freq': array(0.3) * Hz, 'connector_aligned_id': 48, 'electrode_reject_LFC': False, 'filter_type': u'butterworth', 'nsx': 2, 'channel_id': 62, 'electrode_reject_IFC': False, 'filter_hi_pass_order': 1}\n", - "\n", - "An Event: \n", - "[ 39424. 51491. 63532. 72614. 102672. 108766. 136071. 144917.\n", - " 144918. 213943.] (1.0/30000 * s)\n", - "['65296' '65360' '65370' '65360' '65366' '65382' '65514' '65386' '65312'\n", - " '65296']\n", - "[[False, False, False, False, False, False, False, False, False, False], ['TS-ON', 'WS-ON', 'CUE-ON', 'CUE-OFF', 'GO-ON', 'SR', 'RW-ON', 'RW-OFF', 'STOP', 'TS-ON'], [255, 255, 255, 255, 255, 255, 255, 255, 255, 255], [False, False, False, False, False, False, False, False, False, False], [True, True, True, True, True, True, True, True, True, False], [1, 1, 1, 1, 1, 1, 1, 1, 1, 2], ['SGHF', 'SGHF', 'SGHF', 'SGHF', 'SGHF', 'SGHF', 'SGHF', 'SGHF', 'SGHF', 'SGHF'], [39424, 39424, 39424, 39424, 39424, 39424, 39424, 39424, 39424, 213943]]\n", - "\n" - ] - } - ], - "source": [ - "print \"Block: \", data_block.name\n", - "print data_block.annotations\n", - "print\n", - "print \"Segment: \", data_segment.name\n", - "print data_segment.annotations\n", - "print\n", - "print \"A Spiketrain: \"\n", - "print data_segment.spiketrains[0][0:10]\n", - "print data_segment.spiketrains[0][0:10].rescale(pq.ms)\n", - "print data_segment.spiketrains[0][0:10].magnitude\n", - "print data_segment.spiketrains[0].t_start\n", - "print data_segment.spiketrains[0].t_stop\n", - "print data_segment.spiketrains[0].annotations\n", - "print\n", - "print \"An AnalogSignal: \"\n", - "print data_segment.analogsignals[0].times[0:10]\n", - "print data_segment.analogsignals[0][0:10]\n", - "print data_segment.analogsignals[0].annotations\n", - "print\n", - "print \"An Event: \"\n", - "print data_segment.events[0].times[0:10]\n", - "print data_segment.events[0].labels[0:10]\n", - "print [data_segment.events[0].annotations[x][0:10] if type(data_segment.events[0].annotations[x]) is list else data_segment.events[0].annotations[x] for x in data_segment.events[0].annotations]\n", - "print " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Construct analysis epochs\n", - "In this step we extract and cut the data into time segments (termed analysis\n", - "epochs) that we wish to analyze. We contrast these analysis epochs to the\n", - "behavioral trials that are defined by the experiment as occurrence of a Trial\n", - "Start (TS-ON) event in the experiment. Concretely, here our analysis epochs\n", - "are constructed as a cutout of 25ms of data around the TS-ON event of all\n", - "successful behavioral trials.\n", - "\n", - "Get Trial Start (TS-ON) events of all successful behavioral trials\n", - "(corresponds to performance code 255, which is accessed for convenience and\n", - "better legibility in the dictionary attribute performance_codes of the\n", - "ReachGraspIO class).\n", - "\n", - "To this end, we filter all event objects of the loaded data to match the name\n", - "\"TrialEvents\", which is the Event object containing all Events available (see\n", - "documentation of ReachGraspIO). From this Event object we extract only events\n", - "matching \"TS-ON\" and the desired trial performance code (which are\n", - "annotations of the Event object)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "start_events = get_events(\n", - " data_segment,\n", - " properties={\n", - " 'name': 'TrialEvents',\n", - " 'trial_event_labels': 'TS-ON',\n", - " 'performance_in_trial': session.performance_codes['correct_trial']})\n", - "\n", - "# Extract single Neo Event object containing all TS-ON triggers\n", - "assert len(start_events) == 1\n", - "start_event = start_events[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Construct analysis epochs from 10ms before the TS-ON of a successful\n", - "behavioral trial to 15ms after TS-ON. The name \"analysis_epochs\" is given to\n", - "the resulting Neo Epoch object. The object is not attached to the Neo\n", - "Segment. The parameter event2 of add_epoch() is left empty, since we are\n", - "cutting around a single event, as opposed to cutting between two events." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "pre = -10 * pq.ms\n", - "post = 15 * pq.ms\n", - "epoch = add_epoch(\n", - " data_segment,\n", - " event1=start_event, event2=None,\n", - " pre=pre, post=post,\n", - " attach_result=False,\n", - " name='analysis_epochs')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create new segments of data cut according to the analysis epochs of the\n", - "'analysis_epochs' Neo Epoch object. The time axes of all segments are aligned\n", - "such that each segment starts at time 0 (parameter reset_times); annotations\n", - "describing the analysis epoch are carried over to the segments. A new Neo\n", - "Block named \"data_cut_to_analysis_epochs\" is created to capture all cut\n", - "analysis epochs." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "cut_trial_block = Block(name=\"data_cut_to_analysis_epochs\")\n", - "cut_trial_block.segments = cut_segment_by_epoch(\n", - " data_segment, epoch, reset_time=True)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Plot data\n", - "\n", - "Determine the first existing trial ID i from the Event object containing all\n", - "start events. Then, by calling the filter() function of the Neo Block\n", - "\"data_cut_to_analysis_epochs\" containing the data cut into the analysis\n", - "epochs, we ask to return all Segments annotated by the behavioral trial ID i.\n", - "In this case this call should return one matching analysis epoch around TS-ON\n", - "belonging to behavioral trial ID i. For monkey N, this is trial ID 1, for\n", - "monkey L this is trial ID 2 since trial ID 1 is not a correct trial." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "trial_id = int(np.min(start_event.annotations['trial_id']))\n", - "trial_segments = cut_trial_block.filter(\n", - " targdict={\"trial_id\": trial_id}, objects=Segment)\n", - "assert len(trial_segments) == 1\n", - "trial_segment = trial_segments[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create figure" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "fig = plt.figure(facecolor='w')\n", - "time_unit = pq.CompoundUnit('1./30000*s')\n", - "amplitude_unit = pq.microvolt\n", - "nsx_colors = ['b', 'k', 'r']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Loop through all analog signals and plot the signal in a color corresponding\n", - "to its sampling frequency (i.e., originating from the ns2/ns5 or ns2/ns6)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "for i, anasig in enumerate(trial_segment.analogsignals):\n", - " plt.plot(\n", - " anasig.times.rescale(time_unit),\n", - " anasig.rescale(amplitude_unit),\n", - " label=anasig.name,\n", - " color=nsx_colors[i])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Loop through all spike trains and plot the spike time, and overlapping the\n", - "wave form of the spike used for spike sorting stored separately in the nev\n", - "file." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "for st in trial_segment.spiketrains:\n", - " color = np.random.rand(3,)\n", - " for spike_id, spike in enumerate(st):\n", - " # Plot spike times\n", - " plt.axvline(\n", - " spike.rescale(time_unit).magnitude,\n", - " color=color,\n", - " label='Unit ID %i' % st.annotations['unit_id'])\n", - " # Plot waveforms\n", - " waveform = st.waveforms[spike_id, 0, :]\n", - " waveform_times = np.arange(len(waveform))*time_unit + spike\n", - " plt.plot(\n", - " waveform_times.rescale(time_unit).magnitude,\n", - " waveform.rescale(amplitude_unit),\n", - " '--',\n", - " linewidth=2,\n", - " color=color,\n", - " zorder=0)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Loop through all events" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "for event in trial_segment.events:\n", - " if event.name == 'TrialEvents':\n", - " for ev_id, ev in enumerate(event):\n", - " plt.axvline(\n", - " ev,\n", - " alpha=0.2,\n", - " linewidth=3,\n", - " linestyle='dashed',\n", - " label='event ' + event.annotations[\n", - " 'trial_event_labels'][ev_id])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Finishing touches on the plot" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "plt.autoscale(enable=True, axis='x', tight=True)\n", - "plt.xlabel(time_unit.name)\n", - "plt.ylabel(amplitude_unit.name)\n", - "plt.legend(loc=4, fontsize=10)\n", - "plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.4.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}