Interacting with Dimension objectsΒΆ

LinearDimensionΒΆ

There are several attributes and methods associated with the LinearDimension, each controlling the coordinates along the dimension. The following section demonstrates the effect of these attributes and methods on the coordinates of the LinearDimension.

>>> import csdmpy as cp
>>> x = cp.LinearDimension(count=10, increment="0.1 s", label="time",
...                          description="A temporal dimension.")
>>> print(x)
LinearDimension([0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] s)

AttributesΒΆ

type

This attribute returns the type of the instance.

>>> print(x.type)
linear

The attributes that modify the coordinates

count

The number of points along the dimension

>>> print('number of points =', x.count)
number of points = 10

To update the number of points, update the value of this attribute,

>>> x.count = 12
>>> print('new number of points =', x.count)
new number of points = 12

>>> print('new coordinates =', x.coordinates)
new coordinates = [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1] s
increment
>>> print('old increment =', x.increment)
old increment = 0.1 s

>>> x.increment = "10 s"
>>> print('new increment =', x.increment)
new increment = 10.0 s

>>> print('new coordinates =', x.coordinates)
new coordinates = [  0.  10.  20.  30.  40.  50.  60.  70.  80.  90. 100. 110.] s
coordinates_offset
>>> print('old reference offset =', x.coordinates_offset)
old reference offset = 0.0 s

>>> x.coordinates_offset = "1 s"
>>> print('new reference offset =', x.coordinates_offset)
new reference offset = 1.0 s

>>> print('new coordinates =', x.coordinates)
new coordinates = [  1.  11.  21.  31.  41.  51.  61.  71.  81.  91. 101. 111.] s
origin_offset
>>> print('old origin offset =', x.origin_offset)
old origin offset = 0.0 s

>>> x.origin_offset = "1 day"
>>> print ('new origin offset =', x.origin_offset)
new origin offset = 1.0 d

>>> print('new coordinates =', x.coordinates)
new coordinates = [  1.  11.  21.  31.  41.  51.  61.  71.  81.  91. 101. 111.] s

The last operation updates the value of the origin offset, however, the coordinates remain unaffected. This is because the coordinates attribute refers to the reference coordinates. You may access the absolute coordinates through the absolute_coordinates attribute.

>>> print('absolute coordinates =', x.absolute_coordinates)
absolute coordinates = [86401. 86411. 86421. 86431. 86441. 86451. 86461. 86471. 86481. 86491.
 86501. 86511.] s

The attributes that modify the order of coordinates

complex_fft

If true, orders the coordinates along the dimension according to the output of a complex Fast Fourier Transform (FFT) routine.

>>> print('old coordinates =', x.coordinates)
old coordinates = [  1.  11.  21.  31.  41.  51.  61.  71.  81.  91. 101. 111.] s

>>> x.complex_fft = True
>>> print('new coordinates =', x.coordinates)
new coordinates = [-59. -49. -39. -29. -19.  -9.   1.  11.  21.  31.  41.  51.] s

Other attributes

period

The period of the dimension.

>>> print('old period =', x.period)
old period = inf s

>>> x.period = '10 s'
>>> print('new period =', x.period)
new period = 10.0 s
quantity_name

Returns the quantity name.

>>> print('quantity name is', x.quantity_name)
quantity name is time
label
>>> x.label
'time'

>>> x.label = 't1'
>>> x.label
't1'
axis_label

Returns a formatted string for labeling axis.

>>> x.label
't1'
>>> x.axis_label
't1 / (s)'

MethodsΒΆ

to(): This method is used for unit conversions.

>>> print('old unit =', x.coordinates.unit)
old unit = s

>>> print('old coordinates =', x.coordinates)
old coordinates = [-59. -49. -39. -29. -19.  -9.   1.  11.  21.  31.  41.  51.] s

>>> ## unit conversion
>>> x.to('min')

>>> print ('new coordinates =', x.coordinates)
new coordinates = [-0.98333333 -0.81666667 -0.65       -0.48333333 -0.31666667 -0.15
  0.01666667  0.18333333  0.35        0.51666667  0.68333333  0.85      ] min

Note

In the above examples, the coordinates are ordered according to the FFT output order, based on the previous set of operations.

The argument of this method is a string containing the unit, in this case, min, whose dimensionality is be consistent with the dimensionality of the coordinates. An exception will be raised otherwise.

