Exemplo n.º 1
0
def test_encode_decode_roundtrip():
    for dt in [np.float32, np.float64, np.int64]:
        for shape in [(12,), (2, 6), (2,2,3)]:
            a = np.arange(12, dtype=dt)
            a.reshape(shape)
            d = bus.encode_base64_dict(a)
            aa = bus.decode_base64_dict(d)
            assert np.array_equal(a, aa)
Exemplo n.º 2
0
def test_encode_decode_roundtrip():
    for dt in [np.float32, np.float64, np.int64]:
        for shape in [(12, ), (2, 6), (2, 2, 3)]:
            a = np.arange(12, dtype=dt)
            a.reshape(shape)
            d = bus.encode_base64_dict(a)
            aa = bus.decode_base64_dict(d)
            assert np.array_equal(a, aa)
Exemplo n.º 3
0
def test_decode_base64_dict(dt, shape):
    a = np.arange(12, dtype=dt)
    a.reshape(shape)
    data = base64.b64encode(a).decode('utf-8')
    d = {'__ndarray__': data, 'dtype': a.dtype.name, 'shape': a.shape}
    aa = bus.decode_base64_dict(d)

    assert aa.shape == a.shape

    assert aa.dtype.name == a.dtype.name

    assert np.array_equal(a, aa)
Exemplo n.º 4
0
def get_plot_data(data):
    """
    Parse the dictionary "data" received from a GET request
    to api/internal/plot/spectroscopy/<obj_id> and recover
    the underlying data for all spectra in the plot.

    Parameters
    ----------
    data : dict
       A dictionary loaded from the JSON file sent by the plotting API call.

    Returns
    -------
    list
        Returns a list of dictionaries with the flux, wavelength, etc.,
        one dictionary per spectrum.
        Note that the plot outputs several repeated versions of each spectrum,
        i.e., one for the step plot, one for the line plot with the tooltip,
        one for the smoothed spectrum. Each one is a separate dictionary.
    """
    objects = []
    for doc in data['data']['bokehJSON']['doc']['roots']['references']:
        if 'data' in doc['attributes']:
            new_obj = {}
            # go over each attribute with data and look for these keys
            for key in ['wavelength', 'flux', 'flux_original', 'x', 'y']:
                if key in doc['attributes']['data']:
                    array = doc['attributes']['data'][key]
                    if type(array) == list:
                        new_obj[key] = np.array(array)
                    else:
                        new_obj[key] = serialization.decode_base64_dict(array)

            # tooltip data that's duplicated so we only need to get the first item in each array
            for key in [
                    'id',
                    'telescope',
                    'instrument',
                    'origin',
                    'date_observed',
                    'pi',
                    'annotations',
                    'altdata',
            ]:
                if key in doc['attributes']['data']:
                    new_obj[key] = doc['attributes']['data'][key][0]

            if 'telescope' in new_obj:
                objects.append(new_obj)

    return objects
Exemplo n.º 5
0
def test_decode_base64_dict():
    for dt in [np.float32, np.float64, np.int64]:
        for shape in [(12, ), (2, 6), (2, 2, 3)]:
            a = np.arange(12, dtype=dt)
            a.reshape(shape)
            data = base64.b64encode(a).decode('utf-8')
            d = {'__ndarray__': data, 'dtype': a.dtype.name, 'shape': a.shape}
            aa = bus.decode_base64_dict(d)

            assert aa.shape == a.shape

            assert aa.dtype.name == a.dtype.name

            assert np.array_equal(a, aa)
Exemplo n.º 6
0
def test_decode_base64_dict(dt, shape):
    a = np.arange(12, dtype=dt)
    a.reshape(shape)
    data = base64.b64encode(a).decode('utf-8')
    d = {
        '__ndarray__'  : data,
        'dtype'        : a.dtype.name,
        'shape'        : a.shape
    }
    aa = bus.decode_base64_dict(d)

    assert aa.shape == a.shape

    assert aa.dtype.name == a.dtype.name

    assert np.array_equal(a, aa)
Exemplo n.º 7
0
def test_decode_base64_dict():
    for dt in [np.float32, np.float64, np.int64]:
        for shape in [(12,), (2, 6), (2,2,3)]:
            a = np.arange(12, dtype=dt)
            a.reshape(shape)
            data = base64.b64encode(a).decode('utf-8')
            d = {
                '__ndarray__'  : data,
                'dtype'        : a.dtype.name,
                'shape'        : a.shape
            }
            aa = bus.decode_base64_dict(d)

            assert aa.shape == a.shape

            assert aa.dtype.name == a.dtype.name

            assert np.array_equal(a, aa)
Exemplo n.º 8
0
def test_encode_decode_roundtrip(dt, shape) -> None:
    a = np.arange(12, dtype=dt)
    a.reshape(shape)
    d = bus.encode_base64_dict(a)
    aa = bus.decode_base64_dict(d)
    assert np.array_equal(a, aa)
Exemplo n.º 9
0
def test_encode_decode_roundtrip(dt, shape):
    a = np.arange(12, dtype=dt)
    a.reshape(shape)
    d = bus.encode_base64_dict(a)
    aa = bus.decode_base64_dict(d)
    assert np.array_equal(a, aa)