Python numpy.__dict__() Examples

The following are 18 code examples for showing how to use numpy.__dict__(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: spinmob   Author: Spinmob   File: example_daq.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_fake_data():

    # try to evaluate the source script
    try:
        # first get all the extra numpy globals
        g = _n.__dict__

        # update these globals with extra stuff needed for evaluation
        g.update(dict(t=d1[0]+s["settings/simulated_input/noise"]*_n.random.rand()))
        y = eval(s["settings/simulated_input/source"], g)+_n.random.random(len(d1['t']))

    # default to zeros
    except:
        print("ERROR: Invalid source script.")
        y = d1[0]*0.0

    # pretend this acquisition actually took time (avoids black holes)
    _t.sleep(s["settings/simulated_input/duration"])

    return y


# define a function to be called whenever the acquire button is pressed 
Example 2
Project: spinmob   Author: Spinmob   File: _data.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _globals(self):
        """
        Returns the globals needed for eval() statements.
        """

        # start with numpy
        globbies = dict(_n.__dict__)
        globbies.update(_special.__dict__)

        # update with required stuff
        globbies.update({'h':self.h, 'c':self.c, 'd':self, 'self':self})

        # update with user stuff
        globbies.update(self.extra_globals)

        return globbies 
Example 3
Project: rio-pansharpen   Author: mapbox   File: utils.py    License: MIT License 6 votes vote down vote up
def _rescale(arr, ndv, dst_dtype, out_alpha=True):
    """Convert an array from output dtype, scaling up linearly
    """
    if dst_dtype == np.__dict__['uint16']:
        scale = 1
    else:
        # convert to 8bit value range in place
        scale = float(np.iinfo(np.uint16).max) / float(np.iinfo(np.uint8).max)

    res = (arr / scale).astype(dst_dtype)

    if out_alpha:
        mask = _simple_mask(
            arr.astype(dst_dtype),
            (ndv, ndv, ndv)).reshape(
                1, arr.shape[1], arr.shape[2])
        return np.concatenate([res, mask])
    else:
        return res 
Example 4
Project: rio-pansharpen   Author: mapbox   File: test_pansharp_unittest.py    License: MIT License 6 votes vote down vote up
def test_pansharp_data():
    b8_path = 'tests/fixtures/tiny_20_tiffs/LC81070352015122LGN00/'\
              'LC81070352015122LGN00_B8.tif'
    b4_path = 'tests/fixtures/tiny_20_tiffs/LC81070352015122LGN00/'\
              'LC81070352015122LGN00_B4.tif'
    b3_path = 'tests/fixtures/tiny_20_tiffs/LC81070352015122LGN00/'\
              'LC81070352015122LGN00_B3.tif'
    b2_path = 'tests/fixtures/tiny_20_tiffs/LC81070352015122LGN00/'\
              'LC81070352015122LGN00_B2.tif'
    band_paths = [b8_path, b4_path, b3_path, b2_path]
    pan_window = ((1536, 1792), (1280, 1536))
    g_args = {'half_window': False,
              'dst_aff': Affine(75.00483870967741, 0.0, 300892.5,
                                0.0, -75.00475285171103, 4107007.5),
              'verb': False, 'weight': 0.2,
              'dst_crs': {'init': u'epsg:32654'},
              'r_crs': {'init': u'epsg:32654'},
              'dst_dtype': np.__dict__['uint16'],
              'r_aff': Affine(150.0193548387097, 0.0, 300885.0,
                              0.0, -150.0190114068441, 4107015.0),
              'src_nodata': 0}

    return [rasterio.open(f) for f in band_paths],\
        pan_window, (6, 5), g_args 
Example 5
Project: TheCannon   Author: annayqho   File: simpletable.py    License: MIT License 5 votes vote down vote up
def nbytes(self):
        """ number of bytes of the object """
        n = sum(k.nbytes if hasattr(k, 'nbytes') else sys.getsizeof(k)
                for k in self.__dict__.values())
        return n 
Example 6
Project: TheCannon   Author: annayqho   File: simpletable.py    License: MIT License 5 votes vote down vote up
def evalexpr(self, expr, exprvars=None, dtype=float):
        """ evaluate expression based on the data and external variables
            all np function can be used (log, exp, pi...)

        Parameters
        ----------
        expr: str
            expression to evaluate on the table
            includes mathematical operations and attribute names

        exprvars: dictionary, optional
            A dictionary that replaces the local operands in current frame.

        dtype: dtype definition
            dtype of the output array

        Returns
        -------
        out : NumPy array
            array of the result
        """
        _globals = {}
        for k in ( list(self.colnames) + list(self._aliases.keys()) ):
            if k in expr:
                _globals[k] = self[k]

        if exprvars is not None:
            if (not (hasattr(exprvars, 'keys') & hasattr(exprvars, '__getitem__' ))):
                raise AttributeError("Expecting a dictionary-like as condvars")
            for k, v in ( exprvars.items() ):
                _globals[k] = v

        # evaluate expression, to obtain the final filter
        r    = np.empty( self.nrows, dtype=dtype)
        r[:] = eval(expr, _globals, np.__dict__)

        return r 
Example 7
Project: pylada-light   Author: pylada   File: test_structure.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_initialization():
    """ Test structure initialization. """
    a = Structure()
    assert all(abs(a.cell - identity(3)) < 1e-8)
    assert abs(a.scale - 1e0 * angstrom) < 1e0
    assert len(a.__dict__) == 3

    a = Structure(identity(3) * 2.5, scale=5.45)
    assert all(abs(a.cell - identity(3) * 2.5) < 1e-8)
    assert abs(a.scale - 5.45 * angstrom) < 1e0
    assert len(a.__dict__) == 3

    a = Structure(identity(3) * 2.5, scale=0.545 * nanometer)
    assert all(abs(a.cell - identity(3) * 2.5) < 1e-8)
    assert abs(a.scale - 5.45 * angstrom) < 1e0
    assert len(a.__dict__) == 3

    a = Structure(2.5, 0, 0, 0, 2.5, 0, 0, 0, 2.5, scale=5.45)
    assert all(abs(a.cell - identity(3) * 2.5) < 1e-8)
    assert abs(a.scale - 5.45 * angstrom) < 1e0
    assert len(a.__dict__) == 3

    a = Structure([2.5, 0, 0], [0, 2.5, 0], [0, 0, 2.5], scale=5.45)
    assert all(abs(a.cell - identity(3) * 2.5) < 1e-8)
    assert abs(a.scale - 5.45 * angstrom) < 1e0
    assert len(a.__dict__) == 3

    a = Structure(cell=[[2.5, 0, 0], [0, 2.5, 0], [0, 0, 2.5]], scale=5.45)
    assert all(abs(a.cell - identity(3) * 2.5) < 1e-8)
    assert abs(a.scale - 5.45 * angstrom) < 1e0
    assert len(a.__dict__) == 3

    a = Structure(identity(3) * 2.5, scale=5.45, m=True)
    assert all(abs(a.cell - identity(3) * 2.5) < 1e-8)
    assert abs(a.scale - 5.45 * angstrom) < 1e0
    assert len(a.__dict__) == 4 and getattr(a, 'm', False) 
Example 8
Project: pylada-light   Author: pylada   File: test_structure.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_representability():
    import quantities
    import numpy
    dictionary = {Structure.__name__: Structure}
    dictionary.update(numpy.__dict__)
    dictionary.update(quantities.__dict__)

    expected = Structure()
    actual = eval(repr(expected), dictionary)
    assert all(abs(expected.cell - actual.cell) < 1e-8)
    assert abs(expected.scale - actual.scale) < 1e-8
    assert len(expected) == len(actual)

    expected = Structure([1, 2, 0], [3, 4, 5], [6, 7, 8], m=True)
    actual = eval(repr(expected), dictionary)
    assert all(abs(expected.cell - actual.cell) < 1e-8)
    assert abs(expected.scale - actual.scale) < 1e-8
    assert len(expected) == len(actual)
    assert getattr(expected, 'm', False) == actual.m

    expected = Structure([1, 2, 0], [3, 4, 5], [6, 7, 8], m=True) \
        .add_atom(0, 1, 2, "Au", m=5) \
        .add_atom(0, -1, -2, "Pd")
    actual = eval(repr(expected), dictionary)
    assert all(abs(expected.cell - actual.cell) < 1e-8)
    assert abs(expected.scale - actual.scale) < 1e-8
    assert len(expected) == len(actual)
    assert all(abs(expected[0].pos - actual[0].pos) < 1e-8)
    assert getattr(expected[0], 'm', 0) == actual[0].m
    assert expected[0].type == actual[0].type
    assert all(abs(expected[1].pos - actual[1].pos) < 1e-8)
    assert expected[1].type == actual[1].type
    assert getattr(expected, 'm', False) == actual.m 
Example 9
Project: qupulse   Author: qutech   File: sympy.py    License: MIT License 5 votes vote down vote up
def substitute_with_eval(expression: sympy.Expr,
                         substitutions: Dict[str, Union[sympy.Expr, numpy.ndarray, str]]) -> sympy.Expr:
    """Substitutes only sympy.Symbols. Workaround for numpy like array behaviour. ~Factor 3 slower compared to subs"""
    substitutions = {k: v if isinstance(v, sympy.Expr) else sympify(v)
                     for k, v in substitutions.items()}

    for symbol in get_free_symbols(expression):
        symbol_name = str(symbol)
        if symbol_name not in substitutions:
            substitutions[symbol_name] = symbol

    string_representation = sympy.srepr(expression)
    return eval(string_representation, sympy.__dict__, {'Symbol': substitutions.__getitem__,
                                                        'Mul': numpy_compatible_mul}) 
Example 10
Project: rio-pansharpen   Author: mapbox   File: test_pansharp_unittest.py    License: MIT License 5 votes vote down vote up
def test_pansharpen_worker_uint8(test_pansharp_data):
    open_files, pan_window, _, g_args = test_pansharp_data
    g_args.update(dst_dtype=np.__dict__['uint8'])
    pan_output = _pansharpen_worker(open_files, pan_window, _, g_args)
    assert pan_output.dtype == np.uint8
    assert np.max(pan_output) <= 2**8 
Example 11
Project: rio-pansharpen   Author: mapbox   File: test_property_based.py    License: MIT License 5 votes vote down vote up
def test_rescale(arr, ndv, dst_dtype):
    if dst_dtype == np.__dict__['uint16']:
        assert np.array_equal(
                _rescale(arr, ndv, dst_dtype),
                np.concatenate(
                    [
                        (arr).astype(dst_dtype),
                        _simple_mask(
                            arr.astype(dst_dtype),
                            (ndv, ndv, ndv)
                        ).reshape(1, arr.shape[1], arr.shape[2])
                    ]
                )
            )
    else:
        assert np.array_equal(
                _rescale(arr, ndv, dst_dtype),
                np.concatenate(
                    [
                        (arr / 257.0).astype(dst_dtype),
                        _simple_mask(
                            arr.astype(dst_dtype),
                            (ndv, ndv, ndv)
                        ).reshape(1, arr.shape[1], arr.shape[2])
                    ]
                )
            )


# Testing make_windows_block function's random element 
Example 12
Project: pyphot   Author: mfouesneau   File: simpletable.py    License: MIT License 5 votes vote down vote up
def nbytes(self):
        """ number of bytes of the object """
        n = sum(k.nbytes if hasattr(k, 'nbytes') else sys.getsizeof(k)
                for k in self.__dict__.values())
        return n 
Example 13
Project: pyphot   Author: mfouesneau   File: simpletable.py    License: MIT License 5 votes vote down vote up
def evalexpr(self, expr, exprvars=None, dtype=float):
        """ evaluate expression based on the data and external variables
            all np function can be used (log, exp, pi...)

        Parameters
        ----------
        expr: str
            expression to evaluate on the table
            includes mathematical operations and attribute names

        exprvars: dictionary, optional
            A dictionary that replaces the local operands in current frame.

        dtype: dtype definition
            dtype of the output array

        Returns
        -------
        out : NumPy array
            array of the result
        """
        _globals = {}
        for k in ( list(self.colnames) + list(self._aliases.keys()) ):
            if k in expr:
                _globals[k] = self[k]

        if exprvars is not None:
            if (not (hasattr(exprvars, 'keys') & hasattr(exprvars, '__getitem__' ))):
                raise AttributeError("Expecting a dictionary-like as condvars")
            for k, v in ( exprvars.items() ):
                _globals[k] = v

        # evaluate expression, to obtain the final filter
        r    = np.empty( self.nrows, dtype=dtype)
        r[:] = eval(expr, _globals, np.__dict__)

        return r 
Example 14
Project: pyphot   Author: mfouesneau   File: simpletable.py    License: MIT License 5 votes vote down vote up
def nbytes(self):
        """ number of bytes of the object """
        n = sum(k.nbytes if hasattr(k, 'nbytes') else sys.getsizeof(k)
                for k in self.__dict__.values())
        return n 
Example 15
Project: pyphot   Author: mfouesneau   File: simpletable.py    License: MIT License 5 votes vote down vote up
def evalexpr(self, expr, exprvars=None, dtype=float):
        """ evaluate expression based on the data and external variables
            all np function can be used (log, exp, pi...)

        Parameters
        ----------
        expr: str
            expression to evaluate on the table
            includes mathematical operations and attribute names

        exprvars: dictionary, optional
            A dictionary that replaces the local operands in current frame.

        dtype: dtype definition
            dtype of the output array

        Returns
        -------
        out : NumPy array
            array of the result
        """
        _globals = {}
        for k in ( list(self.colnames) + list(self._aliases.keys()) ):
            if k in expr:
                _globals[k] = self[k]

        if exprvars is not None:
            if (not (hasattr(exprvars, 'keys') & hasattr(exprvars, '__getitem__' ))):
                raise AttributeError("Expecting a dictionary-like as condvars")
            for k, v in ( exprvars.items() ):
                _globals[k] = v

        # evaluate expression, to obtain the final filter
        r    = np.empty( self.nrows, dtype=dtype)
        r[:] = eval(expr, _globals, np.__dict__)

        return r 
Example 16
Project: TheCannon   Author: annayqho   File: simpletable.py    License: MIT License 4 votes vote down vote up
def select(self, fields, indices=None, **kwargs):
        """
        Select only a few fields in the table

        Parameters
        ----------
        fields: str or sequence
            fields to keep in the resulting table

        indices: sequence or slice
            extract only on these indices

        returns
        -------
        tab: SimpleTable instance
            resulting table
        """
        _fields = self.keys(fields)

        if fields == '*':
            if indices is None:
                return self
            else:
                tab = self.__class__(self[indices])
                for k in self.__dict__.keys():
                    if k not in ('data', ):
                        setattr(tab, k, deepcopy(self.__dict__[k]))
                return tab
        else:
            d = {}
            for k in _fields:
                _k = self.resolve_alias(k)
                if indices is not None:
                    d[k] = self[_k][indices]
                else:
                    d[k] = self[_k]
            d['header'] = deepcopy(self.header)
            tab = self.__class__(d)
            for k in self.__dict__.keys():
                if k not in ('data', ):
                    setattr(tab, k, deepcopy(self.__dict__[k]))
            return tab 
Example 17
Project: bootstrap.pytorch   Author: Cadene   File: compare.py    License: BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def load_values(dir_logs, metrics, nb_epochs=-1, best=None):
    json_files = {}
    values = {}

    # load argsup of best
    if best:
        if best['json'] not in json_files:
            with open(osp.join(dir_logs, f'{best["json"]}.json')) as f:
                json_files[best['json']] = json.load(f)

        jfile = json_files[best['json']]
        vals = jfile[best['name']]
        end = len(vals) if nb_epochs == -1 else nb_epochs
        argsup = np.__dict__[f'arg{best["order"]}'](vals[:end])

    # load logs
    for _key, metric in metrics.items():
        # open json_files
        if metric['json'] not in json_files:
            with open(osp.join(dir_logs, f'{metric["json"]}.json')) as f:
                json_files[metric['json']] = json.load(f)

        jfile = json_files[metric['json']]

        if 'train' in metric['name']:
            epoch_key = 'train_epoch.epoch'
        else:
            epoch_key = 'eval_epoch.epoch'

        if epoch_key in jfile:
            epochs = jfile[epoch_key]
        else:
            epochs = jfile['epoch']

        vals = jfile[metric['name']]
        if not best:
            end = len(vals) if nb_epochs == -1 else nb_epochs
            argsup = np.__dict__[f'arg{metric["order"]}'](vals[:end])

        try:
            values[metric['name']] = epochs[argsup], vals[argsup]
        except IndexError:
            values[metric['name']] = epochs[argsup - 1], vals[argsup - 1]
    return values 
Example 18
Project: pyphot   Author: mfouesneau   File: simpletable.py    License: MIT License 4 votes vote down vote up
def select(self, fields, indices=None, **kwargs):
        """
        Select only a few fields in the table

        Parameters
        ----------
        fields: str or sequence
            fields to keep in the resulting table

        indices: sequence or slice
            extract only on these indices

        returns
        -------
        tab: SimpleTable instance
            resulting table
        """
        _fields = self.keys(fields)

        if fields == '*':
            if indices is None:
                return self
            else:
                tab = self.__class__(self[indices])
                for k in self.__dict__.keys():
                    if k not in ('data', ):
                        setattr(tab, k, deepcopy(self.__dict__[k]))
                return tab
        else:
            d = {}
            for k in _fields:
                _k = self.resolve_alias(k)
                if indices is not None:
                    d[k] = self[_k][indices]
                else:
                    d[k] = self[_k]
            d['header'] = deepcopy(self.header)
            tab = self.__class__(d)
            for k in self.__dict__.keys():
                if k not in ('data', ):
                    setattr(tab, k, deepcopy(self.__dict__[k]))
            return tab