>>> x.to('km/s')  
Exception: The unit 'km / s' (speed) is inconsistent with the unit 'min' (time).

Changing the dimensionalityΒΆ

You may scale the dimension object by multiplying the object with the appropriate ScalarQuantity, as follows,

>>> print(x)
LinearDimension([-0.98333333 -0.81666667 -0.65       -0.48333333 -0.31666667 -0.15
  0.01666667  0.18333333  0.35        0.51666667  0.68333333  0.85      ] min)
>>> x *= cp.ScalarQuantity('m/s')
>>> print(x)
LinearDimension([-59. -49. -39. -29. -19.  -9.   1.  11.  21.  31.  41.  51.] m)

MonotonicDimensionΒΆ

There are several attributes and methods associated with a MonotonicDimension, controlling the coordinates along the dimension. The following section demonstrates the effect of these attributes and methods on the coordinates.

>>> import numpy as np
>>> array = np.asarray([-0.28758166, -0.22712233, -0.19913859, -0.17235106,
...                     -0.1701172, -0.10372635, -0.01817061, 0.05936719,
...                     0.18141424, 0.34758913])
>>> x = cp.MonotonicDimension(coordinates=array)*cp.ScalarQuantity('cm')

AttributesΒΆ

The following are the attributes of the MonotonicDimension instance.

type

This attribute returns the type of the instance.

>>> print(x.type)
monotonic

The attributes that modify the coordinates

count

The number of points along the dimension

>>> print ('number of points =', x.count)
number of points = 10

You may update the number of points with this attribute, however, you can only lower the number of points.

>>> x.count = 6
>>> print('new number of points =', x.count)
new number of points = 6
>>> print(x.coordinates)
[-0.28758166 -0.22712233 -0.19913859 -0.17235106 -0.1701172  -0.10372635] cm
origin_offset
>>> print('old origin offset =', x.origin_offset)
old origin offset = 0.0 cm

>>> x.origin_offset = "1 km"
>>> print('new origin offset =', x.origin_offset)
new origin offset = 1.0 km

>>> print(x.coordinates)
[-0.28758166 -0.22712233 -0.19913859 -0.17235106 -0.1701172  -0.10372635] cm

The last operation updates the value of the origin offset, however, the value of the coordinates attribute remains unchanged. This is because the coordinates refer to the reference coordinates. The absolute coordinates are accessed through the absolute_coordinates attribute.

>>> print('absolute coordinates =', x.absolute_coordinates)
absolute coordinates = [99999.71241834 99999.77287767 99999.80086141 99999.82764894
 99999.8298828  99999.89627365] cm

Other attributes

label
>>> x.label = 't1'
>>> print('new label =', x.label)
new label = t1
period
>>> print('old period =', x.period)
old period = inf cm

>>> x.period = '10 m'
>>> print('new period =', x.period)
new period = 10.0 m
quantity_name

Returns the quantity name.

>>> print ('quantity is', x.quantity_name)
quantity is length

MethodsΒΆ

to()

The method is used for unit conversions. It follows,

>>> print('old unit =', x.coordinates.unit)
old unit = cm
>>> print('old coordinates =', x.coordinates)
old coordinates = [-0.28758166 -0.22712233 -0.19913859 -0.17235106 -0.1701172  -0.10372635] cm

>>> ## unit conversion
>>> x.to('mm')

>>> print('new coordinates =', x.coordinates)
new coordinates = [-2.8758166 -2.2712233 -1.9913859 -1.7235106 -1.701172  -1.0372635] mm

The argument of this method is a unit, in this case, β€˜mm’, whose dimensionality must be consistent with the dimensionality of the coordinates. An exception will be raised otherwise,

>>> x.to('km/s')  
Exception("Validation Failed: The unit 'km / s' (speed) is inconsistent with the unit 'mm' (length).")

Changing the dimensionalityΒΆ

You may scale the dimension object by multiplying the object with the appropriate ScalarQuantity, as follows,

>>> print(x)
MonotonicDimension([-2.8758166 -2.2712233 -1.9913859 -1.7235106 -1.701172  -1.0372635] mm)
>>> x *= cp.ScalarQuantity('2 s/mm')
>>> print(x)
MonotonicDimension([-0.57516332 -0.45424466 -0.39827718 -0.34470212 -0.3402344  -0.2074527 ] cm s / mm)