Skip to content
Snippets Groups Projects
Commit 01d501c6 authored by Kim Sontheimer's avatar Kim Sontheimer
Browse files

cleanup, removed checkpoints

parent b291d04c
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
import sys
print(sys.path)
#sys.path.insert(0, '/home/denker/Projects/toolboxes/py/python-neo')
#sys.path.insert(0, '/home/denker/Projects/toolboxes/py/elephant')
sys.path.insert(0, '/home/denker/Projects/scientific_data_ms_package/code')
import os
import numpy as np
import matplotlib.pyplot as plt
import quantities as pq
from neo import Block, Segment
from neo.io import BlackrockIO
from elephant.signal_processing import butter
from reachgraspio import reachgraspio
from neo_utils import add_epoch, cut_segment_by_epoch, get_events
# Specify the path to the recording session to load, eg, '/home/user/l101210-001'
session_name = '/home/denker/SciDataPubDatasets/DataNikos2/i140703-001'
odml_dir = '/home/denker/SciDataPubDatasets/MetaDataNikos2/odMLfiles'
```
%% Output
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-801d98b34a28> in <module>()
13 from neo import Block, Segment
14 from neo.io import BlackrockIO
---> 15 from elephant.signal_processing import butter
16
17 from reachgraspio import reachgraspio
ImportError: No module named 'elephant'
%% Cell type:markdown id: tags:
# Open the session for reading
%% Cell type:code id: tags:
``` python
#session = BlackrockIO(session_name)
session = reachgraspio.ReachGraspIO(session_name, odml_directory=odml_dir)
```
%% Cell type:markdown id: tags:
Read the first 300s of data (time series at 1000Hz (ns2) and 30kHz (ns6)
scaled to units of voltage, sorted spike trains, spike waveforms and events)
from electrode 62 of the recording session and return it as a Neo Block. The
time shift of the ns2 signal (LFP) induced by the online filter is
automatically corrected for by a heuristic factor stored in the metadata
(correct_filter_shifts=True).
%% Cell type:code id: tags:
``` python
data_block = session.read_block(
nsx_to_load='all',
n_starts=None, n_stops=300 * pq.s,
channels=[62], units='all',
load_events=True, load_waveforms=True, scaling='voltage')
# Access the single Segment of the data block, reaching up to 300s.
assert len(data_block.segments) == 1
data_segment = data_block.segments[0]
```
%% Cell type:code id: tags:
``` python
print "Block: ", data_block.name
print data_block.annotations
print
print "Segment: ", data_segment.name
print data_segment.annotations
print
print "A Spiketrain: "
print data_segment.spiketrains[0][0:10]
print data_segment.spiketrains[0][0:10].rescale(pq.ms)
print data_segment.spiketrains[0][0:10].magnitude
print data_segment.spiketrains[0].t_start
print data_segment.spiketrains[0].t_stop
print data_segment.spiketrains[0].annotations
print
print "An AnalogSignal: "
print data_segment.analogsignals[0].times[0:10]
print data_segment.analogsignals[0][0:10]
print data_segment.analogsignals[0].annotations
print
print "An Event: "
print data_segment.events[0].times[0:10]
print data_segment.events[0].labels[0:10]
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]
print
```
%% Output
Block: Reachgrasp Recording Data Block
{'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}
Segment: Segment 0
{'condition': 1}
A Spiketrain:
[ 16471. 21599. 58494. 65345. 76005. 90368. 110354. 112677.
112969. 113317.] (1.0/30000 * s)
[ 549.03333333 719.96666667 1949.8 2178.16666667 2533.5
3012.26666667 3678.46666667 3755.9 3765.63333333 3777.23333333] ms
[ 16471. 21599. 58494. 65345. 76005. 90368. 110354. 112677.
112969. 113317.]
0.0 (1.0/30000 * s)
9000000.0 (1.0/30000 * s)
{'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}
An AnalogSignal:
[-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)
[[ 42.25]
[ 5.5 ]
[ -5.25]
[ 2. ]
[ 3.5 ]
[ -4.5 ]
[ -6.25]
[ -2. ]
[ -4. ]
[ -8.5 ]] uV
{'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}
An Event:
[ 39424. 51491. 63532. 72614. 102672. 108766. 136071. 144917.
144918. 213943.] (1.0/30000 * s)
['65296' '65360' '65370' '65360' '65366' '65382' '65514' '65386' '65312'
'65296']
[[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]]
%% Cell type:markdown id: tags:
# Construct analysis epochs
In this step we extract and cut the data into time segments (termed analysis
epochs) that we wish to analyze. We contrast these analysis epochs to the
behavioral trials that are defined by the experiment as occurrence of a Trial
Start (TS-ON) event in the experiment. Concretely, here our analysis epochs
are constructed as a cutout of 25ms of data around the TS-ON event of all
successful behavioral trials.
Get Trial Start (TS-ON) events of all successful behavioral trials
(corresponds to performance code 255, which is accessed for convenience and
better legibility in the dictionary attribute performance_codes of the
ReachGraspIO class).
To this end, we filter all event objects of the loaded data to match the name
"TrialEvents", which is the Event object containing all Events available (see
documentation of ReachGraspIO). From this Event object we extract only events
matching "TS-ON" and the desired trial performance code (which are
annotations of the Event object).
%% Cell type:code id: tags:
``` python
start_events = get_events(
data_segment,
properties={
'name': 'TrialEvents',
'trial_event_labels': 'TS-ON',
'performance_in_trial': session.performance_codes['correct_trial']})
# Extract single Neo Event object containing all TS-ON triggers
assert len(start_events) == 1
start_event = start_events[0]
```
%% Cell type:markdown id: tags:
Construct analysis epochs from 10ms before the TS-ON of a successful
behavioral trial to 15ms after TS-ON. The name "analysis_epochs" is given to
the resulting Neo Epoch object. The object is not attached to the Neo
Segment. The parameter event2 of add_epoch() is left empty, since we are
cutting around a single event, as opposed to cutting between two events.
%% Cell type:code id: tags:
``` python
pre = -10 * pq.ms
post = 15 * pq.ms
epoch = add_epoch(
data_segment,
event1=start_event, event2=None,
pre=pre, post=post,
attach_result=False,
name='analysis_epochs')
```
%% Cell type:markdown id: tags:
Create new segments of data cut according to the analysis epochs of the
'analysis_epochs' Neo Epoch object. The time axes of all segments are aligned
such that each segment starts at time 0 (parameter reset_times); annotations
describing the analysis epoch are carried over to the segments. A new Neo
Block named "data_cut_to_analysis_epochs" is created to capture all cut
analysis epochs.
%% Cell type:code id: tags:
``` python
cut_trial_block = Block(name="data_cut_to_analysis_epochs")
cut_trial_block.segments = cut_segment_by_epoch(
data_segment, epoch, reset_time=True)
```
%% Cell type:markdown id: tags:
# Plot data
Determine the first existing trial ID i from the Event object containing all
start events. Then, by calling the filter() function of the Neo Block
"data_cut_to_analysis_epochs" containing the data cut into the analysis
epochs, we ask to return all Segments annotated by the behavioral trial ID i.
In this case this call should return one matching analysis epoch around TS-ON
belonging to behavioral trial ID i. For monkey N, this is trial ID 1, for
monkey L this is trial ID 2 since trial ID 1 is not a correct trial.
%% Cell type:code id: tags:
``` python
trial_id = int(np.min(start_event.annotations['trial_id']))
trial_segments = cut_trial_block.filter(
targdict={"trial_id": trial_id}, objects=Segment)
assert len(trial_segments) == 1
trial_segment = trial_segments[0]
```
%% Cell type:markdown id: tags:
Create figure
%% Cell type:code id: tags:
``` python
fig = plt.figure(facecolor='w')
time_unit = pq.CompoundUnit('1./30000*s')
amplitude_unit = pq.microvolt
nsx_colors = ['b', 'k', 'r']
```
%% Cell type:markdown id: tags:
Loop through all analog signals and plot the signal in a color corresponding
to its sampling frequency (i.e., originating from the ns2/ns5 or ns2/ns6).
%% Cell type:code id: tags:
``` python
for i, anasig in enumerate(trial_segment.analogsignals):
plt.plot(
anasig.times.rescale(time_unit),
anasig.rescale(amplitude_unit),
label=anasig.name,
color=nsx_colors[i])
```
%% Cell type:markdown id: tags:
Loop through all spike trains and plot the spike time, and overlapping the
wave form of the spike used for spike sorting stored separately in the nev
file.
%% Cell type:code id: tags:
``` python
for st in trial_segment.spiketrains:
color = np.random.rand(3,)
for spike_id, spike in enumerate(st):
# Plot spike times
plt.axvline(
spike.rescale(time_unit).magnitude,
color=color,
label='Unit ID %i' % st.annotations['unit_id'])
# Plot waveforms
waveform = st.waveforms[spike_id, 0, :]
waveform_times = np.arange(len(waveform))*time_unit + spike
plt.plot(
waveform_times.rescale(time_unit).magnitude,
waveform.rescale(amplitude_unit),
'--',
linewidth=2,
color=color,
zorder=0)
```
%% Cell type:markdown id: tags:
Loop through all events
%% Cell type:code id: tags:
``` python
for event in trial_segment.events:
if event.name == 'TrialEvents':
for ev_id, ev in enumerate(event):
plt.axvline(
ev,
alpha=0.2,
linewidth=3,
linestyle='dashed',
label='event ' + event.annotations[
'trial_event_labels'][ev_id])
```
%% Cell type:markdown id: tags:
Finishing touches on the plot
%% Cell type:code id: tags:
``` python
plt.autoscale(enable=True, axis='x', tight=True)
plt.xlabel(time_unit.name)
plt.ylabel(amplitude_unit.name)
plt.legend(loc=4, fontsize=10)
plt.show()
```
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment