Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: to_glue #153

Merged
merged 8 commits into from
Nov 19, 2014
68 changes: 67 additions & 1 deletion spectral_cube/spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ def size(self):
""" Number of elements in the cube """
return self._data.size

def __len__(self):
return self.shape[0]

@property
def ndim(self):
""" Dimensionality of the data """
Expand Down Expand Up @@ -737,7 +740,7 @@ def with_mask(self, mask, inherit_mask=True):
def __getitem__(self, view):

# Need to allow self[:], self[:,:]
if isinstance(view, slice):
if isinstance(view, (slice,int)):
view = (view, slice(None), slice(None))
elif len(view) == 2:
view = view + (slice(None),)
Expand Down Expand Up @@ -1621,6 +1624,69 @@ def to_yt(self, spectral_factor=1.0, nprocs=None, **kwargs):

return ytCube(self, ds, spectral_factor=spectral_factor)

def to_glue(self, name=None, glue_app=None, dataset=None, start_gui=True):
"""
Send data to a new or existing Glue application

Parameters
----------
name : str or None
The name of the dataset within Glue. If None, defaults to
'SpectralCube'. If a dataset with the given name already exists,
a new dataset with "_" appended will be added instead.
glue_app : GlueApplication or None
A glue application to send the data to. If this is not specified,
a new glue application will be started if one does not already
exist for this cube. Otherwise, the data will be sent to the
existing glue application, `self._glue_app`.
dataset : glue.core.Data or None
An existing Data object to add the cube to. This is a good way
to compare cubes with the same dimensions. Supercedes ``glue_app``
start_gui : bool
Start the GUI when this is run. Set to False for testing.
"""
if name is None:
name = 'SpectralCube'

from glue.qt.glue_application import GlueApplication
from glue.core import DataCollection, Data, Component
from glue.core.coordinates import coordinates_from_header
from glue.qt.widgets import ImageWidget

if dataset is not None:
if name in [d.label for d in dataset.components]:
name = name+"_"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this logic just to pick a unique name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. and it's bad, but I just wanted something in place temporarily. Does Glue deal with unique naming automatically?

dataset[name] = self

else:
result = Data(label=name)
result.coords = coordinates_from_header(self.header)

result.add_component(self, name)

if glue_app is None:
if hasattr(self,'_glue_app'):
glue_app = self._glue_app
else:
# Start a new glue session. This will quit when done.
# I don't think the return statement is ever reached, based on
# past attempts [@ChrisBeaumont - chime in here if you'd like]
dc = DataCollection([result])

#start Glue
ga = self._glue_app = GlueApplication(dc)
self._glue_viewer = ga.new_data_viewer(ImageWidget,
data=result)

if start_gui:
self._glue_app.start()

return self._glue_app

glue_app.add_datasets(self._glue_app.data_collection, result)



@property
def header(self):
# Preserve non-WCS information from previous header iteration
Expand Down