def capture_chunk(self, in_data, frame_count, time_info, status): self.data = np.fromstring(in_data, dtype=np.float32) if self._has_runner: data_chunk = nptdms.ChannelObject(self.group_name, self.channel_name, self.data, properties={}) if self.filepath is not None: with nptdms.TdmsWriter(self.filepath, 'a') as writer: writer.write_segment([data_chunk]) return (self.data, pyaudio.paContinue)
def add_tdms_ch(self, grp_name, ch_name: str, ch_properties: dict, data: list) -> None: """ add a channel object to the segment set that will eventually be added to the tdms object :param grp_name: name of the parent group :param ch_name: name of the channel :param ch_properties: dictionary of the channel properties :param data: dictionary of the channel data """ channel = nptdms.ChannelObject(grp_name, ch_name, data, properties=ch_properties.copy()) self.tdms_object_set.update({channel})
def write_chunks_to_file( tdms_writer: nptdms.TdmsWriter, index_ranges: List[Tuple[int, int]], group, channel, segment_size: int, ): """Writes correct data slice per slice to disk. Arguments: tdms_writer: Tdms handle for the new file index_ranges: Chunk Indices that point to valid data slices group: TDMS Group inside the old tdms file channel: TDMS Channel inside group segment_size: Sets size of each segment written to a TDMS file """ clean_data = [] clean_data_nbytes = 0 for (offset, length) in tqdm.tqdm(index_ranges): data = channel.read_data(offset=offset, length=length) clean_data.append(data) clean_data_nbytes += data.nbytes # When segment_size is reached, a new segment is written to file if clean_data_nbytes > segment_size * 1_000_000_000: new_channel = nptdms.ChannelObject(group.name, channel.name, np.concatenate(clean_data)) tdms_writer.write_segment([new_channel]) clean_data = [] clean_data_nbytes = 0 # The remaining chunks are written to file as a last smaller segment if clean_data: new_channel = nptdms.ChannelObject(group.name, channel.name, np.concatenate(clean_data)) tdms_writer.write_segment([new_channel])
def copy_tdms(nwb, in_path, out_path, nrois): num_all_rois = nwb['/processing/Acquired_ROIs/roi_spec'].shape[0] print('Copying {} of {} ROIs from {} to {}'.format( nrois, num_all_rois, in_path, out_path)) in_tdms = nptdms.TdmsFile(in_path) group_name = 'Functional Imaging Data' with nptdms.TdmsWriter(out_path) as out_tdms: root, group = in_tdms.object(), in_tdms.object(group_name) out_tdms.write_segment([root, group]) for ch, channel in {'0': 'Red', '1': 'Green'}.items(): ch_name = 'Channel {} Data'.format(ch) ch_obj = in_tdms.object(group_name, ch_name) shape = (cycles_per_trial(nwb), num_all_rois, -1) ch_data = ch_obj.data.reshape(shape) subset = ch_data[:, :nrois, :].reshape(-1) new_obj = nptdms.ChannelObject(group_name, ch_name, subset, properties={}) out_tdms.write_segment([new_obj])