helpers module

Helper classes and methods for datalab client.

class helpers.Querist(username='anonymous')[source]
checkAsyncJob()[source]

Check the first async job in the FIFO queue (if queue is not empty).

Parameters:None
Returns:
  • Always returns a 3 tuple. If no async job was in the queue,
  • returns (None,None,None). If there was an async query in the
  • queue but its status did not return ‘COMPLETED’, re-inserts
  • the query at its old position in the queue, and returns
  • (None,None,None). If the status was ‘COMPLETED’, returns the
  • tuple (query result,outfmt,preview).
clearQueue()[source]

Clears the async job queue, i.e. they become unretrievable.

clearToken()[source]

Sets to token to empty string. Useful e.g. before saving a notebook.

output_formats

Pretty-print to STDOUT the available outfmt values.

Parameters:None
Returns:
Return type:Nothing
printMapping()[source]

Pretty-print to STDOUT the available outfmt values.

Parameters:None
Returns:
Return type:Nothing
helpers.constructOutlines(x, y, clusterlabels)[source]

Construct convex hulls (outlines) of points in (x,y) feature space, separately for each class identified by a different ‘label’.

Parameters:
  • y (x,) – Location of points in (x,y) feature space (e,g, RA & Dec).
  • clusterlabels (seq (e.g. tuple,list,1-d array)) – 1-d sequence of cluster membership labels, one per (x,y) pair.
Returns:

outlines – List of outlines, each being an instance of shapely.geometry.polygon.Polygon. Check docstring of plotSkymapScatter() on how to access the vertices.

Return type:

list

Example

Given x & y coordinates as 1d sequences, and clusterlabels a 1d sequence of cluster membership labels:

outlines = constructOutlines(x,y,clusterlabels)
for ol in outlines:
    x_ = np.array(ol.boundary.coords[:])[:,0]
    y_ = np.array(ol.boundary.coords[:])[:,1]
    plt.plot(x_,y_,ls='-',color='r',lw=2)
helpers.convert(inp, outfmt='pandas')[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.

Example

Convert a CSV-formatted string to a Pandas dataframe

df = helpers.convert(inpst,outfmt='pandas')
print df.head()  # df is as Pandas dataframe, with all its methods
helpers.findClusters(x, y, method='MiniBatchKMeans', **kwargs)[source]

Find 2D clusters from x & y data.

Parameters:
  • y (x,) – Location of points in (x,y) feature space, e,g, RA & Dec, but x & y need not be spatial in nature.
  • method (str) – Cluster finding method from sklearn.cluster to use. Default: ‘MiniBatchKMeans’ (a streaming implementation of KMeans), which is very fast, but not the most robust. ‘DBSCAN’ is much more robust, but MUCH slower. For other methods, consult sklearn.cluster.
  • **kwargs

    Any other keyword arguments will be passed to the cluster finding method. If method=’MiniBatchKMeans’ or ‘KMeans’, n_clusters (integer number of clusters to find) must be passed, e.g.

    clusters = findClusters(x,y,method='MiniBatchKMeans',n_clusters=3)
    
helpers.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:
  • y (x,) – 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.
helpers.plotSkymapScatter(x, y, c=None, clusterlabels=None, s=3, plot='both', xlabel='RA', ylabel='Dec', clabel='', title='', projection='aitoff', **kwargs)[source]

Plot an all-sky projection of data x,y.

Parameters:
  • y (x,) – 1-d sequence of data (both same length), typically RA & Dec, to plotted via scatter onto an all-sky projection. If c=None and clusterlabels=None, the plot is a simple scatter plot.
  • c (seq or None) – If not None, c is a 1-d sequence of the same length as x & y, and will be used as the value for the colormap applied to the datapoints (x,y). If c is not None, a colorbar will be plotted alongside the sky map.
  • clusterlabels (seq or None) – If not None, clusterlabels is a 1-d sequence of same length as x & y, and carries ‘label’ values for each (x,y) pair designating that datapoint as member of some cluster or class. In this case, all scatter points with the same value in clusterlabels will be plotted in the same color.
  • s (float) – Marker size for scatter plot. Will be passed to matplotlib’s scatter(). Default: s=3.
  • plot (str) – Either ‘both’ (default) or ‘scatter’ or ‘outlines’. If clusterlabels is not none, then plot=’outlines’ or plot=’both’ triggers the computation of outlines (as convex hulls) around all clusters identified by the labels in clusterlabels. If plot=’both’, the scatter points and the outlines will be plotted. If plot=’outlines’, only the outlines will be plotted.
  • ylabel (xlabel,) – x and y labels to be plotted. Defaults: ‘RA’ and ‘Dec’. To turn off labels, supply ‘’.
  • clabel (str) – Colorbar label (if c is not None).
  • title (str) – Figure title. Default ‘’.
  • projection (str) –

    Projection of the all-sky map. Default: ‘aitoff’. For other options, check:

    from matplotlib import projections
    projections.get_projection_names()
       [u'aitoff', u'hammer', u'lambert', u'mollweide', u'polar', u'rectilinear']
    
  • **kwargs (keyword arguments) – All kwargs will be passed on to pylab.scatter().