1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
# Make sure that session files can be read in a backward-compatible manner
import os
import pytest
import numpy as np
from glue.tests.helpers import requires_astropy, requires_h5py, requires_qt, PYSIDE2_INSTALLED # noqa
from glue.core.component import CoordinateComponent, Component
from glue.core.state import GlueUnSerializer
from glue.core.component_id import PixelComponentID
DATA = os.path.join(os.path.dirname(__file__), 'data')
@requires_qt
@requires_astropy
def test_load_simple_tables_04():
# This loads a session file made with Glue v0.4. In this session, we have
# loaded four tables. The first two are from the same file, but one loaded
# via the auto loader and the other via the Astropy FITS table loader. The
# second two were loaded similarly to the first two, but the file contains
# two HDUs this time. However, in Glue v0.4, only the first HDU was read so
# we shouldn't have access to columns c and d in ``double_tables.fits``.
with open(os.path.join(DATA, 'simple_tables.glu'), 'r') as f:
template = f.read()
content = template.replace('{DATA_PATH}', (DATA + os.sep).replace('\\', '\\\\'))
state = GlueUnSerializer.loads(content)
ga = state.object('__main__')
dc = ga.session.data_collection
# All tables should actually be the same because the FITS reader back at
# 0.4 only read in the first HDU so the new reader is back-compatible
# since it preserves HDU order.
assert len(dc) == 4
assert dc[0].label == 'single_table_auto'
assert dc[1].label == 'single_table'
assert dc[2].label == 'double_tables_auto'
assert dc[3].label == 'double_tables'
np.testing.assert_equal(dc[0]['a'], [1, 2, 3])
np.testing.assert_equal(dc[0]['b'], [4, 5, 6])
np.testing.assert_equal(dc[0]['a'], dc[1]['a'])
np.testing.assert_equal(dc[0]['b'], dc[1]['b'])
np.testing.assert_equal(dc[0]['a'], dc[2]['a'])
np.testing.assert_equal(dc[0]['b'], dc[2]['b'])
np.testing.assert_equal(dc[0]['a'], dc[3]['a'])
np.testing.assert_equal(dc[0]['b'], dc[3]['b'])
ga.close()
@requires_qt
@requires_h5py
def test_load_hdf5_grids_04():
# This loads a session file made with Glue v0.4. In this session, we have
# loaded two gridded datasets from an HDF5 datafile: the first one loaded
# via the auto loader and the other via the FITS/HDF5 loader.
with open(os.path.join(DATA, 'simple_hdf5_grid.glu'), 'r') as f:
template = f.read()
content = template.replace('{DATA_PATH}', (DATA + os.sep).replace('\\', '\\\\'))
state = GlueUnSerializer.loads(content)
ga = state.object('__main__')
dc = ga.session.data_collection
assert len(dc) == 2
assert dc[0].label == 'single_grid_auto'
assert dc[1].label == 'single_grid'
np.testing.assert_equal(dc[0]['/array1'], 1)
np.testing.assert_equal(dc[0]['/array1'].shape, (2, 3, 4))
ga.close()
@requires_qt
@requires_astropy
def test_load_link_helpers_04():
# This loads a session file made with Glue v0.4. In this session, we have
# two tables, and we use all the celestial link functions that were present
# in Glue v0.4. We now check that the paths are patched when loading the
# session (since the functions have been moved to a deprecated location)
with open(os.path.join(DATA, 'session_links.glu'), 'r') as f:
content = f.read()
state = GlueUnSerializer.loads(content)
ga = state.object('__main__')
ga.close()
@requires_qt
@requires_astropy
@pytest.mark.skipif('PYSIDE2_INSTALLED')
def test_load_viewers_04():
# FIXME - for some reason this test with PySide2 causes a leftover reference
# to GlueApplication and appears to be due to x_log being True in the
# scatter plot. I suspect maybe there is some kind of circular reference
# This loads a session file made with Glue v0.4. In this session, we have
# three viewers: one scatter viewer, one image viewer, and one histogram
# viewer.
with open(os.path.join(DATA, 'simple_viewers.glu'), 'r') as f:
content = f.read()
state = GlueUnSerializer.loads(content)
ga = state.object('__main__')
assert len(ga.viewers[0]) == 3
labels = sorted([x.LABEL for x in ga.viewers[0]])
assert labels == ['1D Histogram', '2D Image', '2D Scatter']
viewers = {}
for x in ga.viewers[0]:
viewers[x.LABEL] = x
h = viewers['1D Histogram']
assert h.viewer_size == (1235, 531)
assert h.position == (0, 535)
assert h.state.x_att.label == 'b'
i = viewers['2D Image']
assert i.viewer_size == (562, 513)
assert i.position == (672, 0)
assert i.state.layers[0].attribute.label == "image"
s = viewers['2D Scatter']
assert s.viewer_size == (670, 512)
assert s.position == (0, 0)
assert s.state.x_att.label == 'b'
assert s.state.y_att.label == 'a'
assert s.state.x_log
assert not s.state.y_log
ga.close()
@requires_qt
def test_load_pixel_components_07():
# This loads a session file made with Glue v0.7. In 0.7 and before,
# PixelComponentID did not exist, so we need to make sure that when loading
# in such files, we transform the appropriate ComponentIDs to
# PixelComponentIDs.
with open(os.path.join(DATA, 'glue_v0.7_pixel_roi_selection.glu'), 'r') as f:
content = f.read()
state = GlueUnSerializer.loads(content)
ga = state.object('__main__')
assert isinstance(ga.data_collection[0].pixel_component_ids[0], PixelComponentID)
assert isinstance(ga.data_collection[0].pixel_component_ids[1], PixelComponentID)
ga.close()
@requires_qt
def test_table_widget_010():
from glue.viewers.table.qt.tests.test_data_viewer import check_values_and_color
# This loads a session file made with Glue v0.10 that includes a table
# viewer. This is to make sure that loading table viewers from old files
# will always be backward-compatible.
with open(os.path.join(DATA, 'glue_v0.10_table.glu'), 'r') as f:
state = GlueUnSerializer.load(f)
ga = state.object('__main__')
viewer = ga.viewers[0][0]
data = {'x': [1, 2, 3],
'y': [4, 5, 6]}
colors = ['#e31a1c', '#6d7326', None]
check_values_and_color(viewer.model, data, colors)
ga.close()
@requires_qt
@pytest.mark.parametrize('protocol', (0, 1))
def test_load_log(protocol):
# Prior to Glue v0.13, components were added to the data as: first
# non-coordinate component, then coordinate components, then remaining non-
# coordinate components. In Glue v0.13, this changed to be coordinate
# components then non-coordinate components. The LoadLog functionality
# relies on an absolute component index, so we need to be careful - if the
# session file was created prior to Glue v0.13, we need to load the
# components in the log using the old order. The load_log_1.glu file was
# made with Glue v0.12.2, while the load_log_2.glu file was made with
# Glue v0.13.
with open(os.path.join(DATA, 'load_log_{0}.glu'.format(protocol)), 'r') as f:
template = f.read()
content = template.replace('{DATA_PATH}', (DATA + os.sep).replace('\\', '\\\\'))
state = GlueUnSerializer.loads(content)
ga = state.object('__main__')
dc = ga.session.data_collection
assert len(dc) == 1
data = dc[0]
assert data.label == 'simple'
np.testing.assert_equal(data['Pixel Axis 0 [x]'], [0, 1, 2])
np.testing.assert_equal(data['World 0'], [0, 1, 2])
np.testing.assert_equal(data['a'], [1, 3, 5])
np.testing.assert_equal(data['b'], [2, 2, 3])
if protocol == 0:
assert data.components == [data.id['a'], data.id['Pixel Axis 0 [x]'], data.id['World 0'], data.id['b']]
else:
assert data.components == [data.id['Pixel Axis 0 [x]'], data.id['World 0'], data.id['a'], data.id['b']]
assert type(data.get_component('Pixel Axis 0 [x]')) == CoordinateComponent
assert type(data.get_component('World 0')) == CoordinateComponent
assert type(data.get_component('a')) == Component
assert type(data.get_component('b')) == Component
ga.close()
@requires_qt
@requires_astropy
def test_load_coordinate_link_helpers_013():
# This loads a session file made with Glue v0.13. In this session, we have
# two tables, and we use all the celestial link functions that were present
# in Glue v0.13. We now check that the paths are patched when loading the
# session (since the functions have been moved to a deprecated location)
with open(os.path.join(DATA, 'session_coordinate_links_013.glu'), 'r') as f:
content = f.read()
state = GlueUnSerializer.loads(content)
ga = state.object('__main__')
data1, data2 = ga.session.data_collection
print(data1)
print(data2)
# Check that the links works
data1[data2.id['x']]
data1[data2.id['y']]
data2[data1.id['x']]
data2[data1.id['y']]
ga.close()
@requires_qt
@requires_astropy
def test_load_resave_coords(tmp_path):
# This is a regression test for a bug that caused issues when creating a
# session file with a dataset without any world coordinates (e.g. a table)
# in glue 0.15, then loading and re-saving the session in glue 0.16. The
# issue is that the glue 0.16 file will use LoadLog with protocol 2, but
# since the file was originally created in glue 0.15, it will include a
# fake world coordinate column. LoadLog with protocol 2 was incorrectly
# assumed to never have these world coordinates.
# Load in a file create with glue 0.15.* which includes a world component
# even though Data.coords is the default identity transform
with open(os.path.join(DATA, 'simple_table_015.glu'), 'r') as f:
template = f.read()
content = template.replace('{DATA_PATH}', (DATA + os.sep).replace('\\', '\\\\'))
state = GlueUnSerializer.loads(content)
ga = state.object('__main__')
dc = ga.session.data_collection
assert len(dc) == 1
ga.save_session(tmp_path / 'test.glu')
ga.close()
with open(tmp_path / 'test.glu', 'r') as f:
content = f.read()
state = GlueUnSerializer.loads(content)
ga = state.object('__main__')
dc = ga.session.data_collection
assert len(dc) == 1
ga.close()
@requires_qt
@requires_astropy
def test_load_resave_coords_intermediate(tmp_path):
# Load in a file create with glue 0.15.* which includes a world component
# even though Data.coords is the default identity transform
with open(os.path.join(DATA, 'simple_table_resaved.glu'), 'r') as f:
template = f.read()
content = template.replace('{DATA_PATH}', (DATA + os.sep).replace('\\', '\\\\'))
state = GlueUnSerializer.loads(content)
ga = state.object('__main__')
dc = ga.session.data_collection
assert len(dc) == 1
ga.save_session(tmp_path / 'test.glu')
ga.close()
|