我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.get_printoptions()。
def compute_mean(self, file_list): logger = logging.getLogger("acoustic_norm") mean_vector = numpy.zeros((1, self.feature_dimension)) all_frame_number = 0 io_funcs = BinaryIOCollection() for file_name in file_list: features = io_funcs.load_binary_file(file_name, self.feature_dimension) current_frame_number = features.size // self.feature_dimension mean_vector += numpy.reshape(numpy.sum(features, axis=0), (1, self.feature_dimension)) all_frame_number += current_frame_number mean_vector /= float(all_frame_number) # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) logger.info('computed mean vector of length %d :' % mean_vector.shape[1] ) logger.info(' mean: %s' % mean_vector) # restore the print options # numpy.set_printoptions(po) return mean_vector
def compute_mean(self, file_list, start_index, end_index): local_feature_dimension = end_index - start_index mean_vector = numpy.zeros((1, local_feature_dimension)) all_frame_number = 0 io_funcs = BinaryIOCollection() for file_name in file_list: features, current_frame_number = io_funcs.load_binary_file_frame(file_name, self.feature_dimension) mean_vector += numpy.reshape(numpy.sum(features[:, start_index:end_index], axis=0), (1, local_feature_dimension)) all_frame_number += current_frame_number mean_vector /= float(all_frame_number) # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) self.logger.info('computed mean vector of length %d :' % mean_vector.shape[1] ) self.logger.info(' mean: %s' % mean_vector) # restore the print options # numpy.set_printoptions(po) return mean_vector
def compute_mean(self, file_list): logger = logging.getLogger("acoustic_norm") mean_vector = numpy.zeros((1, self.feature_dimension)) all_frame_number = 0 io_funcs = BinaryIOCollection() for file_name in file_list: features = io_funcs.load_binary_file(file_name, self.feature_dimension) current_frame_number = features.size / self.feature_dimension mean_vector += numpy.reshape(numpy.sum(features, axis=0), (1, self.feature_dimension)) all_frame_number += current_frame_number mean_vector /= float(all_frame_number) # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) logger.info('computed mean vector of length %d :' % mean_vector.shape[1] ) logger.info(' mean: %s' % mean_vector) # restore the print options # numpy.set_printoptions(po) return mean_vector
def pformat(obj, indent=0, depth=3): if 'numpy' in sys.modules: import numpy as np print_options = np.get_printoptions() np.set_printoptions(precision=6, threshold=64, edgeitems=1) else: print_options = None out = pprint.pformat(obj, depth=depth, indent=indent) if print_options: np.set_printoptions(**print_options) return out ############################################################################### # class `Logger` ###############################################################################
def after_run(self, run_context, run_values): global_episode = run_values.results['global_episode'] if can_run_hook(run_context): if self._timer.should_trigger_for_episode(global_episode): original = np.get_printoptions() np.set_printoptions(suppress=True) elapsed_secs, _ = self._timer.update_last_triggered_episode(global_episode) if self._formatter: logging.info(self._formatter(run_values.results)) else: stats = [] for tag in self._tag_order: stats.append("%s = %s" % (tag, run_values.results[tag])) if elapsed_secs is not None: logging.info("%s (%.3f sec)", ", ".join(stats), elapsed_secs) else: logging.info("%s", ", ".join(stats)) np.set_printoptions(**original)
def np_printoptions(**kwargs): """Context manager to temporarily set numpy print options.""" old = np.get_printoptions() np.set_printoptions(**kwargs) yield np.set_printoptions(**old)
def setUp(self): self.oldopts = np.get_printoptions()
def printoptions(*args, **kwargs): original = np.get_printoptions() np.set_printoptions(*args, **kwargs) try: yield finally: np.set_printoptions(**original)
def compute_mean(self, file_list, start_index, end_index): logger = logging.getLogger('feature_normalisation') local_feature_dimension = end_index - start_index mean_vector = numpy.zeros((1, local_feature_dimension)) all_frame_number = 0 io_funcs = HTKFeat_read() for file_name in file_list: features, current_frame_number = io_funcs.getall(file_name) # io_funcs = HTK_Parm_IO() # io_funcs.read_htk(file_name) # features = io_funcs.data # current_frame_number = io_funcs.n_samples mean_vector += numpy.reshape(numpy.sum(features[:, start_index:end_index], axis=0), (1, local_feature_dimension)) all_frame_number += current_frame_number mean_vector /= float(all_frame_number) # setting the print options in this way seems to break subsequent printing of numpy float32 types # no idea what is going on - removed until this can be solved # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) logger.info('computed mean vector of length %d :' % mean_vector.shape[1] ) logger.info(' mean: %s' % mean_vector) # restore the print options # numpy.set_printoptions(po) self.mean_vector = mean_vector return mean_vector
def compute_std(self, file_list, mean_vector, start_index, end_index): logger = logging.getLogger('feature_normalisation') local_feature_dimension = end_index - start_index std_vector = numpy.zeros((1, self.feature_dimension)) all_frame_number = 0 io_funcs = HTKFeat_read() for file_name in file_list: features, current_frame_number = io_funcs.getall(file_name) mean_matrix = numpy.tile(mean_vector, (current_frame_number, 1)) std_vector += numpy.reshape(numpy.sum((features[:, start_index:end_index] - mean_matrix) ** 2, axis=0), (1, local_feature_dimension)) all_frame_number += current_frame_number std_vector /= float(all_frame_number) std_vector = std_vector ** 0.5 # setting the print options in this way seems to break subsequent printing of numpy float32 types # no idea what is going on - removed until this can be solved # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) logger.info('computed std vector of length %d' % std_vector.shape[1] ) logger.info(' std: %s' % std_vector) # restore the print options # numpy.set_printoptions(po) self.std_vector = std_vector return std_vector
def printoptions(*args, **kwargs): original = numpy.get_printoptions() numpy.set_printoptions(*args, **kwargs) yield numpy.set_printoptions(**original)
def find_min_max_values(self, in_file_list): logger = logging.getLogger("acoustic_norm") file_number = len(in_file_list) min_value_matrix = numpy.zeros((file_number, self.feature_dimension)) max_value_matrix = numpy.zeros((file_number, self.feature_dimension)) io_funcs = BinaryIOCollection() for i in range(file_number): features = io_funcs.load_binary_file(in_file_list[i], self.feature_dimension) temp_min = numpy.amin(features, axis = 0) temp_max = numpy.amax(features, axis = 0) min_value_matrix[i, ] = temp_min; max_value_matrix[i, ] = temp_max; self.min_vector = numpy.amin(min_value_matrix, axis = 0) self.max_vector = numpy.amax(max_value_matrix, axis = 0) self.min_vector = numpy.reshape(self.min_vector, (1, self.feature_dimension)) self.max_vector = numpy.reshape(self.max_vector, (1, self.feature_dimension)) # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) logger.info('across %d files found min/max values of length %d:' % (file_number,self.feature_dimension) ) logger.info(' min: %s' % self.min_vector) logger.info(' max: %s' % self.max_vector) # restore the print options # numpy.set_printoptions(po)
def find_min_max_values(self, in_file_list, start_index, end_index): local_feature_dimension = end_index - start_index file_number = len(in_file_list) min_value_matrix = numpy.zeros((file_number, local_feature_dimension)) max_value_matrix = numpy.zeros((file_number, local_feature_dimension)) io_funcs = BinaryIOCollection() for i in range(file_number): features = io_funcs.load_binary_file(in_file_list[i], self.feature_dimension) temp_min = numpy.amin(features[:, start_index:end_index], axis = 0) temp_max = numpy.amax(features[:, start_index:end_index], axis = 0) min_value_matrix[i, ] = temp_min; max_value_matrix[i, ] = temp_max; self.min_vector = numpy.amin(min_value_matrix, axis = 0) self.max_vector = numpy.amax(max_value_matrix, axis = 0) self.min_vector = numpy.reshape(self.min_vector, (1, local_feature_dimension)) self.max_vector = numpy.reshape(self.max_vector, (1, local_feature_dimension)) # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) self.logger.info('found min/max values of length %d:' % local_feature_dimension) self.logger.info(' min: %s' % self.min_vector) self.logger.info(' max: %s' % self.max_vector) # restore the print options # numpy.set_printoptions(po)
def compute_std(self, file_list, mean_vector, start_index, end_index): local_feature_dimension = end_index - start_index std_vector = numpy.zeros((1, self.feature_dimension)) all_frame_number = 0 io_funcs = BinaryIOCollection() for file_name in file_list: features, current_frame_number = io_funcs.load_binary_file_frame(file_name, self.feature_dimension) mean_matrix = numpy.tile(mean_vector, (current_frame_number, 1)) std_vector += numpy.reshape(numpy.sum((features[:, start_index:end_index] - mean_matrix) ** 2, axis=0), (1, local_feature_dimension)) all_frame_number += current_frame_number std_vector /= float(all_frame_number) std_vector = std_vector ** 0.5 # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) self.logger.info('computed std vector of length %d' % std_vector.shape[1] ) self.logger.info(' std: %s' % std_vector) # restore the print options # numpy.set_printoptions(po) return std_vector
def compute_mean(self, file_list, start_index, end_index): logger = logging.getLogger('feature_normalisation') local_feature_dimension = end_index - start_index mean_vector = numpy.zeros((1, local_feature_dimension)) all_frame_number = 0 io_funcs = BinaryIOCollection() for file_name in file_list: features, current_frame_number = io_funcs.load_binary_file_frame(file_name, self.feature_dimension) mean_vector += numpy.reshape(numpy.sum(features[:, start_index:end_index], axis=0), (1, local_feature_dimension)) all_frame_number += current_frame_number mean_vector /= float(all_frame_number) # setting the print options in this way seems to break subsequent printing of numpy float32 types # no idea what is going on - removed until this can be solved # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) logger.info('computed mean vector of length %d :' % mean_vector.shape[1] ) logger.info(' mean: %s' % mean_vector) # restore the print options # numpy.set_printoptions(po) self.mean_vector = mean_vector return mean_vector
def compute_std(self, file_list, mean_vector, start_index, end_index): logger = logging.getLogger('feature_normalisation') local_feature_dimension = end_index - start_index std_vector = numpy.zeros((1, self.feature_dimension)) all_frame_number = 0 io_funcs = BinaryIOCollection() for file_name in file_list: features, current_frame_number = io_funcs.load_binary_file_frame(file_name, self.feature_dimension) mean_matrix = numpy.tile(mean_vector, (current_frame_number, 1)) std_vector += numpy.reshape(numpy.sum((features[:, start_index:end_index] - mean_matrix) ** 2, axis=0), (1, local_feature_dimension)) all_frame_number += current_frame_number std_vector /= float(all_frame_number) std_vector = std_vector ** 0.5 # setting the print options in this way seems to break subsequent printing of numpy float32 types # no idea what is going on - removed until this can be solved # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) logger.info('computed std vector of length %d' % std_vector.shape[1] ) logger.info(' std: %s' % std_vector) # restore the print options # numpy.set_printoptions(po) self.std_vector = std_vector return std_vector
def test_precision(): """test various values for float_precision.""" f = PlainTextFormatter() nt.assert_equal(f(pi), repr(pi)) f.float_precision = 0 if numpy: po = numpy.get_printoptions() nt.assert_equal(po['precision'], 0) nt.assert_equal(f(pi), '3') f.float_precision = 2 if numpy: po = numpy.get_printoptions() nt.assert_equal(po['precision'], 2) nt.assert_equal(f(pi), '3.14') f.float_precision = '%g' if numpy: po = numpy.get_printoptions() nt.assert_equal(po['precision'], 2) nt.assert_equal(f(pi), '3.14159') f.float_precision = '%e' nt.assert_equal(f(pi), '3.141593e+00') f.float_precision = '' if numpy: po = numpy.get_printoptions() nt.assert_equal(po['precision'], 8) nt.assert_equal(f(pi), repr(pi))
def __repr__(self): """ FloatArrayParameter needs to "truncate" the array by temporarily overriding np.set_printoptions """ opt = np.get_printoptions() # <Parameter:_qualifier= takes 13+len(qualifier) characters np.set_printoptions(threshold=8, edgeitems=3, linewidth=opt['linewidth']-(13+len(self.qualifier))) repr_ = super(FloatArrayParameter, self).__repr__() np.set_printoptions(**opt) return repr_
def __str__(self): """ FloatArrayParameter needs to "truncate" the array by temporarily overriding np.set_printoptions """ opt = np.get_printoptions() # Value:_ takes 7 characters np.set_printoptions(threshold=8, edgeitems=3, linewidth=opt['linewidth']-7) str_ = super(FloatArrayParameter, self).__str__() np.set_printoptions(**opt) return str_
def to_string_short(self): """ see also :meth:`to_string` :return: a shorter abreviated string reprentation of the parameter """ opt = np.get_printoptions() np.set_printoptions(threshold=8, edgeitems=3, linewidth=opt['linewidth']-len(self.uniquetwig)-2) str_ = super(FloatArrayParameter, self).to_string_short() np.set_printoptions(**opt) return str_
def __repr__(self): """ IntArrayParameter needs to "truncate" the array by temporarily overriding np.set_printoptions """ opt = np.get_printoptions() # <Parameter:_qualifier= takes 13+len(qualifier) characters np.set_printoptions(threshold=8, edgeitems=3, linewidth=opt['linewidth']-(13+len(self.qualifier))) repr_ = super(IntArrayParameter, self).__repr__() np.set_printoptions(**opt) return repr_
def __str__(self): """ IntArrayParameter needs to "truncate" the array by temporarily overriding np.set_printoptions """ opt = np.get_printoptions() # Value:_ takes 7 characters np.set_printoptions(threshold=8, edgeitems=3, linewidth=opt['linewidth']-7) str_ = super(IntArrayParameter, self).__str__() np.set_printoptions(**opt) return str_
def find_min_max_values(self, in_file_list): logger = logging.getLogger("acoustic_norm") file_number = len(in_file_list) min_value_matrix = numpy.zeros((file_number, self.feature_dimension)) max_value_matrix = numpy.zeros((file_number, self.feature_dimension)) io_funcs = BinaryIOCollection() for i in xrange(file_number): features = io_funcs.load_binary_file(in_file_list[i], self.feature_dimension) temp_min = numpy.amin(features, axis = 0) temp_max = numpy.amax(features, axis = 0) min_value_matrix[i, ] = temp_min; max_value_matrix[i, ] = temp_max; self.min_vector = numpy.amin(min_value_matrix, axis = 0) self.max_vector = numpy.amax(max_value_matrix, axis = 0) self.min_vector = numpy.reshape(self.min_vector, (1, self.feature_dimension)) self.max_vector = numpy.reshape(self.max_vector, (1, self.feature_dimension)) # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) logger.info('across %d files found min/max values of length %d:' % (file_number,self.feature_dimension) ) logger.info(' min: %s' % self.min_vector) logger.info(' max: %s' % self.max_vector) # restore the print options # numpy.set_printoptions(po)
def compute_std(self, file_list, mean_vector): logger = logging.getLogger("acoustic_norm") std_vector = numpy.zeros((1, self.feature_dimension)) all_frame_number = 0 io_funcs = BinaryIOCollection() for file_name in file_list: features = io_funcs.load_binary_file(file_name, self.feature_dimension) current_frame_number = features.size / self.feature_dimension mean_matrix = numpy.tile(mean_vector, (current_frame_number, 1)) std_vector += numpy.reshape(numpy.sum((features - mean_matrix) ** 2, axis=0), (1, self.feature_dimension)) all_frame_number += current_frame_number std_vector /= float(all_frame_number) std_vector = std_vector ** 0.5 # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) logger.info('computed std vector of length %d' % std_vector.shape[1] ) logger.info(' std: %s' % std_vector) # restore the print options # numpy.set_printoptions(po) return std_vector
def find_min_max_values(self, in_file_list, start_index, end_index): local_feature_dimension = end_index - start_index file_number = len(in_file_list) min_value_matrix = numpy.zeros((file_number, local_feature_dimension)) max_value_matrix = numpy.zeros((file_number, local_feature_dimension)) io_funcs = BinaryIOCollection() for i in xrange(file_number): features = io_funcs.load_binary_file(in_file_list[i], self.feature_dimension) temp_min = numpy.amin(features[:, start_index:end_index], axis = 0) temp_max = numpy.amax(features[:, start_index:end_index], axis = 0) min_value_matrix[i, ] = temp_min; max_value_matrix[i, ] = temp_max; self.min_vector = numpy.amin(min_value_matrix, axis = 0) self.max_vector = numpy.amax(max_value_matrix, axis = 0) self.min_vector = numpy.reshape(self.min_vector, (1, local_feature_dimension)) self.max_vector = numpy.reshape(self.max_vector, (1, local_feature_dimension)) # po=numpy.get_printoptions() # numpy.set_printoptions(precision=2, threshold=20, linewidth=1000, edgeitems=4) self.logger.info('found min/max values of length %d:' % local_feature_dimension) self.logger.info(' min: %s' % self.min_vector) self.logger.info(' max: %s' % self.max_vector) # restore the print options # numpy.set_printoptions(po)
def _printoptions(*args, **kwargs): original = np.get_printoptions() np.set_printoptions(*args, **kwargs) yield np.set_printoptions(**original) # http://code.activestate.com/recipes/577586-converts-from-decimal-to-any-base-between-2-and-26/
def parse_numpy_printoption(kv_str): """Sets a single numpy printoption from a string of the form 'x=y'. See documentation on numpy.set_printoptions() for details about what values x and y can take. x can be any option listed there other than 'formatter'. Args: kv_str: A string of the form 'x=y', such as 'threshold=100000' Raises: argparse.ArgumentTypeError: If the string couldn't be used to set any nump printoption. """ k_v_str = kv_str.split("=", 1) if len(k_v_str) != 2 or not k_v_str[0]: raise argparse.ArgumentTypeError("'%s' is not in the form k=v." % kv_str) k, v_str = k_v_str printoptions = np.get_printoptions() if k not in printoptions: raise argparse.ArgumentTypeError("'%s' is not a valid printoption." % k) v_type = type(printoptions[k]) if v_type is type(None): raise argparse.ArgumentTypeError( "Setting '%s' from the command line is not supported." % k) try: v = (v_type(v_str) if v_type is not bool else flags.BooleanParser().Parse(v_str)) except ValueError as e: raise argparse.ArgumentTypeError(e.message) np.set_printoptions(**{k: v})