def decode(self, compressed, decompressed=None): """perform decompression params: compressed: bytes - the data to be decompressed decompressed: numpy.ndarray - memory to be used to decompress data into """ compressed_lp = None decompressed_lp = None try: compressed_lp = pressio.io_data_from_bytes(compressed) decompressed_lp = pressio.io_data_from_numpy(decompressed) rc = pressio.compressor_decompress(self._compressor, compressed_lp, decompressed_lp) if rc: raise PressioException.from_compressor(self._compressor) dec = pressio.io_data_to_numpy(decompressed_lp) if decompressed is not None: return dec else: return ndarray_copy(dec, decompressed) finally: pressio.data_free(compressed_lp) pressio.data_free(decompressed_lp)
def evaluate(self, input=None, compressed=None, output=None): results_lp = None try: input_lp = input if input is None else pressio.io_data_from_numpy( input) compressed_lp = compressed if compressed is None else pressio.io_data_from_bytes( compressed) output_lp = output if output is None else pressio.io_data_from_numpy( output) results_lp = pressio.metrics_evaluate(self._metric, input_lp, compressed_lp, output_lp) return _pressio_to_python(results_lp) finally: if input_lp is not None: pressio.data_free(input_lp) if output_lp is not None: pressio.data_free(output_lp) if compressed_lp is not None: pressio.data_free(compressed_lp) if results_lp is not None: pressio.options_free(results_lp)
def to_libpressio(x): op = None if isinstance(value, np.ndarray): value_lp = pressio.io_data_from_numpy(value) op = pressio.option_new_data(value_lp) pressio.data_free(value_lp) elif isinstance(value, list): if value: # list is non-empty if isinstance(value[0], str): op = pressio.option_new_strings( pressio.vector_string([i.encode() for i in value])) elif isinstance(value[0], int) or isinstance(value[0], float): arr = np.array(value) lp = pressio.io_data_from_numpy(arr) op = pressio.option_new_data(lp) pressio.data_free(lp) else: raise TypeError("unexpected list type: " + value) else: # list is empty op = pressio.option_new_strings(pressio.vector_string()) elif isinstance(value, float): op = pressio.option_new_double(value) elif isinstance(value, str): op = pressio.option_new_string(value.encode()) elif isinstance(value, bytes): data = pressio.io_data_from_bytes(value) op = pressio.option_new_data(data) pressio.data_free(data) elif isinstance(value, int): op = pressio.option_new_integer64(value) elif pressio.LIBPRESSIO_HAS_MPI4PY: if isinstance(value, MPI.Comm): op = pressio.option_new_comm(value) else: raise TypeError("Unsupported type " + str(type(value))) return op