DataClass¶
- class sbpy.data.DataClass[source]¶
Bases:
objectDataClassserves as the base class for all data container classes insbpyin order to provide consistent functionality. Classes derived fromDataClasshave the following properties:The core of
DataClassis anQTableobject(referred to as thedata tablebelow) - a type ofTableobject that supports theunitsformalism on a per-column base - which already provides most of the required functionality.DataClassobjects can be manually generated from dictionaries (from_dict), list-like objects on a per-column basis(from_columns) or a per-row basis(from_rows), or directly from anotherQTableobject. It is possible to writeDataClassobjects to a file and from a file.DataClassobjects can hold meta data that are stored asQTablemeta data and can be accessed as aDataClassproperty. Furthermore,DataClassobjects have the ability to recognize alternative names for properties stored in the data table and even do transformations.A few high-level functions for table data access or modification are provided; other, more complex modifications can be applied to the underlying table object(
table) directly.Attributes Summary
Return a list of all field names in the data table.
Enables access to the meta data of the underlying data table.
Return
QTableobject containing all data.Methods Summary
add_row(vals[, names, units])Add a new row to the end of DataClass.
apply(data, name[, unit])Apply an arbitrarily shaped sequence as additional column to a
DataClassobject and reshape it accordingly.from_columns(columns, names[, units, meta])Create
DataClassobject from a sequence.from_dict(data[, meta])Create
DataClassobject from dictionary.from_file(filename[, meta])from_rows(rows, names[, units, meta])Create
DataClassobject from a sequence.from_table(table[, meta])to_file(filename[, format])Write object to a file using
write.verify_fields([field])Verify that the fields have the expected units or object types.
vstack(data, **kwargs)Stack another DataClass object to the end of DataClass
Attributes Documentation
- field_names¶
Return a list of all field names in the data table.
- meta¶
Enables access to the meta data of the underlying data table.
Examples
>>> from sbpy.data import DataClass >>> import astropy.units as u >>> data = DataClass.from_columns([[1, 2, 3, 4]*u.kg, ... [5, 6, 7, 8]*u.m], ... names=['b', 'c'], ... meta={'origin': 'measured'}) >>> data.meta # meta data access {'origin': 'measured'} >>> data.meta['date'] = '2019-06-27' # meta data modification >>> data.meta['date'] '2019-06-27'
Methods Documentation
- add_row(vals, names=None, units=None)[source]¶
Add a new row to the end of DataClass.
This is similar to
astropy.table.Table.add_row, but allows for a set of different columns in the new row from the original DataClass object. It also allows for aliases of column names.- Parameters:
- vals
Row, tuple, list, dict Row to be added
- namesiterable of strings, optional
The names of columns if not implicitly specified in
vals. Takes precedence over the column names invalsif any.- unitsstr or list-like, optional
Unit labels (as provided by
Unit) in which the data provided inrowswill be stored in the underlying table. If None, the units as provided byrowsare used. If the units provided inunitsdiffer from those used inrows,rowswill be transformed to the units provided inunits. Must have the same length asnamesand the individual data rows inrows. Default: None
- vals
Notes
If a time is included in
vals, it can either be an explicitTimeobject, or a number,Quantityobject, or string that can be inferred to be a time by the existing column of the same name or by its position in the sequence. In this case, the type of time values must be valid to initialize anTimeobject with format=’jd’ or ‘isot’, and the scale of time is default to the scale of the corresponding existing column of time.Examples
>>> import astropy.units as u >>> from sbpy.data import DataClass >>> >>> data = DataClass.from_dict( ... {'rh': [1, 2, 3] * u.au, 'delta': [1, 2, 3] * u.au}) >>> row = {'rh': 4 * u.au, 'delta': 4 * u.au, 'phase': 15 * u.deg} >>> data.add_row(row)
- apply(data, name, unit=None)[source]¶
Apply an arbitrarily shaped sequence as additional column to a
DataClassobject and reshape it accordingly.- Parameters:
- datalist or iterable
Quantityobject Data to be added in a new column in form of a one-dimensional list or a two-dimensional nested sequence. Each element in
datacorresponds to one of the rows in the existing data table. If an element ofdatais a list, the corresponding data table row is repeated the same the number of times as there are elements in this sublist. Ifdatais provided as a flat list and has the same length as the current data table,datawill be simply added as a column to the data table and the length of the data table will not change. Ifdatais provided as aQuantityobject (only possible for flat lists), its unit is adopted, unlessunitis specified (not None).- namestr
Name of the new data column.
- unit
unitsobject or str, optional Unit to be applied to the new column. Default:
None
- datalist or iterable
- Returns:
- None
Notes
As a result of this method, the length of the underlying data table will be the same as the length of the flattened
dataparameter.Examples
Imagine the following scenario: you obtain photometric measurements of the same asteroid over a number of nights. The following
Ephemobject summarizes the observations:>>> from sbpy.data import Ephem >>> import astropy.units as u >>> obs = Ephem.from_columns([[2451223, 2451224, 2451226]*u.d, ... [120.1, 121.3, 124.9]*u.deg, ... [12.4, 12.2, 10.8]*u.deg], ... names=('JD', 'RA', 'DEC')) >>> obs <QTable length=3> JD RA DEC d deg deg float64 float64 float64 --------- ------- ------- 2451223.0 120.1 12.4 2451224.0 121.3 12.2 2451226.0 124.9 10.8
After analyzing the observations, you would like to add the measured apparent V-band magnitudes to this object. You have one observation from the first night, two from the second night, and three from the third night. Instead of re-creating
obs,applyoffers a convenient way to supplementobs:>>> obs.apply([[12.1], [12.5, 12.6], [13.5, 13.4, 13.5]], ... name='V', unit='mag') >>> obs <QTable length=6> JD RA DEC V d deg deg mag float64 float64 float64 float64 --------- ------- ------- ------- 2451223.0 120.1 12.4 12.1 2451224.0 121.3 12.2 12.5 2451224.0 121.3 12.2 12.6 2451226.0 124.9 10.8 13.5 2451226.0 124.9 10.8 13.4 2451226.0 124.9 10.8 13.5
Note how the data table has been re-arranged and rows have been duplicated in order to provide the expected shape.
- classmethod from_columns(columns, names, units=None, meta={}, **kwargs)[source]¶
Create
DataClassobject from a sequence. If that sequence is one-dimensional, it is interpreted as a single column; if the sequence is two-dimensional, it is interpreted as a sequence of columns.- Parameters:
- columns: list, `~numpy.ndarray`, tuple, or `~astropy.units.Quantity`
Data that will be ingested in
DataClassobject. A one-dimensional sequence is interpreted as a single column. A two-dimensional sequence is interpreted as a sequence of columns, each of which must have the same length.- names: str or list-like
Field names, must have the same number of names as data columns. Please note that in order to make use of Alternative field names and to ensure compatibility with sbpy functionality, the field names chosen must be in the list of Data Container Field Name Reference.
- units: str or list-like, optional
Unit labels (as provided by
Unit) in which the data provided incolumnswill be stored in the underlying table. If None, the units as provided bycolumnsare used. If the units provided inunitsdiffer from those used incolumns,columnswill be transformed to the units provided inunits. Must have the same length asnamesand the individual data columns incolumns. Default: None- meta: dictionary, optional
Meta data that will be stored in the data table. Default: empty dictionary
- kwargs: additional keyword arguments, optional
Additional keyword arguments that will be passed on to
QTablein the creation of the underlying data table.
- Returns:
DataClassobject
Examples
The following example creates a single-column
Ephemobject.>>> from sbpy.data import Ephem >>> import astropy.units as u >>> eph = Ephem.from_columns([1, 2, 3, 4]*u.au, ... names='a') >>> eph <QTable length=4> a AU float64 ------- 1.0 2.0 3.0 4.0
This example creates a two-column
Ephemobject in which units are assigned using the optionalunitskeyword argument.>>> eph = Ephem.from_columns([[1, 2, 3, 4], ... [90, 50, 30, 10]], ... names=['r', 'alpha'], ... units=['au', 'deg']) >>> eph <QTable length=4> r alpha AU deg float64 float64 ------- ------- 1.0 90.0 2.0 50.0 3.0 30.0 4.0 10.0
If units are provided in
columnsandunits, those units incolumnswill be transformed into those units inunitson a per-column basis.>>> eph = Ephem.from_columns([[1, 2, 3, 4]*u.au, ... [90, 50, 30, 10]*u.deg], ... names=['r', 'alpha'], ... units=['km', 'rad']) >>> eph <QTable length=4> r alpha km rad float64 float64 ------------------ ------------------- 149597870.7 1.5707963267948966 299195741.4 0.8726646259971648 448793612.09999996 0.5235987755982988 598391482.8 0.17453292519943295
- classmethod from_dict(data, meta={}, **kwargs)[source]¶
Create
DataClassobject from dictionary.- Parameters:
- data: `~collections.OrderedDict` or dictionary
Data that will be ingested in
DataClassobject. Each item in the dictionary will form a column in the data table. The item key will be used as column name, the item value, which must be list-like or aQuantityvector, will be used as data. All columns, i.e., all item values, must have the same length. Please note that in order to make use of Alternative field names and to ensure compatibility with sbpy functionality, the field names chosen must be in the list of Data Container Field Name Reference.- meta: dictionary, optional
Meta data that will be stored in the data table. Default: empty dictionary
- kwargs: additional keyword arguments, optional
Additional keyword arguments that will be passed on to
QTablein the creation of the underlying data table.
- Returns:
DataClassobject
Examples
The following example creates a single-row
Orbitobject (the otherDataClassobjects work the exact same way).>>> import astropy.units as u >>> from sbpy.data import Orbit >>> orb = Orbit.from_dict({'a': 2.7674*u.au, ... 'e': 0.0756, ... 'i': 10.59321*u.deg}) >>> orb <QTable length=1> a e i AU deg float64 float64 float64 ------- ------- -------- 2.7674 0.0756 10.59321
A double-row
Orbitexample would look like this:>>> orb = Orbit.from_dict({'a': [2.7674, 3.123]*u.au, ... 'e': [0.0756, 0.021], ... 'i': [10.59321, 3.21]*u.deg}) >>> orb <QTable length=2> a e i AU deg float64 float64 float64 ------- ------- -------- 2.7674 0.0756 10.59321 3.123 0.021 3.21
Note how in this case a list is passed to each key of the dictionary; if a unit is provided for either element in the dictionary, the corresponding
Unithas to be multiplied to this list, forming aQuantityvector.Since dictionaries have no specific order, the ordering of the column in the example above is not defined. If your data table requires a specific order, use
OrderedDict. This example also shows the use of meta data.>>> from collections import OrderedDict >>> orb = Orbit.from_dict(OrderedDict([('a', [2.7674, 3.123]*u.au), ... ('e', [0.0756, 0.021]), ... ('i', [10.59, 3.21]*u.deg)]), ... meta={'targetname': 'asteroid'}) >>> orb <QTable length=2> a e i AU deg float64 float64 float64 ------- ------- ------- 2.7674 0.0756 10.59 3.123 0.021 3.21 >>> orb.meta {'targetname': 'asteroid'} >>> orb.meta['targetname'] 'asteroid'
- classmethod from_file(filename, meta={}, **kwargs)[source]¶
Create
DataClassobject from a file usingread.- Parameters:
- filenamestr
Name of the file that will be read and parsed.
- metadictionary, optional
Meta data that will be stored in the data table. If the data to be read already holds meta data,
metawill be added. Default: empty dictionary- kwargsadditional parameters
Optional parameters that will be passed on to
read.
- Returns:
DataClassobject
Notes
This function is merely a wrapper around
read. Please note that the file formats available (see here for a list of available formats) provide varying support for units and meta data. For instance,basic,csv,html, andlatexdo not provide unit or meta data information. However,fits,cds,daophot,ecsv, andipacdo support units and meta data.Examples
>>> from sbpy.data import DataClass
>>> dat = DataClass.from_file('data.txt', ... format='ascii')
- classmethod from_rows(rows, names, units=None, meta={}, **kwargs)[source]¶
Create
DataClassobject from a sequence. If that sequence is one-dimensional, it is interpreted as a single row; if the sequence is two-dimensional, it is interpreted as a sequence of rows.- Parameters:
- rowslist,
ndarray, or tuple Data that will be ingested in
DataClassobject. A one-dimensional sequence is interpreted as a single row. A two-dimensional sequence is interpreted as a sequence of rows, each of which must have the same length.- namesstr or list
Column names, must have the same number of names as data columns in each row. Please note that in order to make use of Alternative field names and to ensure compatibility with sbpy functionality, the field names chosen must be in the list of Data Container Field Name Reference.
- unitsstr or list-like, optional
Unit labels (as provided by
Unit) in which the data provided inrowswill be stored in the underlying table. If None, the units as provided byrowsare used. If the units provided inunitsdiffer from those used inrows,rowswill be transformed to the units provided inunits. Must have the same length asnamesand the individual data rows inrows. Default: None- metadictionary, optional
Meta data that will be stored in the data table. Default: empty dictionary
- kwargsadditional keyword arguments, optional
Additional keyword arguments that will be passed on to
QTablein the creation of the underlying data table.
- rowslist,
- Returns:
DataClassobject
Examples
The following example creates a single-row
Physobject.>>> from sbpy.data import Phys >>> import astropy.units as u >>> phys = Phys.from_rows([1*u.km, 0.05, 17*u.mag], ... names=['diam', 'pv', 'absmag']) >>> phys <QTable length=1> diam pv absmag km mag float64 float64 float64 ------- ------- ------- 1.0 0.05 17.0
Providing
unitsallows providing unit-less data inrows:>>> phys = Phys.from_rows([[1, 0.05, 17], ... [2, 0.05, 16]], ... names=['diam', 'pv', 'absmag'], ... units=['km', None, 'mag']) >>> phys <QTable length=2> diam pv absmag km mag float64 float64 float64 ------- ------- ------- 1.0 0.05 17.0 2.0 0.05 16.0
- classmethod from_table(table, meta={}, **kwargs)[source]¶
Create
DataClassobject fromTableorQTableobject.- Parameters:
- table
Tableobject Data that will be ingested in
DataClassobject. Must be aTableorQTableobject. Please note that in order to make use of Alternative field names and to ensure compatibility with sbpy functionality, the field names chosen must be in the list of Data Container Field Name Reference.- metadictionary, optional
Meta data that will be stored in the data table. If
tablealready holds meta data,metawill be added. Default: empty dictionary- kwargsadditional keyword arguments, optional
Additional keyword arguments that will be passed on to
QTablein the creation of the underlying data table.
- table
- Returns:
DataClassobject
Examples
>>> from astropy.table import QTable >>> import astropy.units as u >>> from sbpy.data import DataClass >>> tab = QTable([[1,2,3]*u.kg, ... [4,5,6]*u.m/u.s,], ... names=['mass', 'velocity']) >>> dat = DataClass.from_table(tab) >>> dat <QTable length=3> mass velocity kg m / s float64 float64 ------- -------- 1.0 4.0 2.0 5.0 3.0 6.0
- to_file(filename, format='ascii', **kwargs)[source]¶
Write object to a file using
write.- Parameters:
- filenamestr
Name of the file that will be written.
- formatstr, optional
Data format in which the file should be written. Default:
ASCII- kwargsadditional parameters
Optional parameters that will be passed on to
write.
- Returns:
- None
Notes
This function is merely a wrapper around
write. Please note that the file formats available (see here for a list of available formats) provide varying support for units and meta data. For instance,basic,csv,html, andlatexdo not provide unit or meta data information. However,fits,cds,daophot,ecsv, andipacdo support units and meta data.Examples
>>> from sbpy.data import DataClass >>> import astropy.units as u >>> dat = DataClass.from_columns([[1, 2, 3]*u.deg, ... [4, 5, 6]*u.km, ... ['aa', 'bb', 'cc']], ... names=('aa', 'bb', 'cc')) >>> dat.to_file('test.txt')
- verify_fields(field=None)[source]¶
Verify that the fields have the expected units or object types.
See
sbpy.data.Conffor field definitions.- Parameters:
- fieldstring, optional
Only check this field.
- Raises:
astropy.units.UnitsErroron unit error,TypeErroron object error.
- vstack(data, **kwargs)[source]¶
Stack another DataClass object to the end of DataClass
Similar to
vstack, the DataClass object to be stacked doesn’t have to have the same set of columns as the existing object. Thejoin_typekeyword parameter will be used to decide how to process the different sets of columns.Joining will be in-place.
- Parameters:
Examples
>>> import astropy.units as u >>> from sbpy.data import DataClass >>> >>> data1 = DataClass.from_dict( ... {'rh': [1, 2, 3] * u.au, 'delta': [1, 2, 3] * u.au}) >>> data2 = DataClass.from_dict( ... {'rh': [4, 5] * u.au, 'phase': [15, 15] * u.deg}) >>> data1.vstack(data2)