Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix trialization #130

Merged
merged 35 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
26949b4
simplify tokenize method for continuous stim
jihyunbak Feb 18, 2022
802ef40
update .gitignore
jihyunbak Feb 18, 2022
97eb3f5
rename internal variable mark_onsets to mark_events
jihyunbak Feb 18, 2022
4e8e6f1
add logging messages for debugging
jihyunbak Feb 18, 2022
59f9fe2
pass audio_play_length with stimulus metadata
jihyunbak Feb 18, 2022
a48a2e8
update inputs to _tokenize method
jihyunbak Feb 18, 2022
cfb6c4b
copy stimulus metadata into list_of_stimuli.yaml
jihyunbak Feb 19, 2022
1a74c8b
get stimulus metadata from list_of_stimuli.yaml
jihyunbak Feb 19, 2022
fbf69b8
reorder stimuli by alphabetical order
jihyunbak Feb 19, 2022
b87c361
fix stim metadata for trialization
jihyunbak Feb 22, 2022
5a20ec9
update trial tokenization
jihyunbak Feb 22, 2022
5da2527
update logging
jihyunbak Feb 22, 2022
a3ecc28
fix mark event detection
jihyunbak Feb 22, 2022
a4db2cb
fix mark detection for wn2 stimulus
jihyunbak Feb 22, 2022
1418ea5
remove old, ad hoc methods for wn tokenization
jihyunbak Feb 22, 2022
319d0fa
simplify _validate_num_stim_onsets
jihyunbak Feb 22, 2022
3cdc363
deprecate stim metadata mark_offset
jihyunbak Feb 22, 2022
891c9d9
remove wn2 stim metadata stim_values
jihyunbak Feb 22, 2022
6a10c03
load stim parameter values in TIMIT and Tone tokenizers
jihyunbak Feb 22, 2022
818fe01
pass mark time series directly to trials manager
jihyunbak Feb 22, 2022
d03f07b
clarify logics for mark/stimulus starting time
jihyunbak Feb 22, 2022
946d09f
minor fixes
jihyunbak Feb 22, 2022
c32bcd2
update dmr tokenization, reflecting the 60s back padding
jihyunbak Feb 22, 2022
1634bf7
drop unused stim metadata mark_sampling_rate
jihyunbak Feb 22, 2022
ef76e48
bring back mark_offset, fix baseline definitions back to previous beh…
jihyunbak Mar 2, 2022
910c737
move mark event detection to MarkManager
jihyunbak Mar 3, 2022
5772d59
change baseline periods for tone and wn
jihyunbak Mar 3, 2022
51604d8
add baseline_start value for timit and dmr tokenization
jihyunbak Mar 3, 2022
66ff9c3
introduce RecManager
jihyunbak Mar 3, 2022
7b99a7f
minor cleanup
jihyunbak Mar 3, 2022
3c7d83c
do add_stimulus to nwb in StimulusOriginator
jihyunbak Mar 3, 2022
36ced46
pass stimulus file paths with other stimulus metadata
jihyunbak Mar 3, 2022
a75bcc3
make stimulus utils module
jihyunbak Mar 3, 2022
af96ce2
fix mark detection after interactive check
jihyunbak Mar 3, 2022
ec63761
update docs
jihyunbak Mar 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ docs/source/gen_modules/*
*.log

# test output
_test
_test*

# space for users to save custom scripts
scripts/user/*
Expand Down
5 changes: 5 additions & 0 deletions docs/source/nsds_lab_to_nwb/common.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Common API
:undoc-members:
:show-inheritance:

.. automodule:: nsds_lab_to_nwb.common.rec_manager
:members:
:undoc-members:
:show-inheritance:

.. automodule:: nsds_lab_to_nwb.common.time
:members:
:undoc-members:
Expand Down
5 changes: 0 additions & 5 deletions docs/source/nsds_lab_to_nwb/components/stimulus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ components.stimulus API
:undoc-members:
:show-inheritance:

.. automodule:: nsds_lab_to_nwb.components.stimulus.stim_value_extractor
:members:
:undoc-members:
:show-inheritance:

.. automodule:: nsds_lab_to_nwb.components.stimulus.tokenizers.base_tokenizer
:members:
:undoc-members:
Expand Down
54 changes: 54 additions & 0 deletions nsds_lab_to_nwb/common/rec_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import logging

from nsds_lab_to_nwb.tools.htk.htk_reader import HTKReader
from nsds_lab_to_nwb.tools.htk.readers.htkfile import HTKFile
from nsds_lab_to_nwb.tools.tdt.tdt_reader import TDTReader

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class RecManager():
def __init__(self, dataset):
self.dataset = dataset

if hasattr(self.dataset, 'htk_mark_path'):
logger.info('Using HTK')
self.rec_source = 'htk'
self.rec_reader = HTKReader(self.dataset.htk_path)
else:
logger.info('Using TDT')
self.rec_source = 'tdt'
self.rec_reader = TDTReader(self.dataset.tdt_path)

def read_info(self):
if self.rec_source == 'htk':
return None

return self.rec_reader.tdt_obj['info']

def read_neural_data(self, stream, dev_conf):
data, metadata = self.rec_reader.get_data(stream=stream, dev_conf=dev_conf)
return data, metadata

def read_marks(self):
# Read the mark track
if self.rec_source == 'htk':
mark_file = HTKFile(self.dataset.htk_mark_path)
mark_track, meta = mark_file.read_data()
rate = mark_file.sample_rate
else:
mark_track, meta = self.rec_reader.get_data(stream='mrk1')
rate = meta['sample_rate']
return mark_track, rate

def read_mark_events(self):
if self.rec_source == 'htk':
return None

# for tdt
try:
return self.rec_reader.get_events()
except AttributeError:
# there is no mark for baseline (no stimulus) block
return None
15 changes: 3 additions & 12 deletions nsds_lab_to_nwb/components/neural_data/neural_data_originator.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
import logging.config
from pynwb.ecephys import ElectricalSeries

from nsds_lab_to_nwb.tools.htk.htk_reader import HTKReader
from nsds_lab_to_nwb.tools.tdt.tdt_reader import TDTReader
from process_nwb.resample import resample

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class NeuralDataOriginator():
def __init__(self, dataset, metadata, resample_flag=True):
self.dataset = dataset # this should have all relavant paths
def __init__(self, rec_manager, metadata, resample_flag=True):
self.rec_manager = rec_manager
self.metadata = metadata # this should have all relevant metadata
self.resample_flag = resample_flag
self.hardware_rate = None
self.resample_rate = None

if hasattr(self.dataset, 'htk_mark_path'):
logger.info('Using HTK')
self.neural_data_reader = HTKReader(self.dataset.htk_path)
else:
logger.info('Using TDT')
self.neural_data_reader = TDTReader(self.dataset.tdt_path)

def make(self, nwb_content, electrode_table_regions):
for device_name, dev_conf in self.metadata['device'].items():
if isinstance(dev_conf, str): # skip other annotations
continue

logger.info(f'Extracting {device_name} data...')
data, metadata = self.neural_data_reader.get_data(stream=device_name, dev_conf=dev_conf)
data, metadata = self.rec_manager.read_neural_data(stream=device_name, dev_conf=dev_conf)
if data is None:
logger.info(f'No data available for {device_name}. Skipping...')
else:
Expand Down
70 changes: 39 additions & 31 deletions nsds_lab_to_nwb/components/stimulus/mark_manager.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
from pynwb import TimeSeries
import logging

from nsds_lab_to_nwb.tools.htk.readers.htkfile import HTKFile
from nsds_lab_to_nwb.tools.tdt.tdt_reader import TDTReader
from nsds_lab_to_nwb.components.stimulus.utils import detect_events

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class MarkManager():
def __init__(self, dataset):
self.dataset = dataset

def get_mark_track(self, starting_time, name='recorded_mark'):
# Read the mark track
if hasattr(self.dataset, 'htk_mark_path'):
mark_file = HTKFile(self.dataset.htk_mark_path)
mark_track, meta = mark_file.read_data()
rate = mark_file.sample_rate
mark_onsets = None
def __init__(self, rec_manager, stim_configs, use_tdt_mark_events=False):
self.rec_manager = rec_manager
self.stim_configs = stim_configs
self.use_tdt_mark_events = use_tdt_mark_events

def get_mark_track(self):
# read recorded mark tracks
mark_track, mark_rate = self.rec_manager.read_marks()

# detect marked event times
if self.rec_manager.rec_source == 'tdt' and self.use_tdt_mark_events:
mark_events = self.rec_manager.read_mark_events()
else:
tdt_reader = TDTReader(self.dataset.tdt_path)
mark_track, meta = tdt_reader.get_data(stream='mrk1')
rate = meta['sample_rate']
try:
mark_onsets = tdt_reader.get_events()
except AttributeError:
# there is no mark for baseline (no stimulus) block
mark_onsets = None

# Create the mark timeseries
mark_time_series = TimeSeries(name=name,
data=mark_track,
unit='Volts',
starting_time=starting_time,
rate=rate,
description='Recorded mark that tracks stimulus onsets.')

return mark_time_series, mark_onsets
mark_events = None
mark_events = self.get_mark_events(mark_events, mark_track, mark_rate)

return mark_track, mark_rate, mark_events

def get_mark_events(self, mark_events_input, mark_data, mark_rate):
if mark_events_input is not None:
# loaded directly from TDT object
# (now suppressed by use_tdt_mark_events=False because wn2 requires re-detection)
logger.info('Using marker events directly loaded from TDT')
logger.debug(f'found {len(mark_events_input)} mark events')
return mark_events_input

logger.info('Detecting stimulus onsets by thresholding the mark track')
# note: the min_separation condition is probably not required,
# although it doesn't hurt to keep
stim_duration = self.stim_configs.get('duration', None)
mark_threshold = self.stim_configs['mark_threshold']
mark_events = detect_events(mark_data, mark_rate, mark_threshold,
min_separation=stim_duration)
logger.debug(f'found {len(mark_events)} mark events')
return mark_events
132 changes: 0 additions & 132 deletions nsds_lab_to_nwb/components/stimulus/stim_value_extractor.py

This file was deleted.

Loading