def test_load_file_fail(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, 'no_file.h5') with pytest.raises(IOError) as ex: _ = ws.loadDataFile(file_name) assert 'The file' in str(ex.value) file_name = os.path.join(this_dir_name, 'no_h5.h6') with pytest.raises(RuntimeError) as ex: _ = ws.loadDataFile(file_name) assert 'File must be a WaveSurfer-generated HDF5 (.h5) file.' in str(ex.value)
def test_version_higher_then_latest(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, 'ws_v100_data.h5') with pytest.raises(RuntimeWarning) as ex: _ = ws.loadDataFile(file_name) assert 'You are reading a WaveSurfer file version this module was not' in str(ex.value)
def test_type_single(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, 'test2.h5') dataAsDict = ws.loadDataFile(file_name, format_string='single') scan = dataAsDict['sweep_0001']['analogScans'] assert scan.dtype == 'float32' assert np.allclose(scan.mean(axis=1), np.array([1.78443789, 1.78402293]))
def test_type_raw(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, 'test2.h5') dataAsDict = ws.loadDataFile(file_name, format_string='raw') # conversion to scaled data would fail for this file scan = dataAsDict['sweep_0001']['analogScans'] assert scan.dtype == 'int16' assert np.allclose(scan.mean(axis=1), np.array([5565.12903571, 5563.84042857]))
def test_version_higher(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, 'ws_v1p0p2_data.h5') with pytest.warns(RuntimeWarning) as record: _ = ws.loadDataFile(file_name) # check that only one warning was raised assert len(record) == 1 # check that the message matches assert 'You are reading a WaveSurfer file version this module was not' in record[ 0].message.args[0]
def test_loading_newer_file_with_funnier_sampling_rate(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, '29997_Hz_sampling_rate_0p913_0001.h5') data_file_as_struct = ws.loadDataFile(file_name, 'raw') # The nominal sampling rate was 29997, but the returned # sampling rate should be 100e6/3333 for both acq and stim. returned_acq_sampling_rate = data_file_as_struct['header']['Acquisition']['SampleRate'] n_timebase_ticks_per_acq_sample = 100e6 / returned_acq_sampling_rate # should be exactly 3333 assert n_timebase_ticks_per_acq_sample == 3333 returned_stim_sampling_rate = data_file_as_struct['header']['Stimulation']['SampleRate'] n_timebase_ticks_per_stim_sample = 100e6 / returned_stim_sampling_rate # should be exactly 3333 assert n_timebase_ticks_per_stim_sample == 3333
def test_loading_newer_file_with_funny_sampling_rate(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, '30_kHz_sampling_rate_0p913_0001.h5') data_file_as_struct = ws.loadDataFile(file_name, 'raw') # The requested sampling rate was 30000, but this version of WS # coerces that in the UI to an acheivable rate, which should be # ~30003 Hz, to make (100 Mhz)/fs an integer. returned_acq_sampling_rate = data_file_as_struct['header']['Acquisition']['SampleRate'] n_timebase_ticks_per_acq_sample = 100e6 / returned_acq_sampling_rate # should be exactly 3333 assert n_timebase_ticks_per_acq_sample == 3333 returned_stim_sampling_rate = data_file_as_struct['header']['Stimulation']['SampleRate'] n_timebase_ticks_per_stim_sample = 100e6 / returned_stim_sampling_rate # should be exactly 3333 assert n_timebase_ticks_per_stim_sample == 3333
def test_loading_older_file_with_funny_sampling_rate(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, '30_kHz_sampling_rate_0p912_0001.h5') data_file_as_struct = ws.loadDataFile(file_name, 'raw') # conversion to scaled data would fail for this file # The nominal sampling rate was 30000, but the returned # sampling rate should be ~30003 Hz, to make (100 Mhz)/fs an # integer. returned_acq_sampling_rate = data_file_as_struct['header']['Acquisition']['SampleRate'] n_timebase_ticks_per_acq_sample = 100e6 / returned_acq_sampling_rate # should be exactly 3333 assert n_timebase_ticks_per_acq_sample == 3333 returned_stim_sampling_rate = data_file_as_struct['header']['Stimulation']['SampleRate'] n_timebase_ticks_per_stim_sample = 100e6 / returned_stim_sampling_rate # should be exactly 3333 assert n_timebase_ticks_per_stim_sample == 3333
def load_wavesurfer_file(WS_path): #%% ws_data = ws.loadDataFile(filename=WS_path, format_string='double' ) units = np.array(ws_data['header']['AIChannelUnits']).astype(str) channelnames = np.array(ws_data['header']['AIChannelNames']).astype(str) commandchannel = np.asarray(ws_data['header']['AOChannelNames'],str) =='Command' commandunit = np.asarray(ws_data['header']['AOChannelUnits'],str)[np.where(commandchannel)[0][0]] ephysidx = (channelnames == 'Voltage').argmax() if 'A' in commandunit: recording_mode = 'CC' else: recording_mode = 'VC' framatimesidx = (channelnames == 'FrameTrigger').argmax() keys = list(ws_data.keys()) del keys[keys=='header'] if len(keys)>1: print('MULTIPLE SWEEPS! HANDLE ME!!') time.sleep(1000) sweep = ws_data[keys[0]] sRate = ws_data['header']['AcquisitionSampleRate'][0][0] voltage = sweep['analogScans'][ephysidx,:] #% if units[ephysidx] == 'mV': voltage =voltage/1000 units[ephysidx] = 'V' if units[ephysidx] == 'pA': voltage =voltage/10**12 units[ephysidx] = 'A' if units[ephysidx] == 'nA': voltage =voltage/10**9 units[ephysidx] = 'A' frame_trigger = sweep['analogScans'][framatimesidx,:] timestamp = ws_data['header']['ClockAtRunStart'] #% # ============================================================================= # plt.plot(voltage) # plt.plot(peaks,voltage[peaks],'ro') # ============================================================================= #%% return voltage, frame_trigger, sRate, recording_mode, timestamp , units[ephysidx]
def test_loading_0p933_file(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, 'ws_0p933_data_0001.h5') dataAsDict = ws.loadDataFile(file_name) acq_sampling_rate = float(dataAsDict['header']['Acquisition']['SampleRate']) assert acq_sampling_rate == 20e3 n_a_i_channels = dataAsDict['header']['Acquisition']['AnalogChannelScales'].size assert n_a_i_channels == 1 n_active_a_i_channels = int(dataAsDict['header']['Acquisition']['IsChannelActive'].sum()) assert n_active_a_i_channels == 1 stim_sampling_rate = dataAsDict['header']['Stimulation']['SampleRate'] assert stim_sampling_rate == 20e3 x = dataAsDict['sweep_0001']['analogScans'] assert x.dtype == 'float64' assert np.absolute(np.max(x[0]) - 5) < 0.01 assert np.absolute(np.min(x[0]) - 0) < 0.01 assert np.allclose(x.mean(axis=1), np.array([2.49962616]))
def test_loading_file(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, 'test2.h5') dataAsDict = ws.loadDataFile(file_name) scan = dataAsDict['sweep_0001']['analogScans'] assert scan.dtype == 'float64' assert np.allclose(scan.mean(axis=1), np.array([1.78443789, 1.78402293])) acq_sampling_rate = float(dataAsDict['header']['AcquisitionSampleRate']) assert acq_sampling_rate == 20e3 n_a_i_channels = int(dataAsDict['header']['NAIChannels']) assert n_a_i_channels == 3 n_active_a_i_channels = int(dataAsDict['header']['IsAIChannelActive'].sum()) assert n_active_a_i_channels == 2 stim_sampling_rate = dataAsDict['header']['StimulationSampleRate'] assert stim_sampling_rate == 20e3 x = dataAsDict['sweep_0001']['analogScans'] assert np.absolute(np.max(x[0]) - 5) < 0.01 assert np.absolute(np.min(x[0]) - 0) < 0.01
def test_loading_0p74_file(): this_file_path = os.path.realpath(__file__) this_dir_name = os.path.dirname(this_file_path) file_name = os.path.join(this_dir_name, 'ws_0p74_data_0001.h5') dataAsDict = ws.loadDataFile(file_name, 'raw') # conversion to scaled data would fail for this file acq_sampling_rate = float(dataAsDict['header']['Acquisition']['SampleRate']) assert acq_sampling_rate == 20e3 n_a_i_channels = dataAsDict['header']['Acquisition']['ChannelScales'].size assert n_a_i_channels == 4 n_active_a_i_channels = int(dataAsDict['header']['Acquisition']['IsChannelActive'].sum()) assert n_active_a_i_channels == 4 stim_sampling_rate = dataAsDict['header']['Stimulation']['SampleRate'] assert stim_sampling_rate == 20e3 x_as_int16 = dataAsDict['trial_0001'] assert x_as_int16.dtype == 'int16' assert np.max(x_as_int16[0]) == 15204 assert np.min(x_as_int16[0]) == 2 x = x_as_int16.astype('float64') assert np.allclose(x.mean(axis=1), np.array([7603.29115, 7594.2194, 7598.7204, 7594.06135]))