numpy.get_printoptions

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.

56 Examples 7

3 Source : util.py
with MIT License
from afourast

def printoptions(*args, **kwargs):
    original = np.get_printoptions()
    np.set_printoptions(*args, **kwargs)
    yield
    np.set_printoptions(**original)


def print_prec(x, prec):

3 Source : formatter.py
with MIT License
from 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 Source : valuesummarymeter.py
with MIT License
from 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 Source : test_arrayprint.py
with MIT License
from alvarobartt

    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):
            pass
        assert_equal(np.get_printoptions(), opts)

    def test_ctx_mgr_exceptions(self):

3 Source : test_arrayprint.py
with MIT License
from alvarobartt

    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:
            pass
        assert_equal(np.get_printoptions(), opts)

    def test_ctx_mgr_as_smth(self):

3 Source : logger.py
with MIT License
from alvarobartt

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`
###############################################################################
class Logger(object):

3 Source : display.py
with MIT License
from bobzwik

def fullprint(*args, **kwargs):
    opt = np.get_printoptions()
    np.set_printoptions(threshold=np.inf)
    print(*args, **kwargs)
    np.set_printoptions(opt)


def makeFigures(params, time, pos_all, vel_all, quat_all, omega_all, euler_all, commands, wMotor_all, thrust, torque, sDes_traj, sDes_calc):

3 Source : label_composer.py
with GNU General Public License v3.0
from filby89

def printoptions(*args, **kwargs):
    original = numpy.get_printoptions()
    numpy.set_printoptions(*args, **kwargs)
    yield 
    numpy.set_printoptions(**original)


class LabelComposer(object):

3 Source : device_array.py
with Apache License 2.0
from google

  def __repr__(self):
    line_width = np.get_printoptions()["linewidth"]
    prefix = '{}('.format(self.__class__.__name__.lstrip('_'))
    s = np.array2string(self._value, prefix=prefix, suffix=',',
                        separator=', ', max_line_width=line_width)
    if self.aval is not None and self.aval.weak_type:
      dtype_str = f'dtype={self.dtype.name}, weak_type=True)'
    else:
      dtype_str = f'dtype={self.dtype.name})'
    last_line_len = len(s) - s.rfind('\n') + 1
    sep = ' '
    if last_line_len + len(dtype_str) + 1 > line_width:
      sep = ' ' * len(prefix)
    return "{}{},{}{}".format(prefix, s, sep, dtype_str)

  setattr(device_array, "__repr__", __repr__)

3 Source : variable_logger_hook.py
with Apache License 2.0
from 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 Source : utils.py
with MIT License
from HumanCompatibleAI

def printoptions(*args, **kwargs):
    original = np.get_printoptions()
    np.set_printoptions(*args, **kwargs)
    try:
        yield
    finally:
        np.set_printoptions(**original)

3 Source : util.py
with MIT License
from isarandi

def numpy_printoptions(*args, **kwargs):
    original_printoptions = np.get_printoptions()
    try:
        np.set_printoptions(*args, **kwargs)
        yield
    finally:
        np.set_printoptions(**original_printoptions)


def load_pickle(file_path):

3 Source : test_findiff.py
with MIT License
from 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.assert_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)

    def test_matrix_2d_mixed(self):

3 Source : test_findiff.py
with MIT License
from maroba

    def test_matrix_2d_nonuni(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, 2) + FinDiff(1, y, 2)
        #d = FinDiff(1, y[1]-y[0], 2)
        u = X**2 + Y**2

        mat = laplace.matrix(u.shape)

        np.testing.assert_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)

    def test_matrix_2d_mixed_nonuni(self):

3 Source : quantity.py
with GNU General Public License v3.0
from noam09

def printoptions(*args, **kwargs):
    """Numpy printoptions context manager released with version 1.15.0
    https://docs.scipy.org/doc/numpy/reference/generated/numpy.printoptions.html
    """

    opts = np.get_printoptions()
    try:
        np.set_printoptions(*args, **kwargs)
        yield np.get_printoptions()
    finally:
        np.set_printoptions(**opts)


# Workaround to bypass dynamically generated Quantity with overload method
Magnitude = TypeVar("Magnitude")

3 Source : basic_session_run_hooks.py
with MIT License
from PacktPublishing

  def _log_tensors(self, tensor_values):
    original = np.get_printoptions()
    np.set_printoptions(suppress=True)
    elapsed_secs, _ = self._timer.update_last_triggered_step(self._iter_count)
    if self._formatter:
      logging.info(self._formatter(tensor_values))
    else:
      stats = []
      for tag in self._tag_order:
        stats.append("%s = %s" % (tag, tensor_values[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 after_run(self, run_context, run_values):

3 Source : py_utils.py
with Apache License 2.0
from tensorflow

def _PrintOptions(*args, **kwargs):
  original = np.get_printoptions()
  np.set_printoptions(*args, **kwargs)
  try:
    yield
  finally:
    np.set_printoptions(**original)


def _Print(name, x):

3 Source : compute_stats.py
with Apache License 2.0
from tensorflow

  def _PrintMeanVar(self):
    m, v = self._ComputeMeanVar()
    original = np.get_printoptions()
    np.set_printoptions(threshold=np.inf)
    tf.logging.info('== Mean/variance.')
    tf.logging.info('mean = %s', m)
    tf.logging.info('var = %s', v)
    np.set_printoptions(**original)

  def Print(self):

3 Source : display.py
with MIT License
from tjards

def fullprint(*args, **kwargs):
    opt = np.get_printoptions()
    np.set_printoptions(threshold=np.inf)
    print(*args, **kwargs)
    np.set_printoptions(opt)


#def makeFigures(params, time, pos_all, vel_all, quat_all, omega_all, euler_all, commands, wMotor_all, thrust, torque, sDes_traj, sDes_calc):
def makeFigures(params, myData):

3 Source : util.py
with MIT License
from wittawatj

def fullprint(*args, **kwargs):
    "https://gist.github.com/ZGainsforth/3a306084013633c52881"
    from pprint import pprint
    import numpy

    opt = numpy.get_printoptions()
    numpy.set_printoptions(threshold="nan")
    pprint(*args, **kwargs)
    numpy.set_printoptions(**opt)


def standardize(X):

2 Source : test_formatters.py
with BSD 3-Clause "New" or "Revised" License
from holzschu

def test_precision():
    """test various values for float_precision."""
    f = PlainTextFormatter()
    assert f(pi) == repr(pi)
    f.float_precision = 0
    if numpy:
        po = numpy.get_printoptions()
        assert po["precision"] == 0
    assert f(pi) == "3"
    f.float_precision = 2
    if numpy:
        po = numpy.get_printoptions()
        assert po["precision"] == 2
    assert f(pi) == "3.14"
    f.float_precision = "%g"
    if numpy:
        po = numpy.get_printoptions()
        assert po["precision"] == 2
    assert f(pi) == "3.14159"
    f.float_precision = "%e"
    assert f(pi) == "3.141593e+00"
    f.float_precision = ""
    if numpy:
        po = numpy.get_printoptions()
        assert po["precision"] == 8
    assert f(pi) == repr(pi)


def test_bad_precision():

2 Source : array_repr.py
with BSD 2-Clause "Simplified" License
from jonathf

def to_string(
        poly: ndpoly,
        precision: Optional[float] = None,
        suppress_small: Optional[bool] = None,
) -> Union[str, List]:
    """
    Convert numpoly object to string object, or array of string objects.

    Args:
        poly:
            Polynomial object to convert to strings.
        precision:
            Floating point precision. Defaults to
            ``numpy.get_printoptions()['precision']``.
        suppress_small:
            Represent numbers "very close" to zero as zero; default is False.
            Very close is defined by precision: if the precision is 8, e.g.,
            numbers smaller (in absolute value) than 5e-9 are represented as
            zero. Defaults to ``numpy.get_printoptions()['suppress']``.

    Returns:
        If scalar, a string, or if array, numpy.array with string values.

    Examples:
        >>> q0, q1 = numpoly.variable(2)
        >>> poly = numpoly.polynomial([[1, q0**3], [q1-1, -3*q0]])
        >>> string_array = to_string(poly)
        >>> string_array
        [['1', 'q0**3'], ['q1-1', '-3*q0']]
        >>> type(string_array[0][0]) == str
        True

    """
    if precision is None:
        precision = numpy.get_printoptions()["precision"]
    if suppress_small is None:
        suppress_small = numpy.get_printoptions()["suppress"]

    if poly.shape:
        return [to_string(poly_, precision=precision,
                          suppress_small=suppress_small)
                for poly_ in poly]

    output = _to_string(poly, precision, suppress_small)
    if output:
        return "".join(output)
    return str(numpy.zeros(1, dtype=poly.dtype).item())


def _to_string(

2 Source : test_formatters.py
with MIT License
from ktraunmueller

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 test_bad_precision():

0 Source : test_arrayprint.py
with GNU General Public License v3.0
from adityaprakash-bobby

    def setup(self):
        self.oldopts = np.get_printoptions()

    def teardown(self):

0 Source : test_core.py
with GNU General Public License v3.0
from adityaprakash-bobby

    def test_str_repr_legacy(self):
        oldopts = np.get_printoptions()
        np.set_printoptions(legacy='1.13')
        try:
            a = array([0, 1, 2], mask=[False, True, False])
            assert_equal(str(a), '[0 -- 2]')
            assert_equal(repr(a), 'masked_array(data = [0 -- 2],\n'
                                  '             mask = [False  True False],\n'
                                  '       fill_value = 999999)\n')

            a = np.ma.arange(2000)
            a[1:50] = np.ma.masked
            assert_equal(
                repr(a),
                'masked_array(data = [0 -- -- ..., 1997 1998 1999],\n'
                '             mask = [False  True  True ..., False False False],\n'
                '       fill_value = 999999)\n'
            )
        finally:
            np.set_printoptions(**oldopts)

    def test_0d_unicode(self):

0 Source : _linprog.py
with GNU General Public License v3.0
from adityaprakash-bobby

def linprog_verbose_callback(xk, **kwargs):
    """
    A sample callback function demonstrating the linprog callback interface.
    This callback produces detailed output to sys.stdout before each iteration
    and after the final iteration of the simplex algorithm.

    Parameters
    ----------
    xk : array_like
        The current solution vector.
    **kwargs : dict
        A dictionary containing the following parameters:

        tableau : array_like
            The current tableau of the simplex algorithm.
            Its structure is defined in _solve_simplex.
        phase : int
            The current Phase of the simplex algorithm (1 or 2)
        nit : int
            The current iteration number.
        pivot : tuple(int, int)
            The index of the tableau selected as the next pivot,
            or nan if no pivot exists
        basis : array(int)
            A list of the current basic variables.
            Each element contains the name of a basic variable and its value.
        complete : bool
            True if the simplex algorithm has completed
            (and this is the final call to callback), otherwise False.
    """
    tableau = kwargs["tableau"]
    nit = kwargs["nit"]
    pivrow, pivcol = kwargs["pivot"]
    phase = kwargs["phase"]
    basis = kwargs["basis"]
    complete = kwargs["complete"]

    saved_printoptions = np.get_printoptions()
    np.set_printoptions(linewidth=500,
                        formatter={'float': lambda x: "{0: 12.4f}".format(x)})
    if complete:
        print("--------- Iteration Complete - Phase {0:d} -------\n".format(phase))
        print("Tableau:")
    elif nit == 0:
        print("--------- Initial Tableau - Phase {0:d} ----------\n".format(phase))

    else:
        print("--------- Iteration {0:d}  - Phase {1:d} --------\n".format(nit, phase))
        print("Tableau:")

    if nit >= 0:
        print("" + str(tableau) + "\n")
        if not complete:
            print("Pivot Element: T[{0:.0f}, {1:.0f}]\n".format(pivrow, pivcol))
        print("Basic Variables:", basis)
        print()
        print("Current Solution:")
        print("x = ", xk)
        print()
        print("Current Objective Value:")
        print("f = ", -tableau[-1, -1])
        print()
    np.set_printoptions(**saved_printoptions)


def linprog_terse_callback(xk, **kwargs):

0 Source : arrayprint.py
with MIT License
from alvarobartt

def printoptions(*args, **kwargs):
    """Context manager for setting print options.

    Set print options for the scope of the `with` block, and restore the old
    options at the end. See `set_printoptions` for the full description of
    available options.

    Examples
    --------

    >>> with np.printoptions(precision=2):
    ...     print(np.array([2.0])) / 3
    [0.67]

    The `as`-clause of the `with`-statement gives the current print options:

    >>> with np.printoptions(precision=2) as opts:
    ...      assert_equal(opts, np.get_printoptions())

    See Also
    --------
    set_printoptions, get_printoptions

    """
    opts = np.get_printoptions()
    try:
        np.set_printoptions(*args, **kwargs)
        yield np.get_printoptions()
    finally:
        np.set_printoptions(**opts)


def _leading_trailing(a, edgeitems, index=()):

0 Source : base.py
with MIT License
from alvarobartt

def _pprint(params, offset=0, printer=repr):
    """Pretty print the dictionary 'params'

    Parameters
    ----------
    params : dict
        The dictionary to pretty print

    offset : int
        The offset in characters to add at the begin of each line.

    printer : callable
        The function to convert entries to strings, typically
        the builtin str or repr

    """
    # Do a multi-line justified repr:
    options = np.get_printoptions()
    np.set_printoptions(precision=5, threshold=64, edgeitems=2)
    params_list = list()
    this_line_length = offset
    line_sep = ',\n' + (1 + offset // 2) * ' '
    for i, (k, v) in enumerate(sorted(six.iteritems(params))):
        if type(v) is float:
            # use str for representing floating point numbers
            # this way we get consistent representation across
            # architectures and versions.
            this_repr = '%s=%s' % (k, str(v))
        else:
            # use repr of the rest
            this_repr = '%s=%s' % (k, printer(v))
        if len(this_repr) > 500:
            this_repr = this_repr[:300] + '...' + this_repr[-100:]
        if i > 0:
            if (this_line_length + len(this_repr) >= 75 or '\n' in this_repr):
                params_list.append(line_sep)
                this_line_length = len(line_sep)
            else:
                params_list.append(', ')
                this_line_length += 2
        params_list.append(this_repr)
        this_line_length += len(this_repr)

    np.set_printoptions(**options)
    lines = ''.join(params_list)
    # Strip trailing space to avoid nightmare in doctests
    lines = '\n'.join(l.rstrip(' ') for l in lines.split('\n'))
    return lines


###############################################################################
class BaseEstimator(object):

0 Source : inspect_checkpoint.py
with Apache License 2.0
from balancap

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})


def main(unused_argv):

0 Source : dump2module.py
with MIT License
from birforce

    def save(self, what=None, filename=None, header=True, useinstance=True,
             comment=None, print_options=None):
        '''write attributes of this instance to python module given by filename

        Parameters
        ----------
        what : list or None
            list of attributes that are added to the module. If None (default)
            then all attributes in __dict__ that do not start with an underline
            will be saved.
        filename : string
            specifies filename with path. If the file does not exist, it will be
            created. If the file is already exists, then the new data will be
            appended to the file.
        header : bool
            If true, then the imports of the module and the class definition are
            written before writing the data.
        useinstance : bool
            If true, then the data in the module are attached to an instance of a
            holder class. If false, then each array will be saved as separate
            variable.
        comment : string
            If comment is not empty then this string will be attached as a
            description comment to the data instance in the saved module.
        print_options : dict or None
            The print_options for the numpy arrays will be updated with this.
            see notes

        Notes
        -----
        The content of an numpy array are written using repr, which can be
        controlled with the np.set_printoptions. The numpy default is updated
        with:  precision=20, linewidth=100, nanstr='nan', infstr='inf'

        This should provide enough precision for double floating point numbers.
        If one array has more than 1000 elements, then threshold should be
        overwritten by the user, see keyword argument print_options.
        '''

        print_opt_old = np.get_printoptions()
        print_opt = dict(precision=20, linewidth=100, nanstr='nan',
                            infstr='inf')
        if print_options:
            print_opt.update(print_options)
        np.set_printoptions(**print_opt)
        #precision corrects for non-scientific notation
        if what is None:
            what = (i for i in self.__dict__ if i[0] != '_')
        if header:
            txt = ['import numpy as np\n'
                   'from numpy import array, rec, inf, nan\n\n']
            if useinstance:
                txt.append('class Holder(object):\n    pass\n\n')
        else:
            txt = []

        if useinstance:
            txt.append('%s = Holder()' % self.name)
            prefix = '%s.' % self.name
        else:
            prefix = ''

        if not comment is None:
            txt.append("%scomment = '%s'" % (prefix, comment))

        for x in what:
            txt.append('%s%s = %s' % (prefix, x, repr(getattr(self,x))))
        txt.extend(['','']) #add empty lines at end
        if not filename is None:
            file(filename, 'a+').write('\n'.join(txt))
        np.set_printoptions(**print_opt_old)
        self._filename = filename
        self._useinstance = useinstance
        self._what = what
        return txt

    def verify(self):

0 Source : _linprog.py
with MIT License
from buds-lab

def linprog_verbose_callback(res):
    """
    A sample callback function demonstrating the linprog callback interface.
    This callback produces detailed output to sys.stdout before each iteration
    and after the final iteration of the simplex algorithm.

    Parameters
    ----------
    res : A `scipy.optimize.OptimizeResult` consisting of the following fields:

        x : 1D array
            The independent variable vector which optimizes the linear
            programming problem.
        fun : float
            Value of the objective function.
        success : bool
            True if the algorithm succeeded in finding an optimal solution.
        slack : 1D array
            The values of the slack variables. Each slack variable corresponds
            to an inequality constraint. If the slack is zero, then the
            corresponding constraint is active.
        con : 1D array
            The (nominally zero) residuals of the equality constraints, that is,
            ``b - A_eq @ x``
        phase : int
            The phase of the optimization being executed. In phase 1 a basic
            feasible solution is sought and the T has an additional row
            representing an alternate objective function.
        status : int
            An integer representing the exit status of the optimization::

                 0 : Optimization terminated successfully
                 1 : Iteration limit reached
                 2 : Problem appears to be infeasible
                 3 : Problem appears to be unbounded
                 4 : Serious numerical difficulties encountered

        nit : int
            The number of iterations performed.
        message : str
            A string descriptor of the exit status of the optimization.
    """
    x = res['x']
    fun = res['fun']
    phase = res['phase']
    status = res['status']
    nit = res['nit']
    message = res['message']
    complete = res['complete']

    saved_printoptions = np.get_printoptions()
    np.set_printoptions(linewidth=500,
                        formatter={'float': lambda x: "{0: 12.4f}".format(x)})
    if status:
        print('--------- Simplex Early Exit -------\n'.format(nit))
        print('The simplex method exited early with status {0:d}'.format(status))
        print(message)
    elif complete:
        print('--------- Simplex Complete --------\n')
        print('Iterations required: {}'.format(nit))
    else:
        print('--------- Iteration {0:d}  ---------\n'.format(nit))

    if nit > 0:
        if phase == 1:
            print('Current Pseudo-Objective Value:')
        else:
            print('Current Objective Value:')
        print('f = ', fun)
        print()
        print('Current Solution Vector:')
        print('x = ', x)
        print()

    np.set_printoptions(**saved_printoptions)


def linprog_terse_callback(res):

0 Source : arrayprint.py
with Apache License 2.0
from dashanji

def printoptions(*args, **kwargs):
    """Context manager for setting print options.

    Set print options for the scope of the `with` block, and restore the old
    options at the end. See `set_printoptions` for the full description of
    available options.

    Examples
    --------

    >>> from numpy.testing import assert_equal
    >>> with np.printoptions(precision=2):
    ...     np.array([2.0]) / 3
    array([0.67])

    The `as`-clause of the `with`-statement gives the current print options:

    >>> with np.printoptions(precision=2) as opts:
    ...      assert_equal(opts, np.get_printoptions())

    See Also
    --------
    set_printoptions, get_printoptions

    """
    opts = np.get_printoptions()
    try:
        np.set_printoptions(*args, **kwargs)
        yield np.get_printoptions()
    finally:
        np.set_printoptions(**opts)


def _leading_trailing(a, edgeitems, index=()):

0 Source : _linprog.py
with Apache License 2.0
from dashanji

def linprog_verbose_callback(res):
    """
    A sample callback function demonstrating the linprog callback interface.
    This callback produces detailed output to sys.stdout before each iteration
    and after the final iteration of the simplex algorithm.

    Parameters
    ----------
    res : A `scipy.optimize.OptimizeResult` consisting of the following fields:

        x : 1-D array
            The independent variable vector which optimizes the linear
            programming problem.
        fun : float
            Value of the objective function.
        success : bool
            True if the algorithm succeeded in finding an optimal solution.
        slack : 1-D array
            The values of the slack variables. Each slack variable corresponds
            to an inequality constraint. If the slack is zero, then the
            corresponding constraint is active.
        con : 1-D array
            The (nominally zero) residuals of the equality constraints, that is,
            ``b - A_eq @ x``
        phase : int
            The phase of the optimization being executed. In phase 1 a basic
            feasible solution is sought and the T has an additional row
            representing an alternate objective function.
        status : int
            An integer representing the exit status of the optimization::

                 0 : Optimization terminated successfully
                 1 : Iteration limit reached
                 2 : Problem appears to be infeasible
                 3 : Problem appears to be unbounded
                 4 : Serious numerical difficulties encountered

        nit : int
            The number of iterations performed.
        message : str
            A string descriptor of the exit status of the optimization.
    """
    x = res['x']
    fun = res['fun']
    phase = res['phase']
    status = res['status']
    nit = res['nit']
    message = res['message']
    complete = res['complete']

    saved_printoptions = np.get_printoptions()
    np.set_printoptions(linewidth=500,
                        formatter={'float': lambda x: "{0: 12.4f}".format(x)})
    if status:
        print('--------- Simplex Early Exit -------\n'.format(nit))
        print('The simplex method exited early with status {0:d}'.format(status))
        print(message)
    elif complete:
        print('--------- Simplex Complete --------\n')
        print('Iterations required: {}'.format(nit))
    else:
        print('--------- Iteration {0:d}  ---------\n'.format(nit))

    if nit > 0:
        if phase == 1:
            print('Current Pseudo-Objective Value:')
        else:
            print('Current Objective Value:')
        print('f = ', fun)
        print()
        print('Current Solution Vector:')
        print('x = ', x)
        print()

    np.set_printoptions(**saved_printoptions)


def linprog_terse_callback(res):

0 Source : base.py
with Apache License 2.0
from dashanji

def _pprint(params, offset=0, printer=repr):
    """Pretty print the dictionary 'params'

    Parameters
    ----------
    params : dict
        The dictionary to pretty print

    offset : int, default=0
        The offset in characters to add at the begin of each line.

    printer : callable, default=repr
        The function to convert entries to strings, typically
        the builtin str or repr

    """
    # Do a multi-line justified repr:
    options = np.get_printoptions()
    np.set_printoptions(precision=5, threshold=64, edgeitems=2)
    params_list = list()
    this_line_length = offset
    line_sep = ',\n' + (1 + offset // 2) * ' '
    for i, (k, v) in enumerate(sorted(params.items())):
        if type(v) is float:
            # use str for representing floating point numbers
            # this way we get consistent representation across
            # architectures and versions.
            this_repr = '%s=%s' % (k, str(v))
        else:
            # use repr of the rest
            this_repr = '%s=%s' % (k, printer(v))
        if len(this_repr) > 500:
            this_repr = this_repr[:300] + '...' + this_repr[-100:]
        if i > 0:
            if (this_line_length + len(this_repr) >= 75 or '\n' in this_repr):
                params_list.append(line_sep)
                this_line_length = len(line_sep)
            else:
                params_list.append(', ')
                this_line_length += 2
        params_list.append(this_repr)
        this_line_length += len(this_repr)

    np.set_printoptions(**options)
    lines = ''.join(params_list)
    # Strip trailing space to avoid nightmare in doctests
    lines = '\n'.join(l.rstrip(' ') for l in lines.split('\n'))
    return lines


class BaseEstimator:

0 Source : CollectiveBase.py
with Apache License 2.0
from datamllab

def _pprint(params, offset=0, printer=repr): # pragma: no cover
    # noinspection PyPep8
    """Pretty print the dictionary 'params'

    See http://scikit-learn.org/stable/modules/generated/sklearn.base.BaseEstimator.html
    and sklearn/base.py for more information.

    :param params: The dictionary to pretty print
    :type params: dict

    :param offset: The offset in characters to add at the begin of each line.
    :type offset: int

    :param printer: The function to convert entries to strings, typically
        the builtin str or repr
    :type printer: callable

    :return: None
    """

    # Do a multi-line justified repr:
    options = np.get_printoptions()
    np.set_printoptions(precision=5, threshold=64, edgeitems=2)
    params_list = list()
    this_line_length = offset
    line_sep = ',\n' + (1 + offset // 2) * ' '
    for i, (k, v) in enumerate(sorted(params.items())):
        if type(v) is float:
            # use str for representing floating point numbers
            # this way we get consistent representation across
            # architectures and versions.
            this_repr = '%s=%s' % (k, str(v))
        else:
            # use repr of the rest
            this_repr = '%s=%s' % (k, printer(v))
        if len(this_repr) > 500:
            this_repr = this_repr[:300] + '...' + this_repr[-100:]
        if i > 0:
            if this_line_length + len(this_repr) >= 75 or '\n' in this_repr:
                params_list.append(line_sep)
                this_line_length = len(line_sep)
            else:
                params_list.append(', ')
                this_line_length += 2
        params_list.append(this_repr)
        this_line_length += len(this_repr)

    np.set_printoptions(**options)
    lines = ''.join(params_list)
    # Strip trailing space to avoid nightmare in doctests
    lines = '\n'.join(l.rstrip(' ') for l in lines.split('\n'))
    return lines


class CollectiveBaseDetector(metaclass=ABCMeta):

0 Source : _polybase.py
with GNU General Public License v3.0
from dnn-security

    def _generate_string(self, term_method):
        """
        Generate the full string representation of the polynomial, using
        ``term_method`` to generate each polynomial term.
        """
        # Get configuration for line breaks
        linewidth = np.get_printoptions().get('linewidth', 75)
        if linewidth   <   1:
            linewidth = 1
        out = f"{self.coef[0]}"
        for i, coef in enumerate(self.coef[1:]):
            out += " "
            power = str(i + 1)
            # Polynomial coefficient
            # The coefficient array can be an object array with elements that
            # will raise a TypeError with >= 0 (e.g. strings or Python
            # complex). In this case, represent the coeficient as-is.
            try:
                if coef >= 0:
                    next_term = f"+ {coef}"
                else:
                    next_term = f"- {-coef}"
            except TypeError:
                next_term = f"+ {coef}"
            # Polynomial term
            next_term += term_method(power, "x")
            # Length of the current line with next term added
            line_len = len(out.split('\n')[-1]) + len(next_term)
            # If not the last term in the polynomial, it will be two
            # characters longer due to the +/- with the next term
            if i  <  len(self.coef[1:]) - 1:
                line_len += 2
            # Handle linebreaking
            if line_len >= linewidth:
                next_term = next_term.replace(" ", "\n", 1)
            out += next_term
        return out

    @classmethod

0 Source : regular_surface.py
with GNU Lesser General Public License v3.0
from equinor

    def generate_hash(self, hashmethod="md5"):
        """Return a unique hash ID for current instance.

        See :meth:`~xtgeo.common.sys.generic_hash()` for documentation.

        .. versionadded:: 2.14
        """
        required = (
            "ncol",
            "nrow",
            "xori",
            "yori",
            "xinc",
            "yinc",
            "yflip",
            "rotation",
            "values",
        )
        opt = np.get_printoptions()
        np.set_printoptions(threshold=sys.maxsize)
        gid = ""
        for req in required:
            gid += f"{getattr(self, '_' + req)}"

        np.set_printoptions(**opt)
        hash_ = xtgeosys.generic_hash(gid, hashmethod=hashmethod)
        return hash_

    def describe(self, flush=True):

0 Source : base.py
with GNU General Public License v3.0
from gustavowillam

def _pprint(params, offset=0, printer=repr):
    """Pretty print the dictionary 'params'

    Parameters
    ----------
    params : dict
        The dictionary to pretty print

    offset : int
        The offset in characters to add at the begin of each line.

    printer : callable
        The function to convert entries to strings, typically
        the builtin str or repr

    """
    # Do a multi-line justified repr:
    options = np.get_printoptions()
    np.set_printoptions(precision=5, threshold=64, edgeitems=2)
    params_list = list()
    this_line_length = offset
    line_sep = ',\n' + (1 + offset // 2) * ' '
    for i, (k, v) in enumerate(sorted(params.items())):
        if type(v) is float:
            # use str for representing floating point numbers
            # this way we get consistent representation across
            # architectures and versions.
            this_repr = '%s=%s' % (k, str(v))
        else:
            # use repr of the rest
            this_repr = '%s=%s' % (k, printer(v))
        if len(this_repr) > 500:
            this_repr = this_repr[:300] + '...' + this_repr[-100:]
        if i > 0:
            if (this_line_length + len(this_repr) >= 75 or '\n' in this_repr):
                params_list.append(line_sep)
                this_line_length = len(line_sep)
            else:
                params_list.append(', ')
                this_line_length += 2
        params_list.append(this_repr)
        this_line_length += len(this_repr)

    np.set_printoptions(**options)
    lines = ''.join(params_list)
    # Strip trailing space to avoid nightmare in doctests
    lines = '\n'.join(l.rstrip(' ') for l in lines.split('\n'))
    return lines


class BaseEstimator:

0 Source : _linprog.py
with GNU General Public License v3.0
from HHHHhgqcdxhg

def linprog_verbose_callback(res):
    """
    A sample callback function demonstrating the linprog callback interface.
    This callback produces detailed output to sys.stdout before each iteration
    and after the final iteration of the simplex algorithm.

    Parameters
    ----------
    res : A `scipy.optimize.OptimizeResult` consisting of the following fields:

        x : 1D array
            The independent variable vector which optimizes the linear
            programming problem.
        fun : float
            Value of the objective function.
        success : bool
            True if the algorithm succeeded in finding an optimal solution.
        slack : 1D array
            The values of the slack variables. Each slack variable corresponds
            to an inequality constraint. If the slack is zero, then the
            corresponding constraint is active.
        con : 1D array
            The (nominally zero) residuals of the equality constraints, that is,
            ``b - A_eq @ x``
        phase : int
            The phase of the optimization being executed. In phase 1 a basic
            feasible solution is sought and the T has an additional row
            representing an alternate objective function.
        status : int
            An integer representing the exit status of the optimization::

                 0 : Optimization terminated successfully
                 1 : Iteration limit reached
                 2 : Problem appears to be infeasible
                 3 : Problem appears to be unbounded
                 4 : Serious numerical difficulties encountered

        nit : int
            The number of iterations performed.
        message : str
            A string descriptor of the exit status of the optimization.
    """
    x = res['x']
    fun = res['fun']
    success = res['success']
    phase = res['phase']
    status = res['status']
    nit = res['nit']
    message = res['message']
    complete = res['complete']

    saved_printoptions = np.get_printoptions()
    np.set_printoptions(linewidth=500,
                        formatter={'float': lambda x: "{0: 12.4f}".format(x)})
    if status:
        print('--------- Simplex Early Exit -------\n'.format(nit))
        print('The simplex method exited early with status {0:d}'.format(status))
        print(message)
    elif complete:
        print('--------- Simplex Complete --------\n')
        print('Iterations required: {}'.format(nit))
    else:
        print('--------- Iteration {0:d}  ---------\n'.format(nit))

    if nit > 0:
        if phase == 1:
            print('Current Pseudo-Objective Value:')
        else:
            print('Current Objective Value:')
        print('f = ', fun)
        print()
        print('Current Solution Vector:')
        print('x = ', x)
        print()

    np.set_printoptions(**saved_printoptions)


def linprog_terse_callback(res):

0 Source : quantity.py
with BSD 3-Clause "New" or "Revised" License
from holzschu

    def to_string(self, unit=None, precision=None, format=None, subfmt=None):
        """
        Generate a string representation of the quantity and its unit.

        The behavior of this function can be altered via the
        `numpy.set_printoptions` function and its various keywords.  The
        exception to this is the ``threshold`` keyword, which is controlled via
        the ``[units.quantity]`` configuration item ``latex_array_threshold``.
        This is treated separately because the numpy default of 1000 is too big
        for most browsers to handle.

        Parameters
        ----------
        unit : `~astropy.units.UnitBase`, optional
            Specifies the unit.  If not provided,
            the unit used to initialize the quantity will be used.

        precision : numeric, optional
            The level of decimal precision. If `None`, or not provided,
            it will be determined from NumPy print options.

        format : str, optional
            The format of the result. If not provided, an unadorned
            string is returned. Supported values are:

            - 'latex': Return a LaTeX-formatted string

        subfmt : str, optional
            Subformat of the result. For the moment,
            only used for format="latex". Supported values are:

            - 'inline': Use ``$ ... $`` as delimiters.

            - 'display': Use ``$\\displaystyle ... $`` as delimiters.

        Returns
        -------
        lstr
            A string with the contents of this Quantity
        """
        if unit is not None and unit != self.unit:
            return self.to(unit).to_string(
                unit=None, precision=precision, format=format, subfmt=subfmt)

        formats = {
            None: None,
            "latex": {
                None: ("$", "$"),
                "inline": ("$", "$"),
                "display": (r"$\displaystyle ", r"$"),
            },
        }

        if format not in formats:
            raise ValueError(f"Unknown format '{format}'")
        elif format is None:
            return f'{self.value}{self._unitstr:s}'

        # else, for the moment we assume format="latex"

        # need to do try/finally because "threshold" cannot be overridden
        # with array2string
        pops = np.get_printoptions()

        format_spec = '.{}g'.format(
            precision if precision is not None else pops['precision'])

        def float_formatter(value):
            return Latex.format_exponential_notation(value,
                                                     format_spec=format_spec)

        def complex_formatter(value):
            return '({}{}i)'.format(
                Latex.format_exponential_notation(value.real,
                                                  format_spec=format_spec),
                Latex.format_exponential_notation(value.imag,
                                                  format_spec='+' + format_spec))

        try:
            formatter = {'float_kind': float_formatter,
                         'complex_kind': complex_formatter}
            if conf.latex_array_threshold > -1:
                np.set_printoptions(threshold=conf.latex_array_threshold,
                                    formatter=formatter)

            # the view is needed for the scalar case - value might be float
            latex_value = np.array2string(
                self.view(np.ndarray),
                max_line_width=np.inf, separator=',~')

            latex_value = latex_value.replace('...', r'\dots')
        finally:
            np.set_printoptions(**pops)

        # Format unit
        # [1:-1] strips the '$' on either side needed for math mode
        latex_unit = (self.unit._repr_latex_()[1:-1]  # note this is unicode
                      if self.unit is not None
                      else _UNIT_NOT_INITIALISED)

        delimiter_left, delimiter_right = formats[format][subfmt]

        return r'{left}{0} \; {1}{right}'.format(latex_value, latex_unit,
                                                 left=delimiter_left,
                                                 right=delimiter_right)

    def __str__(self):

0 Source : suite_carla.py
with Apache License 2.0
from HorizonRobotics

    def render(self, mode):
        """Render the simulation.

        Args:
            mode (str): one of ['rgb_array', 'human']
        Returns:
            one of the following:
                - None: if mode is 'human'
                - np.ndarray: the image of shape [height, width, channeles] if
                    mode is 'rgb_array'
        """
        import pygame
        if self._surface is None:
            pygame.init()
            pygame.font.init()
            self._clock = pygame.time.Clock()
            if self._camera_sensor:
                height, width = self._camera_sensor.observation_spec(
                ).shape[1:3]
                height, width = get_scaled_image_size(height, width)
            else:
                height = MINIMUM_RENDER_HEIGHT
                width = MINIMUM_RENDER_WIDTH
            if mode == 'human':
                self._surface = pygame.display.set_mode(
                    (width, height), pygame.HWSURFACE | pygame.DOUBLEBUF)
            else:
                self._surface = pygame.Surface((width, height))

        if mode == 'human':
            self._clock.tick_busy_loop(1000)

        if self._camera_sensor:
            self._camera_sensor.render(self._surface)
        obs = self._current_time_step.observation
        env_info = self._current_time_step.env_info
        np_precision = np.get_printoptions()['precision']
        np.set_printoptions(precision=1)
        info_text = [
            'FPS: %6.2f' % self._clock.get_fps(),
            'GPS:  (%7.4f, %8.4f, %5.2f)' % tuple(obs['gnss'].tolist()) \
                                if 'gnss' in obs.keys() else '',
            'Goal: (%7.1f, %8.1f, %5.1f)' % tuple(obs['goal'].tolist()) \
                                if 'goal' in obs.keys() else '',
            'Ahead: (%7.1f, %8.1f, %5.1f)' % tuple(
                obs['navigation'][2].tolist()) \
                                if 'navigation' in obs.keys() else '',
            'Distance: %7.2f' % np.linalg.norm(obs['goal']) \
                                if 'goal' in obs.keys() else '',
            'Velocity: (%4.1f, %4.1f, %4.1f) m/s' % tuple(
                    obs['velocity'].tolist()) \
                                if 'velocity' in obs.keys() else '',
            'Acceleration: (%4.1f, %4.1f, %4.1f)' % tuple(
                    obs['imu'][0:3].tolist()) \
                                if 'imu' in obs.keys() else '',
            'Compass: %5.1f' % math.degrees(float(obs['imu'][6])) \
                                if 'imu' in obs.keys() else '',
            'Throttle: %4.2f' % self._control.throttle,
            'Brake:    %4.2f' % self._control.brake,
            'Steer:    %4.2f' % self._control.steer,
            'Reverse:  %4s' % self._control.reverse,
            'Reward: (%s)' % self._current_time_step.reward,
            'Route Length: %4.2f m' % self._route_length,
            'Speed Limit: %4.2f m/s' % self._speed_limit,
            'Red light zone: %1d' % (self._prev_encountered_red_light_id != None),
            'Red light violation: %1d' % env_info['red_light_violated'],
            'Red light dist: %4.2f' % self._prev_encountered_red_light_dist,
        ]
        info_text = [info for info in info_text if info != '']
        np.set_printoptions(precision=np_precision)
        self._draw_text(info_text)

        if mode == 'human':
            pygame.display.flip()
        elif mode == 'rgb_array':
            if self._camera_sensor is not None:
                # (x, y, c) => (y, x, c)
                rgb_img = pygame.surfarray.array3d(self._surface).swapaxes(
                    0, 1)

                if 'navigation' in obs.keys() and self._render_waypoints:
                    # index of waypoint to be rendered
                    waypoint_index = np.arange(2, 5)
                    nav_traj = obs['navigation'][waypoint_index]
                    self._draw_ego_traj_on_image(
                        nav_traj,
                        rgb_img,
                        camera_sensor=self._camera_sensor,
                        color=(0, 255, 0),
                        size=5,
                        zero_world_z=True,
                        interp_num=500)
            else:
                rgb_img = None

            if self._bev_sensor is not None:
                bev_img = self._bev_sensor.render()
                if rgb_img is not None:
                    concat_img = np.zeros(
                        (max(rgb_img.shape[0], bev_img.shape[0]),
                         rgb_img.shape[1] + bev_img.shape[1], 3), np.uint8)
                    concat_img[:rgb_img.shape[0], :rgb_img.shape[1]] = rgb_img
                    concat_img[:bev_img.shape[0], -bev_img.shape[1]:] = bev_img
                    rgb_img = concat_img
                else:
                    rgb_img = bev_img

            return rgb_img
        else:
            raise ValueError("Unsupported render mode: %s" % mode)

    def _draw_ego_traj_on_image(self,

0 Source : summarize.py
with MIT License
from jdmoorman

def summarize(tmplt, world, candidates, alert_missing=True):
    cand_counts = candidates.sum(axis=1)

    # Nodes that have only one candidate
    identified = tmplt.nodes[cand_counts==1]
    n_found = len(identified)

    # Assuming ground truth nodes have same names, get the nodes for which
    # ground truth identity is not a candidate
    missing_ground_truth = [node for idx, node in enumerate(tmplt.nodes)
                            if node not in world.nodes[candidates[idx]]]
    n_missing = len(missing_ground_truth)

    # Use number of candidates to decide the order to print the summaries
    def key_func(node, cand_counts=cand_counts):
        return (-cand_counts[tmplt.node_idxs[node]], -tmplt.node_idxs[node])

    # TODO: if multiple nodes have the same candidates, condense them
    for node in sorted(tmplt.nodes, key=key_func):
        cands = world.nodes[candidates[tmplt.node_idxs[node]]]
        n_cands = len(cands)

        if n_cands == 1:
            continue

        # TODO: abstract out the getting and setting before and after
        print_opts = np.get_printoptions()
        np.set_printoptions(threshold=10, edgeitems=6)
        print(node, "has", n_cands, "candidates:", cands)
        np.set_printoptions(**print_opts)

    if n_found:
        print(n_found, "template nodes have 1 candidate:", identified)

    # This message is useful for debugging datasets for which you have
    # a ground truth signal
    if n_missing > 0 and alert_missing:
        print(n_missing, "nodes are missing ground truth candidate:",
              missing_ground_truth)

0 Source : base.py
with MIT License
from jingw2

def _pprint(params, offset=0, printer=repr):
    """Pretty print the dictionary 'params'

    Parameters
    ----------
    params : dict
        The dictionary to pretty print

    offset : int
        The offset in characters to add at the begin of each line.

    printer : callable
        The function to convert entries to strings, typically
        the builtin str or repr

    """
    # Do a multi-line justified repr:
    options = np.get_printoptions()
    np.set_printoptions(precision=5, threshold=64, edgeitems=2)
    params_list = list()
    this_line_length = offset
    line_sep = ',\n' + (1 + offset // 2) * ' '
    for i, (k, v) in enumerate(sorted(six.iteritems(params))):
        if type(v) is float:
            # use str for representing floating point numbers
            # this way we get consistent representation across
            # architectures and versions.
            this_repr = '%s=%s' % (k, str(v))
        else:
            # use repr of the rest
            this_repr = '%s=%s' % (k, printer(v))
        if len(this_repr) > 500:
            this_repr = this_repr[:300] + '...' + this_repr[-100:]
        if i > 0:
            if (this_line_length + len(this_repr) >= 75 or '\n' in this_repr):
                params_list.append(line_sep)
                this_line_length = len(line_sep)
            else:
                params_list.append(', ')
                this_line_length += 2
        params_list.append(this_repr)
        this_line_length += len(this_repr)

    np.set_printoptions(**options)
    lines = ''.join(params_list)
    # Strip trailing space to avoid nightmare in doctests
    lines = '\n'.join(l.rstrip(' ') for l in lines.split('\n'))
    return lines

0 Source : test_arrayprint.py
with MIT License
from ktraunmueller

    def setUp(self):
        self.oldopts = np.get_printoptions()

    def tearDown(self):

0 Source : rnn.py
with MIT License
from limbo018

    def train(self, params, train_x, train_y, test_x, test_y):

        #optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.learning_rate)
        optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
        if params.max_grad_norm > 0: 
            print("clip gradients")
            tvars = tf.trainable_variables()
            grads, _ = tf.clip_by_global_norm(tf.gradients(self.loss_op, tvars), params.max_grad_norm)
            train_op = optimizer.apply_gradients(
                    zip(grads, tvars),
                    global_step=tf.train.get_or_create_global_step())
        else: 
            train_op = optimizer.minimize(self.loss_op)

        if params.compute_initial_state_grad: 
            initial_state_grads = []
            if params.regression_flag: 
                for gi in range(0, params.output_size, 20): 
                    print gi 
                    if isinstance(self.initial_state, tf.contrib.rnn.LSTMStateTuple): 
                        #initial_state_grads = [tf.gradients(self.loss_op, self.initial_state.h), tf.gradients(self.loss_op, self.initial_state.c)]
                        initial_state_grads.append([tf.gradients(tf.reduce_mean(tf.pow(self.pred[:, gi:min(gi+20, params.output_size)]-self.y[:, gi:min(gi+20, params.output_size)], 2)), self.initial_state.h), tf.gradients(tf.reduce_mean(tf.pow(self.pred[:, gi:min(gi+20, params.output_size)]-self.y[:, gi:min(gi+20, params.output_size)], 2)), self.initial_state.c)])
                    else:
                        #initial_state_grads = tf.gradients(self.loss_op, self.initial_state)
                        initial_state_grads.append(tf.gradients(tf.reduce_mean(tf.pow(self.pred[:, gi:min(gi+20, params.output_size)]-self.y[:, gi:min(gi+20, params.output_size)], 2)), self.initial_state))
            else:
                print("gradients for regression")
                if isinstance(self.initial_state, tf.contrib.rnn.LSTMStateTuple): 
                    #initial_state_grads = [tf.gradients(self.loss_op, self.initial_state.h), tf.gradients(self.loss_op, self.initial_state.c)]
                    initial_state_grads = tf.gradients(self.loss_op, tf.trainable_variables())
                else:
                    initial_state_grads = tf.gradients(self.loss_op, self.initial_state)

        # Initialize the variables (i.e. assign their default value)
        init = tf.global_variables_initializer()

        # Start training 
        self.session.run(init)

        if params.compute_initial_state_grad: 
            initial_weights = self.session.run(tf.trainable_variables())
            print "initial_weights shape = ", len(initial_weights)
            opt = np.get_printoptions()
            np.set_printoptions(threshold='nan')
            for index, weights in enumerate(initial_weights): 
                print "initial_weights[%d] shape = %s" % (index, weights.shape)
                print weights
            np.set_printoptions(**opt)

        # only initialize if not train 
        if not params.train_flag: 
            print("model not trained")
            return None, None 

        train_error = []
        test_error = [] 
        iterations = 0 
        num_batches = math.ceil(len(train_x)/float(params.batch_size))
        for epoch in range(params.num_epochs): 
            if epoch == 0: 
                train_error.append(self.validate(train_x, train_y, batch_size=self.validate_batch_size))
                test_error.append(self.validate(test_x, test_y, batch_size=self.validate_batch_size))
                print("Epoch %d, iterations = %d, training %s = %.6f, testing %s = %.6f" % (-1, iterations, self.metric, train_error[-1], self.metric, test_error[-1]))

            # permute batches 
            perm = np.random.permutation(len(train_x))

            # run on batches 
            batch_index = 0 
            for batch_begin in range(0, len(train_x), params.batch_size): 
                # get batch x and y 
                batch_x = train_x[perm[batch_begin:min(batch_begin+params.batch_size, len(train_x))]]
                batch_y = train_y[perm[batch_begin:min(batch_begin+params.batch_size, len(train_x))]]
                # reduce learning rate every 1000 iterations
                learning_rate = params.initial_learning_rate*math.pow(params.lr_decay, iterations//1000)
                feed_dict = {self.x: batch_x, 
                        self.y: batch_y, 
                        self.train_flag: True, 
                        self.learning_rate: learning_rate, 
                        self.dropout_keep_rate: params.dropout_keep_rate}
                # Run optimization op (backprop)
                if params.compute_initial_state_grad and epoch in [0, 1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90] and batch_index == 0: 
                    initial_state_grads_values, _ = self.session.run([initial_state_grads, train_op], feed_dict=feed_dict)
                    for gi in range(len(initial_state_grads_values)): 
                        initial_state_grads_value = np.array(initial_state_grads_values[gi]).ravel()
                        #print "[%d]" % (gi)
                        #print initial_state_grads_value
                        l1_norm = np.linalg.norm(initial_state_grads_value, ord=1)
                        l2_norm = np.linalg.norm(initial_state_grads_value, ord=None)
                        linf_norm = np.linalg.norm(initial_state_grads_value, ord=np.inf)
                        lninf_norm = np.linalg.norm(initial_state_grads_value, ord=-np.inf)
                        print "initial_state_grads_values[%d] l1 = %.6f, l2 = %.6f, linf = %.6f, lninf = %.6f" % (
                                gi, 
                                l1_norm, 
                                l2_norm, 
                                linf_norm, 
                                lninf_norm
                                )
                else:
                    self.session.run(train_op, feed_dict=feed_dict)
                batch_index += 1
                iterations += 1

                # decay the display intervals for speedup 
                if batch_index % max(math.ceil(num_batches/float(max(params.display_epoch_num/pow(2, epoch//10)+1, 1))), 1) == 0: 
                    train_error.append(self.validate(train_x, train_y, batch_size=self.validate_batch_size))
                    test_error.append(self.validate(test_x, test_y, batch_size=self.validate_batch_size))
                    print("Epoch %s.%s, iterations = %s, training %s = %.6f, testing %s = %.6f, learning rate = %f" % 
                            ('{:03}'.format(epoch), '{:03}'.format(batch_index), '{:05}'.format(iterations), self.metric, train_error[-1], self.metric, test_error[-1], learning_rate))
            
            ## early exit if reach best training 
            #if params.regression_flag and train_error[-1] == 0.0: 
            #    break 
            #elif not params.regression_flag and train_error[-1] == 1.0:
            #    break 

            if np.isnan(train_error[-1]) or np.isinf(train_error[-1]) or np.isnan(test_error[-1]) or np.isinf(test_error[-1]): 
                print("found nan or inf, stop training")
                break 
            if params.tune_flag and epoch == 19:
                if not params.regression_flag: 
                    if train_error[-1]   <   0.4: 
                        print("tune mode exit early")
                        break 

        print("Optimization Finished!")

        return train_error, test_error 

    """

0 Source : core.py
with Apache License 2.0
from mars-project

    def _to_str(self, representation=False):
        if is_build_mode() or len(self._executed_sessions) == 0:
            # in build mode, or not executed, just return representation
            if representation:
                return f"Tensor   <  op={type(self._op).__name__}, shape={self._shape}, key={self._key}>"
            else:
                return f"Tensor(op={type(self._op).__name__}, shape={self._shape})"
        else:
            print_options = np.get_printoptions()
            threshold = print_options["threshold"]

            corner_data = fetch_corner_data(self, session=self._executed_sessions[-1])
            # if less than default threshold, just set it as default,
            # if not, set to corner_data.size - 1 make sure ... exists in repr
            threshold = threshold if self.size  < = threshold else corner_data.size - 1
            with np.printoptions(threshold=threshold):
                corner_str = repr(corner_data) if representation else str(corner_data)
            return corner_str

    def __str__(self):

0 Source : test_utils.py
with Apache License 2.0
from mars-project

def test_fetch_tensor_corner_data(setup):
    print_options = np.get_printoptions()

    # make sure numpy default option
    assert print_options["edgeitems"] == 3
    assert print_options["threshold"] == 1000

    size = 12
    for i in (2, 4, size - 3, size, size + 3):
        arr = np.random.rand(i, i, i)
        t = mt.tensor(arr, chunk_size=size // 2)
        t.execute()

        corner_data = fetch_corner_data(t)
        corner_threshold = 1000 if t.size   <   1000 else corner_data.size - 1
        with np.printoptions(threshold=corner_threshold, suppress=True):
            # when we repr corner data, we need to limit threshold that
            # it's exactly less than the size
            repr_corner_data = repr(corner_data)
        with np.printoptions(suppress=True):
            repr_result = repr(arr)
        assert repr_corner_data == repr_result

0 Source : utils.py
with Apache License 2.0
from mars-project

def fetch_corner_data(tensor, session=None):
    print_option = np.get_printoptions()
    # only fetch corner data when data > threshold
    threshold = print_option["threshold"]
    # number of edge items to print
    edgeitems = print_option["edgeitems"]

    # we fetch corner data based on the fact that
    # the tensor must have been executed,
    # thus the size could not be NaN
    if tensor.size > threshold:
        # two edges for each exis
        indices_iter = list(itertools.product(*(range(2) for _ in range(tensor.ndim))))
        corners = np.empty(shape=(2,) * tensor.ndim, dtype=object)
        shape = [0 for _ in range(tensor.ndim)]
        for indices in indices_iter:
            slc = []
            for ax, i in enumerate(indices):
                size = tensor.shape[ax]
                if size > edgeitems * 2 + 2:
                    # fetch two more elements
                    if i == 0:
                        slc.append(slice(edgeitems + 1))
                    else:
                        slc.append(slice(-edgeitems - 1, None))
                    shape[ax] += edgeitems + 1
                else:
                    i_sep = size // 2
                    if i == 0:
                        slc.append(slice(i_sep))
                        shape[ax] += i_sep
                    else:
                        slc.append(slice(i_sep, None))
                        shape[ax] += size - i_sep
            corners[indices] = tensor[tuple(slc)]
        # fetch together
        fetched = ExecutableTuple(corners.flat).fetch(session=session)
        for indices, f in zip(indices_iter, fetched):
            corners[indices] = f
        return np.block(corners.tolist())
    else:
        return tensor.fetch(session=session)


def implement_scipy(scipy_fun):

0 Source : _ee_aux.py
with GNU General Public License v3.0
from miknab

def disable_numpy_summarization():
    # Author: Jeppe Mosgaard Dakin
    """
    Signature:   disable_numpy_summarization()

    Description: Allows numpy array to have arbitrary length without summarizing
                 its entries.

    Input type:  None

    Output type: None
    """
    threshold = _np.get_printoptions()['threshold']
    _np.set_printoptions(threshold=_np.inf)
    try:
        yield
    finally:
        _np.set_printoptions(threshold=threshold)

def stringify_arr(arr):

0 Source : _linprog.py
with GNU Affero General Public License v3.0
from nccgroup

def linprog_verbose_callback(xk, **kwargs):
    """
    A sample callback function demonstrating the linprog callback interface.
    This callback produces detailed output to sys.stdout before each iteration
    and after the final iteration of the simplex algorithm.

    Parameters
    ----------
    xk : array_like
        The current solution vector.
    **kwargs : dict
        A dictionary containing the following parameters:

        tableau : array_like
            The current tableau of the simplex algorithm.
            Its structure is defined in _solve_simplex.
        phase : int
            The current Phase of the simplex algorithm (1 or 2)
        nit : int
            The current iteration number.
        pivot : tuple(int, int)
            The index of the tableau selected as the next pivot,
            or nan if no pivot exists
        basis : array(int)
            A list of the current basic variables.
            Each element contains the name of a basic variable and its value.
        complete : bool
            True if the simplex algorithm has completed
            (and this is the final call to callback), otherwise False.
    """
    tableau = kwargs["tableau"]
    nit = kwargs["nit"]
    pivrow, pivcol = kwargs["pivot"]
    phase = kwargs["phase"]
    basis = kwargs["basis"]
    complete = kwargs["complete"]

    saved_printoptions = np.get_printoptions()
    np.set_printoptions(linewidth=500,
                        formatter={'float':lambda x: "{0: 12.4f}".format(x)})
    if complete:
        print("--------- Iteration Complete - Phase {0:d} -------\n".format(phase))
        print("Tableau:")
    elif nit == 0:
        print("--------- Initial Tableau - Phase {0:d} ----------\n".format(phase))

    else:
        print("--------- Iteration {0:d}  - Phase {1:d} --------\n".format(nit, phase))
        print("Tableau:")

    if nit >= 0:
        print("" + str(tableau) + "\n")
        if not complete:
            print("Pivot Element: T[{0:.0f}, {1:.0f}]\n".format(pivrow, pivcol))
        print("Basic Variables:", basis)
        print()
        print("Current Solution:")
        print("x = ", xk)
        print()
        print("Current Objective Value:")
        print("f = ", -tableau[-1, -1])
        print()
    np.set_printoptions(**saved_printoptions)


def linprog_terse_callback(xk, **kwargs):

See More Examples