def run(self): while True: overwrite = self.get_parameter("overwrite") packet = self.receive("in") try: log.debug("%s received %s", self.__class__.__name__, packet) file_name = packet.get("file_name") log.debug("with path %s", file_name) output_file = h5py.File(file_name) output_group_name = self.get_parameter("group") output_group = output_file.require_group(output_group_name) for key in packet.get_attribute_names(): value = packet.get(key) log.debug("%s: read attribute %s=%s", self.__class__.__name__, key, value) if not isinstance(value, np.ndarray): log.debug("%s is not a ndarray, skipping", type(value)) continue if key in output_group and overwrite: del output_group[key] elif key in output_group and not overwrite: log.debug("dataset exists, not overwriting") continue output_group[key] = value log.debug(""" %s: written dataset %s with shape %s to file %s group %s """, self.__class__.__name__, key, value.shape, file_name, output_group_name) output_file.close() except: log.error('Component Failed: %s', self.__class__.__name__, exc_info=True) # yield the CPU, allowing another component to run self.yield_ctrl()
def run(self): # Define our components entry point while True: # for each file name string waiting on our input port for packet in self.receive_all("in"): if packet is not None: input_file = False try: log.debug("%s received %s", self.__class__.__name__, packet) log.debug("with file name %s and data %s", packet.get("file_name"), packet.get("data")) file_name = packet.get("file_name") object_name = packet.get("data") input_file = h5py.File(file_name) input_object = [ dataset[...] for dataset in input_file[object_name].values() if isinstance(dataset, h5py.Dataset)] attrs = [ dataset.attrs for dataset in input_file[object_name].values() if isinstance(dataset, h5py.Dataset)][0] #add info from first dataset for key, value in attrs.items(): packet.set(key, value) log.debug("%s found %d datasets", self.__class__.__name__, len(input_object)) packet.set("data", input_object) except: log.error('%s failed while reading %s', self.__class__.__name__, file_name, exc_info=True) finally: if input_file: input_file.close() # send the packet to the next component self.send('out', packet) # yield the CPU, allowing another component to run self.yield_ctrl()
def run(self): # Define our components entry point while True: # for each file name string waiting on our input port for packet in self.receive_all("in"): log.debug("%s received %s %s", self.__class__.__name__, packet.get("file_name"), packet.get("data")) try: file_name = packet.get("file_name") object_name = packet.get("data") input_file = h5py.File(file_name) input_object = input_file[object_name] self.files.append(input_file) packet.set("data", input_object) except: log.error('%s failed while reading %s', self.__class__.__name__, file_name, exc_info=True) # send the packet to the next component self.send('out', packet) # yield the CPU, allowing another component to run self.yield_ctrl()