def plot_analog_stream_channel(analog_stream, channel_idx, from_in_s=0, to_in_s=None, show=True): """ Plots data from a single AnalogStream channel :param analog_stream: A AnalogStream object :param channel_idx: A scalar channel index (0 <= channel_idx < # channels in the AnalogStream) :param from_in_s: The start timestamp of the plot (0 <= from_in_s < to_in_s). Default: 0 :param to_in_s: The end timestamp of the plot (from_in_s < to_in_s <= duration). Default: None (= recording duration) :param show: If True (default), the plot is directly created. For further plotting, use show=False """ # extract basic information ids = [c.channel_id for c in analog_stream.channel_infos.values()] channel_id = ids[channel_idx] channel_info = analog_stream.channel_infos[channel_id] sampling_frequency = channel_info.sampling_frequency.magnitude # get start and end index from_idx = max(0, int(from_in_s * sampling_frequency)) if to_in_s is None: to_idx = analog_stream.channel_data.shape[1] else: to_idx = min(analog_stream.channel_data.shape[1], int(to_in_s * sampling_frequency)) # get the timestamps for each sample time = analog_stream.get_channel_sample_timestamps(channel_id, from_idx, to_idx) # scale time to seconds: scale_factor_for_second = Q_(1, time[1]).to(ureg.s).magnitude time_in_sec = time[0] * scale_factor_for_second # get the signal signal = analog_stream.get_channel_in_range(channel_id, from_idx, to_idx) # scale signal to µV: scale_factor_for_uV = Q_(1, signal[1]).to(ureg.uV).magnitude signal_in_uV = signal[0] * scale_factor_for_uV # construct the plot _ = plt.figure(figsize=(20, 6)) _ = plt.plot(time_in_sec, signal_in_uV) _ = plt.xlabel('Time (%s)' % ureg.s) _ = plt.ylabel('Voltage (%s)' % ureg.uV) _ = plt.title('Channel %s' % channel_info.info['Label']) if show: plt.show()
def draw_channel_with_spectrogram(stream, channel_id): ''' Draw one channel of given ID within its original range ''' time = stream.get_channel_sample_timestamps(channel_id, 0, 10000) # scale time to seconds: scale_factor_for_second = Q_(1, time[1]).to(ureg.s).magnitude time_in_sec = time[0] * scale_factor_for_second signal = stream.get_channel_in_range(channel_id, 0, 10000) sampling_frequency = stream.channel_infos[ channel_id].sampling_frequency.magnitude # already in Hz pl.figure(figsize=(20, 12)) # time domain axtp = pl.subplot(211) pl.plot(time_in_sec, signal[0]) pl.xlabel('Time (%s)' % time[1]) pl.ylabel('Voltage (%s)' % signal[1]) pl.title('Sampled signal (%s)' % stream.label) # frequency domain pl.subplot(212) pl.specgram(signal[0], NFFT=512, noverlap=128, Fs=sampling_frequency, cmap=pl.cm.gist_heat, scale_by_freq=False) pl.xlabel('Time (%s)' % time[1]) pl.ylabel('Frequency (Hz)') pl.show()
def read_h5_file(data_path, progress_callback): progress_callback.emit(0) channel_raw_data = McsPy.McsData.RawData(data_path) progress_callback.emit(10) fs = int(channel_raw_data.recordings[0].analog_streams[0].channel_infos[0]. sampling_frequency.magnitude) analog_stream_0 = channel_raw_data.recordings[0].analog_streams[0] analog_stream_0_data = analog_stream_0.channel_data progress_callback.emit(25) np_analog_stream_0_data = np.transpose(analog_stream_0_data) progress_callback.emit(50) stream = channel_raw_data.recordings[0].analog_streams[0] time = stream.get_channel_sample_timestamps(0, 0) scale_factor_for_second = Q_(1, time[1]).to(ureg.s).magnitude time_in_sec = time[0] * scale_factor_for_second progress_callback.emit(75) data = Data() data.stream = np_analog_stream_0_data / 1000000 data.time = np.asarray(time_in_sec) data.fs = fs progress_callback.emit(100) return data
def get_MEA_Signal(analog_stream, channel_idx, from_in_s=0, to_in_s=None): ''' Extracts one Channels (channel_idx) Sginal :param analog_stream = the analogstream from one recording :param channel_idx = the channel index of the channel where you extract the values from :param from_in_s= starting point of the range you want to observe in seconds :param to_in_s= ending point of the range you want to observe. Default is None (i.e. whole range) Returns: the signal in uV, time stamps in sec, the sampling frequency ''' ids = [c.channel_id for c in analog_stream.channel_infos.values()] channel_id = ids[channel_idx] channel_info = analog_stream.channel_infos[channel_id] sampling_frequency = channel_info.sampling_frequency.magnitude # get start and end index from_idx = max(0, int(from_in_s * sampling_frequency)) if to_in_s is None: to_idx = analog_stream.channel_data.shape[1] else: to_idx = min(analog_stream.channel_data.shape[1], int(to_in_s * sampling_frequency)) # get the timestamps for each sample time = analog_stream.get_channel_sample_timestamps(channel_id, from_idx, to_idx) # scale time to seconds: scale_factor_for_second = Q_(1, time[1]).to(ureg.s).magnitude time_in_sec = time[0] * scale_factor_for_second # get the signal signal = analog_stream.get_channel_in_range(channel_id, from_idx, to_idx) # scale signal to �V: scale_factor_for_uV = Q_(1, signal[1]).to(ureg.uV).magnitude signal_in_uV = signal[0] * scale_factor_for_uV return signal_in_uV, time_in_sec, sampling_frequency, scale_factor_for_second
def read_h5_file(data_path): channel_raw_data = McsPy.McsData.RawData(data_path) analog_stream_0 = channel_raw_data.recordings[0].analog_streams[0] analog_stream_0_data = analog_stream_0.channel_data np_analog_stream_0_data = np.transpose(analog_stream_0_data) stream = channel_raw_data.recordings[0].analog_streams[0] time = stream.get_channel_sample_timestamps(0, 0) scale_factor_for_second = Q_(1, time[1]).to(ureg.s).magnitude time_in_sec = time[0] * scale_factor_for_second return np_analog_stream_0_data, time_in_sec
def get_channel_infos(filedirectory, meafile): channel_raw_data = McsPy.McsData.RawData( os.path.join(filedirectory, meafile)) print(channel_raw_data.recordings) print(channel_raw_data.comment) print(channel_raw_data.date) print(channel_raw_data.clr_date) print(channel_raw_data.date_in_clr_ticks) print(channel_raw_data.file_guid) print(channel_raw_data.mea_name) print(channel_raw_data.mea_sn) print(channel_raw_data.mea_layout) print(channel_raw_data.program_name) print(channel_raw_data.program_version) analognumber = len(channel_raw_data.recordings[0].analog_streams.keys()) print('In total ' + str(analognumber) + " analog_streams were identified.\n") for i in range(len(channel_raw_data.recordings[0].analog_streams.keys())): keylist = [] stream = channel_raw_data.recordings[0].analog_streams[i] for key in stream.channel_infos.keys(): keylist.append(key) channel_id = keylist[0] datapoints = channel_raw_data.recordings[0].analog_streams[ i].channel_data.shape[1] samplingfrequency = stream.channel_infos[channel_id].sampling_frequency ticks = stream.channel_infos[channel_id].info['Tick'] time = stream.get_channel_sample_timestamps(channel_id) scale_factor_for_second = Q_(1, time[1]).to(ureg.s).magnitude time_in_sec = time[0] * scale_factor_for_second timelengthrecording_ms = time[0][-1] + ticks timelengthrecording_s = (time[0][-1] + ticks) * scale_factor_for_second print("analog_stream Nr. " + str(i) + ": ") print("datapoints measured = " + str(datapoints)) print("sampling frequency = " + str(samplingfrequency)) print("ticks = " + str(ticks)) print("total recordingtime is: " + str(timelengthrecording_s) + "seconds \n")
resting_spikedic = {} spikedic = {} cutouts_dic = {} keylist = [] filename = file print('Working on file: ' + filename) channel_raw_data = McsPy.McsData.RawData(filename) get_channel_infos(inputdirectory, filename) analog_stream_0 = channel_raw_data.recordings[0].analog_streams[0] stream = analog_stream_0 for key in stream.channel_infos.keys(): keylist.append(key) channel_id = keylist[0] tick = stream.channel_infos[channel_id].info['Tick'] time = stream.get_channel_sample_timestamps(channel_id) scale_factor_for_second = Q_(1, time[1]).to(ureg.s).magnitude time_in_sec = time[0] * scale_factor_for_second timelengthrecording_ms = time[0][-1] + tick timelengthrecording_s = (time[0][-1] + tick) * scale_factor_for_second fs = int(stream.channel_infos[channel_id].sampling_frequency.magnitude) analog_stream_0_data = analog_stream_0.channel_data np_analog_stream_0_data = np.transpose( channel_raw_data.recordings[0].analog_streams[0].channel_data) np_analog_for_filter = np.transpose(np_analog_stream_0_data) # np_analog_stream_1_data = np.transpose(channel_raw_data.recordings[0].analog_streams[1].channel_data) # np_analog_stream_1_data_transpose = np.transpose(np_analog_stream_1_data) outpath = outputdirectory + '/' + filename.split('.')[0] + '_plots' try: os.mkdir(outpath) except OSError: