예제 #1
0
def test_data_collection_combo_helper():

    callback = MagicMock()
    state = ExampleState()
    state.add_callback('combo', callback)

    dc = DataCollection([])

    helper = DataCollectionComboHelper(state, 'combo', dc)  # noqa

    data1 = Data(x=[1, 2, 3], y=[2, 3, 4], label='data1')

    assert callback.call_count == 0

    dc.append(data1)

    assert callback.call_count == 1

    assert selection_choices(state, 'combo') == "data1"

    data1.label = 'mydata1'
    assert selection_choices(state, 'combo') == "mydata1"

    assert callback.call_count == 2

    dc.remove(data1)

    assert callback.call_count == 3

    assert selection_choices(state, 'combo') == ""
예제 #2
0
def test_data_collection_combo_helper():

    callback = MagicMock()
    state = ExampleState()
    state.add_callback('combo', callback)

    dc = DataCollection([])

    helper = DataCollectionComboHelper(state, 'combo', dc)  # noqa

    data1 = Data(x=[1, 2, 3], y=[2, 3, 4], label='data1')

    assert callback.call_count == 0

    dc.append(data1)

    assert callback.call_count == 1

    assert selection_choices(state, 'combo') == "data1"

    data1.label = 'mydata1'
    assert selection_choices(state, 'combo') == "mydata1"

    assert callback.call_count == 2

    dc.remove(data1)

    assert callback.call_count == 3

    assert selection_choices(state, 'combo') == ""
예제 #3
0
def _load_GALFAHI_data_LowRes(filename, **kwargs):
    # Data loader customized for GALFA-HI data cube
    # Resize the data cube into lower resolution in velocity/space

    def _bin_cube(cube, factor, axis_label):
        # resize the cube to lower resolution
        shape = cube.shape
        if axis_label == 'VELO':
            new_shape = (shape[0]/factor, factor, shape[1], shape[2])
            return cube.reshape(new_shape).mean(axis = 1)
        elif axis_label == 'RADEC':
            new_shape = (shape[0], shape[1]/factor, factor, shape[2]/factor, factor)
            return cube.reshape(new_shape).mean(axis = 4).mean(axis = 2)
        else: return cube

    # change the header for those cubes that has been binned into low resolutions
    def _get_new_header(header, factor, axis_label):
        new_header = header
        if axis_label == 'VELO':
            new_header['NAXIS3'] = header['NAXIS3'] / factor
            new_header['CRVAL3'] = header['CRVAL3']
            new_header['CRPIX3'] = float(header['CRPIX3'] / factor)
            new_header['CDELT3'] = header['CDELT3'] * factor
        elif axis_label == 'RADEC':
            for ax in [1, 2]:
                new_header['NAXIS%d'%(ax)] = header['NAXIS%d'%(ax)] / factor
                new_header['CRVAL%d'%(ax)] = header['CRVAL%d'%(ax)]
                new_header['CRPIX%d'%(ax)] = float(header['CRPIX%d'%(ax)] / factor)
                new_header['CDELT%d'%(ax)] = header['CDELT%d'%(ax)] * factor
        else: new_header = header
        # m/s --> km/s
        new_header['CDELT3'] = new_header['CDELT3'] * (10**(-3))
        return new_header

    def _get_cube_center(header, cubeshape):
        ra  = header['CRVAL1'] + header['CDELT1'] * (np.arange(cubeshape[2])+0.5 - header['CRPIX1'])              ## degree
        dec = header['CRVAL2'] + header['CDELT2'] * (np.arange(cubeshape[1])+0.5 - header['CRPIX2'])            ## degree
        return np.mean(ra), np.mean(dec)

    data_list = []
    # add 3 data objects with different resolutions:
    for factor, axis_label in zip([4, 16, 2], ['VELO', 'VELO', 'RADEC']):
        cube = fits.getdata(filename)
        header = fits.getheader(filename)
        cen_ra, cen_dec = _get_cube_center(header, cube.shape)
        new_header = _get_new_header(header, factor, axis_label)
        cube_name = 'G_%d%+.2fradec_%.1fkm/s_%.1fa' % (cen_ra, cen_dec, new_header['CDELT3'], new_header['CDELT2']*60.)

        data = Data()
        data.coords = coordinates_from_header(new_header)
        data.add_component(_bin_cube(cube, factor, axis_label), cube_name)
        data.label  = cube_name
        data_list.append(data)
        del data, cube, header
    return data_list
예제 #4
0
def open_where_dataset_as_glue(dataset_vars):

    dset = data.Dataset(**dataset_vars)

    # Set where config
    import where

    rundate = dataset_vars["rundate"]

    # Add fields of dataset as components to glue
    components = dict()

    # As 1-d tables
    for field in dset.fields:
        try:
            values = dset[field]
        except:
            print("Not able to add", field)
            continue

        if isinstance(values, table.Table):
            continue

        if isinstance(values, time.Time):
            values = values.mjd

        try:
            suffixes = "x y z" if values.shape[
                1] == 3 else "1_x 1_y 1_z 2_x 2_y 2_z"
            for column, suffix in zip(values.T, suffixes.split()):
                components[field + "_" + suffix] = column
                print("Adding {}".format(field + "_" + suffix))
        except (AttributeError, IndexError):
            try:
                len(values)
            except TypeError:
                print("Not able to add", field)
                continue

            components[field] = values
            print("Adding {}: {}".format(field, type(values)))

    glue_data = Data(**components)
    glue_data.label = "{tech} {stage} {rundate:%Y%m%d} {name}".format(
        name=dset.name, **dataset_vars)
    return glue_data
예제 #5
0
def test_data_collection_combo_helper():

    combo = QtWidgets.QComboBox()

    dc = DataCollection([])

    helper = DataCollectionComboHelper(combo, dc)

    data1 = Data(x=[1,2,3], y=[2,3,4], label='data1')

    dc.append(data1)

    assert _items_as_string(combo) == "data1"

    data1.label = 'mydata1'
    assert _items_as_string(combo) == "mydata1"

    dc.remove(data1)

    assert _items_as_string(combo) == ""
예제 #6
0
def test_data_collection_combo_helper():

    combo = QtWidgets.QComboBox()

    dc = DataCollection([])

    helper = DataCollectionComboHelper(combo, dc)

    data1 = Data(x=[1, 2, 3], y=[2, 3, 4], label='data1')

    dc.append(data1)

    assert combo_as_string(combo) == "data1"

    data1.label = 'mydata1'
    assert combo_as_string(combo) == "mydata1"

    dc.remove(data1)

    assert combo_as_string(combo) == ""
예제 #7
0
def test_data_collection_combo_helper():

    state = ExampleState()

    dc = DataCollection([])

    helper = DataCollectionComboHelper(state, 'combo', dc)  # noqa

    data1 = Data(x=[1, 2, 3], y=[2, 3, 4], label='data1')

    dc.append(data1)

    assert selection_choices(state, 'combo') == "data1"

    data1.label = 'mydata1'
    assert selection_choices(state, 'combo') == "mydata1"

    dc.remove(data1)

    assert selection_choices(state, 'combo') == ""
예제 #8
0
def test_manual_data_combo_helper(initialize_data_collection):

    # The case with initialize_data_collection=False is a regression test for a
    # bug which meant that when a ManualDataComboHelper was initialized without
    # a data collection, it did not change when a data object added later has a
    # label changed.

    callback = MagicMock()
    state = ExampleState()
    state.add_callback('combo', callback)

    dc = DataCollection([])

    if initialize_data_collection:
        helper = ManualDataComboHelper(state, 'combo', dc)
    else:
        helper = ManualDataComboHelper(state, 'combo')

    data1 = Data(x=[1, 2, 3], y=[2, 3, 4], label='data1')

    dc.append(data1)

    assert callback.call_count == 0

    assert selection_choices(state, 'combo') == ""

    helper.append_data(data1)
    assert callback.call_count == 1

    assert selection_choices(state, 'combo') == "data1"

    data1.label = 'mydata1'
    assert selection_choices(state, 'combo') == "mydata1"
    assert callback.call_count == 2

    if initialize_data_collection:

        dc.remove(data1)

        assert selection_choices(state, 'combo') == ""
        assert callback.call_count == 3
예제 #9
0
def test_manual_data_combo_helper(initialize_data_collection):

    # The case with initialize_data_collection=False is a regression test for a
    # bug which meant that when a ManualDataComboHelper was initialized without
    # a data collection, it did not change when a data object added later has a
    # label changed.

    callback = MagicMock()
    state = ExampleState()
    state.add_callback('combo', callback)

    dc = DataCollection([])

    if initialize_data_collection:
        helper = ManualDataComboHelper(state, 'combo', dc)
    else:
        helper = ManualDataComboHelper(state, 'combo')

    data1 = Data(x=[1, 2, 3], y=[2, 3, 4], label='data1')

    dc.append(data1)

    assert callback.call_count == 0

    assert selection_choices(state, 'combo') == ""

    helper.append_data(data1)
    assert callback.call_count == 1

    assert selection_choices(state, 'combo') == "data1"

    data1.label = 'mydata1'
    assert selection_choices(state, 'combo') == "mydata1"
    assert callback.call_count == 2

    if initialize_data_collection:

        dc.remove(data1)

        assert selection_choices(state, 'combo') == ""
        assert callback.call_count == 3
예제 #10
0
def test_manual_data_combo_helper():

    combo = QtGui.QComboBox()

    dc = DataCollection([])

    helper = ManualDataComboHelper(combo, dc)

    data1 = Data(x=[1, 2, 3], y=[2, 3, 4], label='data1')

    dc.append(data1)

    assert _items_as_string(combo) == ""

    helper.append(data1)

    assert _items_as_string(combo) == "data1"

    data1.label = 'mydata1'
    assert _items_as_string(combo) == "mydata1"

    dc.remove(data1)

    assert _items_as_string(combo) == ""
예제 #11
0
def _load_GALFAHI_data(filename, **kwargs):
    def _get_cube_center(header, cubeshape):
        ra  = header['CRVAL1'] + header['CDELT1'] * (np.arange(cubeshape[2])+0.5 - header['CRPIX1'])              ## degree
        dec = header['CRVAL2'] + header['CDELT2'] * (np.arange(cubeshape[1])+0.5 - header['CRPIX2'])            ## degree
        return np.mean(ra), np.mean(dec)

    # add the primary components
    cube = fits.getdata(filename)
    header = fits.getheader(filename)
    header['CDELT3'] = header['CDELT3'] * (10**(-3))        # m/s --> km/s
    cen_ra, cen_dec = _get_cube_center(header, cube.shape)
    nn = filename.split('/')[-1]
    # cube_name = '%s_RA%dDEC%d' % (nn[0:3], cen_ra, cen_dec)
    cube_name = 'G_%d%+.2fradec_%.1fkm/s_%.1fa' % (cen_ra, cen_dec, header['CDELT3'], header['CDELT2']*60.)

    data = Data()
    data.coords = coordinates_from_header(header)
    data.add_component(cube, cube_name)
    data.label  = cube_name

    data_list = []
    data_list.append(data)
    data_list.append(data)
    return data_list
예제 #12
0
    def receive_message(self, message):
        """ Receives each DataMessage relay """
        print("    MyClient received a message \n")


# create objects
client = MyClient()
data = Data()
subset = data.new_subset()
data_collection = DataCollection()

# connect them to each other
hub = data_collection.hub
data_collection.append(data)
client.register_to_hub(hub)

# manually send a DataMessage. Relayed to MyClient
print('Manually sending DataMessage')
message = DataMessage(data)
hub.broadcast(message)

# modify the data object. Automatically generates a DataMessage
print('Automatically triggering DataMessage')
data.label = "New label"

# send a SubsetMessage to the Hub.
print('Manually sending SubsetMessage')
message = SubsetMessage(subset)
hub.broadcast(message)  # nothing is printed
예제 #13
0
    def receive_message(self, message):
        """ Receives each DataMessage relay """
        print "    MyClient received a message \n"


# create objects
hub = Hub()
client = MyClient()
data = Data()
subset = data.new_subset()
data_collection = DataCollection()

# connect them to each other
data_collection.append(data)
data_collection.register_to_hub(hub)
client.register_to_hub(hub)

# manually send a DataMessage. Relayed to MyClient
print 'Manually sending DataMessage'
message = DataMessage(data)
hub.broadcast(message)

#modify the data object. Automatically generates a DataMessage
print 'Automatically triggering DataMessage'
data.label = "New label"

#send a SubsetMessage to the Hub.
print 'Manually sending SubsetMessage'
message = SubsetMessage(subset)
hub.broadcast(message) # nothing is printed
예제 #14
0
def frame_to_data(frame):
    data = Data(red=frame[::-1,::-1,2],
                 green=frame[::-1,::-1,1],
                 blue=frame[::-1,::-1,0])
    data.label = "Webcam Snapshot"
    return data