def _read_file(self, tdms_reader, read_metadata_only): tdms_reader.read_metadata() # Use object metadata to build group and channel objects group_properties = OrderedDict() group_channels = OrderedDict() object_properties = { path_string: self._convert_properties(obj.properties) for path_string, obj in tdms_reader.object_metadata.items() } try: self._properties = object_properties['/'] except KeyError: pass for (path_string, obj) in tdms_reader.object_metadata.items(): properties = object_properties[path_string] path = ObjectPath.from_string(path_string) if path.is_root: pass elif path.is_group: group_properties[path.group] = properties else: # Object is a channel try: channel_group_properties = object_properties[ path.group_path()] except KeyError: channel_group_properties = OrderedDict() channel = TdmsChannel(path, obj.data_type, obj.scaler_data_types, obj.num_values, properties, channel_group_properties, self._properties, tdms_reader, self._raw_timestamps, self._memmap_dir) if path.group in group_channels: group_channels[path.group].append(channel) else: group_channels[path.group] = [channel] # Create group objects containing channels and properties for group_name, properties in group_properties.items(): try: channels = group_channels[group_name] except KeyError: channels = [] group_path = ObjectPath(group_name) self._groups[group_name] = TdmsGroup(group_path, properties, channels) for group_name, channels in group_channels.items(): if group_name not in self._groups: # Group with channels but without any corresponding object metadata in the file: group_path = ObjectPath(group_name) self._groups[group_name] = TdmsGroup(group_path, {}, channels) if not read_metadata_only: self._read_data(tdms_reader)
def _build_index(self): """ Builds an index into the segment data for faster lookup of values _segment_channel_offsets provides data offset at the end of each segment per channel _segment_chunk_sizes provides chunk sizes in each segment per channel """ data_objects = [ path for (path, obj) in self.object_metadata.items() if ObjectPath.from_string(path).is_channel ] num_segments = len(self._segments) segment_num_values = { path: np.zeros(num_segments, dtype=np.int64) for path in data_objects } segment_chunk_sizes = { path: np.zeros(num_segments, dtype=np.int64) for path in data_objects } for i, segment in enumerate(self._segments): for obj in segment.ordered_objects: if not obj.has_data: continue segment_chunk_sizes[ obj.path][i] = obj.number_values if obj.has_data else 0 segment_num_values[obj.path][i] = _number_of_segment_values( obj, segment) self._segment_chunk_sizes = segment_chunk_sizes self._segment_channel_offsets = { path: np.cumsum(segment_count) for (path, segment_count) in segment_num_values.items() }
def objects(self): """ (Deprecated) A dictionary of objects in the TDMS file, where the keys are the object paths. """ _deprecated( "TdmsFile.objects", "Use TdmsFile.groups() to access all groups in the file, " + "and group.channels() to access all channels in a group.") objects = OrderedDict() root_path = ObjectPath() objects[str(root_path)] = RootObject(self._properties) for group in self.groups(): objects[group.path] = group for channel in group.channels(): objects[channel.path] = channel return objects
def object(self, *path): """(Deprecated) Get a TDMS object from the file :param path: The object group and channel names. Providing no channel returns a group object, and providing no channel or group will return the root object. :rtype: One of :class:`TdmsGroup`, :class:`TdmsChannel`, :class:`RootObject` For example, to get the root object:: object() To get a group:: object("group_name") To get a channel:: object("group_name", "channel_name") """ _deprecated( "TdmsFile.object", "Use TdmsFile.properties to access properties of the root object, " + "TdmsFile[group_name] to access a group object and " + "TdmsFile[group_name][channel_name] to access a channel object.") def get_name(component): try: return component.name except AttributeError: return component path = [get_name(c) for c in path] object_path = ObjectPath(*path) try: return self.objects[str(object_path)] except KeyError: raise KeyError("Invalid object path: %s" % object_path)
def path(self): """The string representation of this channel's path """ return str(ObjectPath(self.group, self.channel))
def path(self): """The string representation of this group's path """ return str(ObjectPath(self.group))