def test_as_array(self): """ Tests :func:`colour.utilities.array.as_array` definition. """ np.testing.assert_equal(as_array([1, 2, 3]), np.array([1, 2, 3])) self.assertEqual( as_array([1, 2, 3], DEFAULT_FLOAT_DTYPE).dtype, DEFAULT_FLOAT_DTYPE) self.assertEqual( as_array([1, 2, 3], DEFAULT_INT_DTYPE).dtype, DEFAULT_INT_DTYPE)
def test_as_array(self): """ Tests :func:`colour.utilities.array.as_array` definition. """ np.testing.assert_equal(as_array([1, 2, 3]), np.array([1, 2, 3])) self.assertEqual( as_array([1, 2, 3], DEFAULT_FLOAT_DTYPE).dtype, DEFAULT_FLOAT_DTYPE) self.assertEqual( as_array([1, 2, 3], DEFAULT_INT_DTYPE).dtype, DEFAULT_INT_DTYPE)
def parse_array(a, separator=' ', dtype=DEFAULT_FLOAT_DTYPE): """ Converts given string or array of strings to :class:`ndarray` class. Parameters ---------- a : unicode or array_like String or array of strings to convert. separator : unicode Separator to split the string with. dtype : object Type to use for conversion. Returns ------- ndarray Converted string or array of strings. Examples -------- >>> parse_array('-0.25 0.5 0.75') array([-0.25, 0.5 , 0.75]) >>> parse_array(['-0.25', '0.5', '0.75']) array([-0.25, 0.5 , 0.75]) """ if is_string(a): a = a.split(separator) return as_array([dtype(token) for token in a], dtype)
def test_as_array(self): """ Tests :func:`colour.utilities.array.as_array` definition. """ np.testing.assert_equal(as_array([1, 2, 3]), np.array([1, 2, 3])) self.assertEqual( as_array([1, 2, 3], DEFAULT_FLOAT_DTYPE).dtype, DEFAULT_FLOAT_DTYPE) self.assertEqual( as_array([1, 2, 3], DEFAULT_INT_DTYPE).dtype, DEFAULT_INT_DTYPE) if six.PY3: # pragma: no cover np.testing.assert_equal( as_array(dict(zip('abc', [1, 2, 3])).values()), np.array([1, 2, 3]))
def signal_unpack_data(data=None, domain=None, dtype=None): """ Unpack given data for continuous signal instantiation. Parameters ---------- data : Series or Signal or array_like or dict_like, optional Data to unpack for continuous signal instantiation. domain : array_like, optional Values to initialise the :attr:`colour.continuous.Signal.domain` attribute with. If both ``data`` and ``domain`` arguments are defined, the latter will be used to initialise the :attr:`colour.continuous.Signal.domain` attribute. dtype : type, optional **{np.float16, np.float32, np.float64, np.float128}**, Floating point data type. Returns ------- tuple Independent domain :math:`x` variable and corresponding range :math:`y` variable unpacked for continuous signal instantiation. Examples -------- Unpacking using implicit *domain*: >>> range_ = np.linspace(10, 100, 10) >>> domain, range_ = Signal.signal_unpack_data(range_) >>> print(domain) [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] >>> print(range_) [ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] Unpacking using explicit *domain*: >>> domain = np.arange(100, 1100, 100) >>> domain, range = Signal.signal_unpack_data(range_, domain) >>> print(domain) [ 100. 200. 300. 400. 500. 600. 700. 800. 900. 1000.] >>> print(range_) [ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] Unpacking using a *dict*: >>> domain, range_ = Signal.signal_unpack_data( ... dict(zip(domain, range_))) >>> print(domain) [ 100. 200. 300. 400. 500. 600. 700. 800. 900. 1000.] >>> print(range_) [ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] Unpacking using a *Pandas* `Series`: >>> if is_pandas_installed(): ... from pandas import Series ... domain, range = Signal.signal_unpack_data( ... Series(dict(zip(domain, range_)))) ... # doctest: +ELLIPSIS >>> print(domain) # doctest: +SKIP [ 100. 200. 300. 400. 500. 600. 700. 800. 900. 1000.] >>> print(range_) # doctest: +SKIP [ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] Unpacking using a :class:`colour.continuous.Signal` class: >>> domain, range_ = Signal.signal_unpack_data( ... Signal(range_, domain)) >>> print(domain) [ 100. 200. 300. 400. 500. 600. 700. 800. 900. 1000.] >>> print(range_) [ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] """ if dtype is None: dtype = DEFAULT_FLOAT_DTYPE domain_u, range_u = None, None if isinstance(data, Signal): domain_u = data.domain range_u = data.range elif (issubclass(type(data), Sequence) or isinstance(data, (tuple, list, np.ndarray, Iterator))): data = tsplit(list(data) if isinstance(data, Iterator) else data) assert data.ndim == 1, 'User "data" must be 1-dimensional!' domain_u, range_u = np.arange(0, data.size, dtype=dtype), data elif (issubclass(type(data), Mapping) or isinstance(data, (dict, OrderedDict))): domain_u, range_u = tsplit(sorted(data.items())) elif is_pandas_installed(): from pandas import Series if isinstance(data, Series): domain_u = data.index.values range_u = data.values if domain is not None and range_u is not None: assert len(domain) == len(range_u), ( 'User "domain" is not compatible with unpacked range!') domain_u = as_array(domain, dtype) if range_u is not None: range_u = as_array(range_u, dtype) return domain_u, range_u
def signal_unpack_data(data=None, domain=None, dtype=DEFAULT_FLOAT_DTYPE): """ Unpack given data for continuous signal instantiation. Parameters ---------- data : Series or Signal or array_like or dict_like, optional Data to unpack for continuous signal instantiation. domain : array_like, optional Values to initialise the :attr:`colour.continuous.Signal.domain` attribute with. If both ``data`` and ``domain`` arguments are defined, the latter will be used to initialise the :attr:`colour.continuous.Signal.domain` attribute. dtype : type, optional **{np.float16, np.float32, np.float64, np.float128}**, Floating point data type. Returns ------- tuple Independent domain :math:`x` variable and corresponding range :math:`y` variable unpacked for continuous signal instantiation. Examples -------- Unpacking using implicit *domain*: >>> range_ = np.linspace(10, 100, 10) >>> domain, range_ = Signal.signal_unpack_data(range_) >>> print(domain) [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] >>> print(range_) [ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] Unpacking using explicit *domain*: >>> domain = np.arange(100, 1100, 100) >>> domain, range = Signal.signal_unpack_data(range_, domain) >>> print(domain) [ 100. 200. 300. 400. 500. 600. 700. 800. 900. 1000.] >>> print(range_) [ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] Unpacking using a *dict*: >>> domain, range_ = Signal.signal_unpack_data( ... dict(zip(domain, range_))) >>> print(domain) [ 100. 200. 300. 400. 500. 600. 700. 800. 900. 1000.] >>> print(range_) [ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] Unpacking using a *Pandas* *Series*: >>> if is_pandas_installed(): ... from pandas import Series ... domain, range = Signal.signal_unpack_data( ... Series(dict(zip(domain, range_)))) ... # doctest: +ELLIPSIS >>> print(domain) # doctest: +SKIP [ 100. 200. 300. 400. 500. 600. 700. 800. 900. 1000.] >>> print(range_) # doctest: +SKIP [ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] Unpacking using a :class:`colour.continuous.Signal` class: >>> domain, range_ = Signal.signal_unpack_data( ... Signal(range_, domain)) >>> print(domain) [ 100. 200. 300. 400. 500. 600. 700. 800. 900. 1000.] >>> print(range_) [ 10. 20. 30. 40. 50. 60. 70. 80. 90. 100.] """ assert dtype in np.sctypes['float'], ( '"dtype" must be one of the following types: {0}'.format( np.sctypes['float'])) domain_u, range_u = None, None if isinstance(data, Signal): domain_u = data.domain range_u = data.range elif (issubclass(type(data), Sequence) or isinstance(data, (tuple, list, np.ndarray, Iterator))): data = tsplit(list(data) if isinstance(data, Iterator) else data) assert data.ndim == 1, 'User "data" must be 1-dimensional!' domain_u, range_u = np.arange(0, data.size, dtype=dtype), data elif (issubclass(type(data), Mapping) or isinstance(data, (dict, OrderedDict))): domain_u, range_u = tsplit(sorted(data.items())) elif is_pandas_installed(): from pandas import Series if isinstance(data, Series): domain_u = data.index.values range_u = data.values if domain is not None and range_u is not None: assert len(domain) == len(range_u), ( 'User "domain" is not compatible with unpacked range!') domain_u = as_array(domain, dtype) if range_u is not None: range_u = as_array(range_u, dtype) return domain_u, range_u