dl.helpers.utils module

Data Lab utility helper functions.

dl.helpers.utils.convert(inp, outfmt='pandas', verbose=False, **kwargs)[source]

Convert input inp to a data structure defined by outfmt.

Parameters:
  • inp (str) – String representation of the result of a query. Usually this is a CSV-formatted string, but can also be, e.g. an XML-formatted votable (as string)

  • outfmt (str) –

    The desired data structure for converting inp to. Default: ‘pandas’, which returns a Pandas dataframe. Other available conversions are:

    string - no conversion array - Numpy array structarray - Numpy structured array (also called record array) table - Astropy Table votable - Astropy VOtable

    For outfmt=’votable’, the input string must be an XML-formatted string. For all other values, as CSV-formatted string.

  • verbose (bool) – If True, print status message after conversion. Default: False

  • kwargs (optional params) – Will be passed as **kwargs to the converter method.

Example

Convert a CSV-formatted string to a Pandas dataframe

arr = convert(inp,'array')
arr.shape  # arr is a Numpy array

df = convert(inp,outfmt='pandas')
df.head()  # df is as Pandas dataframe, with all its methods

df = convert(inp,'pandas',na_values='Infinity') # na_values is a kwarg; adds 'Infinity' to list of values converter to np.inf
dl.helpers.utils.normalizeCoordinates(x, y, frame_in='icrs', units_in='deg', frame_out=None, wrap_at=180)[source]

Makes 2D spatial coordinates (e.g. RA & Dec) suitable for use with matplotlib’s all-sky projection plotting.

Parameters:
  • x (seq (e.g. tuple,list,1-d array)) – Location of points in (x,y) feature space (e,g, RA & Dec in degrees). Avoid supplying x and y as columns from a pandas dataframe, as this unfortunately makes the coordinate conversions much slower. Numpy arrays, lists, astropy table and votable columns, all are fine.

  • y (seq (e.g. tuple,list,1-d array)) – Location of points in (x,y) feature space (e,g, RA & Dec in degrees). Avoid supplying x and y as columns from a pandas dataframe, as this unfortunately makes the coordinate conversions much slower. Numpy arrays, lists, astropy table and votable columns, all are fine.

  • frame_in (str) – Coordinate frame of x & y. Default: ‘icrs’. ‘galactic’ is also available. If the user desires other frames from astropy.coordinates, please contact __author__.

  • units_in (str) – Units of x & y. Default ‘deg’ (degrees).

  • frame_out (None or str) – If not None, and not same as frame_in, the x & y coordinates will be transformed from frame_in to frame_out.

  • wrap_at (float) – matplotlib plotting functions such as matplotlib.scatter() with all-sky projections expect the x-coordinate (e.g. RA) to be between -180 and +180 degrees (or more precisely: between -pi and +pi). The default wrap_at=180 shifts the input coordinate x (e.g. RA) accordingly.

dl.helpers.utils.resolve(name=None)[source]

Resolve object name to coordinates.

Parameters:

name (str or None) – If str, it is the name of the object to resolve. If None (default), a primpt for the object name will be presented.

Returns:

sc – Instance of SkyCoord from astropy. Get e.g. RA via sc.ra (with units), or sc.ra.value (without units). Or explictly in a different coordinate system, e.g. sc.galactic.b, etc.

Return type:

instance

dl.helpers.utils.vospace_readable_fileobj(name_or_obj, token=None, **kwargs)[source]

Read data from VOSpace or some other place.

Notes

Most of the heavy lifting is done with get_readable_fileobj(). Any additional keywords passed to this function will get passed directly to that function.

Parameters:
  • name_or_obj (str or file-like object) –

    The filename of the file to access (if given as a string), or the file-like object to access.

    If a file-like object, it must be opened in binary mode.

  • token (str) – A token granting access to VOSpace.

Returns:

A readable file-like object.

Return type:

file