numpy.get_printoptions - python examples

Here are the examples of the python api numpy.get_printoptions taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

40 Examples 7

3 View Complete Implementation : formatter.py
Copyright MIT License
Author : agoose77
def array_to_html(
    array: np.ndarray, formatter: typing.Callable[..., typing.List[str]] = fixed_format_items, **formatter_kwargs
) -> str:
    """Render NumPy array as an HTML table.

    :param array: ndarray object
    :param formatter: items formatter
    :param formatter_kwargs: keyword arguments for items formatter
    :return: HTML string
    """
    print_options = np.get_printoptions()
    edge_items = print_options["edgeitems"]
    threshold = print_options["threshold"]

    if array.size < threshold:
        edge_items = 0

    items = render_table((), array, edge_items)
    return "\n".join(formatter(items, **formatter_kwargs))

3 View Complete Implementation : valuesummarymeter.py
Copyright MIT License
Author : alexsax
    def __str__(self): 
        old_po = np.get_printoptions()
        np.set_printoptions(precision=3)
        res = "mean(std) {} ({}) \tmin/max {}/{}\t".format(
            *[np.array(v) for v in [self.mean, self.std, self.min, self.max]])
        np.set_printoptions(**old_po)
        return res

3 View Complete Implementation : test_arrayprint.py
Copyright MIT License
Author : alvarob96
    def test_ctx_mgr_restores(self):
        # test that print options are actually restrored
        opts = np.get_printoptions()
        with np.printoptions(precision=opts['precision'] - 1,
                             linewidth=opts['linewidth'] - 4):
            past
        astert_equal(np.get_printoptions(), opts)

3 View Complete Implementation : test_arrayprint.py
Copyright MIT License
Author : alvarob96
    def test_ctx_mgr_exceptions(self):
        # test that print options are restored even if an exception is raised
        opts = np.get_printoptions()
        try:
            with np.printoptions(precision=2, linewidth=11):
                raise ValueError
        except ValueError:
            past
        astert_equal(np.get_printoptions(), opts)

3 View Complete Implementation : logger.py
Copyright MIT License
Author : alvarob96
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

3 View Complete Implementation : logger.py
Copyright Apache License 2.0
Author : dnanexus
    def format(self, obj, indent=0):
        """ Return the formated representation of the object.
        """
        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=self.depth, indent=indent)
        if print_options:
            np.set_printoptions(**print_options)
        return out

3 View Complete Implementation : variable_logger_hook.py
Copyright Apache License 2.0
Author : google-research
  def after_run(self, run_context, run_values):
    del run_context
    original = np.get_printoptions()
    np.set_printoptions(suppress=True)
    for variable, variable_value in zip(self._variables_to_log,
                                        run_values.results):
      if not isinstance(variable_value, np.ndarray):
        continue
      variable_value = variable_value.ravel()
      logging.info('%s.mean = %s', variable.op.name, np.mean(variable_value))
      logging.info('%s.std = %s', variable.op.name, np.std(variable_value))
      if self._max_num_variable_values:
        variable_value = variable_value[:self._max_num_variable_values]
      logging.info('%s = %s', variable.op.name, variable_value)
    np.set_printoptions(**original)

3 View Complete Implementation : utils.py
Copyright MIT License
Author : HumanCompatibleAI
@contextlib.contextmanager
def printoptions(*args, **kwargs):
    original = np.get_printoptions()
    np.set_printoptions(*args, **kwargs)
    try:
        yield
    finally:
        np.set_printoptions(**original)

3 View Complete Implementation : test_findiff.py
Copyright MIT License
Author : maroba
    def test_matrix_2d(self):
        thr = np.get_printoptions()["threshold"]
        lw = np.get_printoptions()["linewidth"]
        np.set_printoptions(threshold=np.inf)
        np.set_printoptions(linewidth=500)
        x, y = [np.linspace(0, 4, 5)] * 2
        X, Y = np.meshgrid(x, y, indexing='ij')
        laplace = FinDiff(0, x[1]-x[0], 2) + FinDiff(0, y[1]-y[0], 2)
        #d = FinDiff(1, y[1]-y[0], 2)
        u = X**2 + Y**2

        mat = laplace.matrix(u.shape)

        np.testing.astert_array_almost_equal(4 * np.ones_like(X).reshape(-1), mat.dot(u.reshape(-1)))

        np.set_printoptions(threshold=thr)
        np.set_printoptions(linewidth=lw)

3 View Complete Implementation : utils.py
Copyright MIT License
Author : perslev
@contextlib.contextmanager
def print_options_context(*args, **kwargs):
    original = np.get_printoptions()
    np.set_printoptions(*args, **kwargs)
    try:
        yield
    finally:
        np.set_printoptions(**original)