예제 #1
0
def made_dimensionless(exp):
    return [
        False
        if "origin_offset" not in item
        else cp.ScalarQuantity(item["origin_offset"]).quantity.value != 0
        for item in exp["csdm"]["dimensions"]
    ]
예제 #2
0
def test_baseline_polynomial():
    dataset_in = generate_dataset()
    dataset_in.dimensions[0] *= cp.ScalarQuantity("1 ms")
    x_c = dataset_in.dimensions[0].coordinates.value

    # zeroth order
    PS_0 = [sp.baseline.Polynomial(polynomial_dictionary={"c0": 10})]
    dataset_out = sp.SignalProcessor(operations=PS_0).apply_operations(
        dataset=dataset_in.copy()
    )

    _, y0, y1, y2 = dataset_in.to_list()
    _, y0_, y1_, y2_ = dataset_out.to_list()

    for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]):
        assert np.allclose(out_.max() - in_.max(), 10), "Offset failed"

    # first order
    PS_0 = [sp.baseline.Polynomial(polynomial_dictionary={"c0": 30, "c1": 1})]
    dataset_out = sp.SignalProcessor(operations=PS_0).apply_operations(
        dataset=dataset_in.copy()
    )

    _, y0, y1, y2 = dataset_in.to_list()
    _, y0_, y1_, y2_ = dataset_out.to_list()

    for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]):
        assert np.allclose(out_, in_ + 30 + x_c), "Polynomial 1st order failed"

    # second order
    PS_0 = [sp.baseline.Polynomial(polynomial_dictionary={"c0": 1, "c2": 1})]
    dataset_out = sp.SignalProcessor(operations=PS_0).apply_operations(
        dataset=dataset_in.copy()
    )

    _, y0, y1, y2 = dataset_in.to_list()
    _, y0_, y1_, y2_ = dataset_out.to_list()

    for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]):
        assert np.allclose(out_, in_ + 1 + x_c**2), "Polynomial 2nd order failed"

    # third order
    PS_0 = [
        sp.baseline.Polynomial(polynomial_dictionary={"c0": 10, "c3": 2, "c1": 13.1})
    ]
    dataset_out = sp.SignalProcessor(operations=PS_0).apply_operations(
        dataset=dataset_in.copy()
    )

    _, y0, y1, y2 = dataset_in.to_list()
    _, y0_, y1_, y2_ = dataset_out.to_list()

    for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]):
        assert np.allclose(
            out_, in_ + 10 + 13.1 * x_c + 2 * x_c**3
        ), "Polynomial3rd order failed"
예제 #3
0
def test_baseline_polynomial():
    data_in = generate_data()
    data_in.dimensions[0] *= cp.ScalarQuantity("1 ms")
    x_c = data_in.dimensions[0].coordinates.to("s").value

    # zeroth order
    PS_0 = [sp.baseline.Polynomial(x0="10")]
    data_out = sp.SignalProcessor(operations=PS_0).apply_operations(
        data=data_in.copy())

    _, y0, y1, y2 = data_in.to_list()
    _, y0_, y1_, y2_ = data_out.to_list()

    for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]):
        assert np.allclose(out_.max() - in_.max(), 10), "Offset failed"

    py_dict = {
        "function": "baseline",
        "type": "Polynomial",
        "x0": "10.0",
        "dim_index": 0,
    }
    setup_read_write(PS_0[0], py_dict, sp.baseline.Polynomial)

    # first order
    PS_0 = [sp.baseline.Polynomial(x0="30", x1="1 Hz")]
    data_out = sp.SignalProcessor(operations=PS_0).apply_operations(
        data=data_in.copy())

    _, y0, y1, y2 = data_in.to_list()
    _, y0_, y1_, y2_ = data_out.to_list()

    for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]):
        assert np.allclose(out_, in_ + 30 + x_c), "Polynomial 1st order failed"

    py_dict = {
        "function": "baseline",
        "type": "Polynomial",
        "x0": 30.0,
        "x1": "1.0 Hz",
        "dim_index": 0,
    }
    setup_read_write(PS_0[0], py_dict, sp.baseline.Polynomial)

    # second order
    PS_0 = [sp.baseline.Polynomial(x0="1", x2="1 Hz^2")]
    data_out = sp.SignalProcessor(operations=PS_0).apply_operations(
        data=data_in.copy())

    _, y0, y1, y2 = data_in.to_list()
    _, y0_, y1_, y2_ = data_out.to_list()

    for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]):
        assert np.allclose(out_,
                           in_ + 1 + x_c**2), "Polynomial 2nd order failed"

    py_dict = {
        "function": "baseline",
        "type": "Polynomial",
        "x0": 1.0,
        "x2": "1.0 Hz2",
        "dim_index": 0,
    }
    setup_read_write(PS_0[0], py_dict, sp.baseline.Polynomial)

    # third order
    PS_0 = [sp.baseline.Polynomial(x0="10", x3="2 Hz^3", x1="13.1 Hz")]
    data_out = sp.SignalProcessor(operations=PS_0).apply_operations(
        data=data_in.copy())

    _, y0, y1, y2 = data_in.to_list()
    _, y0_, y1_, y2_ = data_out.to_list()

    for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]):
        assert np.allclose(out_, in_ + 10 + 13.1 * x_c +
                           2 * x_c**3), "Polynomial 3rd order failed"
예제 #4
0
def test_method():
    # test-1 single dimension method

    # parse dict with units
    event_dictionary = {
        "fraction": 0.5,
        "freq_contrib": freq_default,
        "magnetic_flux_density": "9.6 T",
        "rotor_frequency": "1 kHz",
        "rotor_angle": "54.735 deg",
    }
    dimension_dictionary = {
        "count": 1024,
        "spectral_width": "100 Hz",
        "reference_offset": "0 GHz",
        "events": [event_dictionary],
    }
    method_dictionary = {
        "name": "test-1-d",
        "description": "Test-1",
        "channels": ["29Si"],
        "spectral_dimensions": [dimension_dictionary],
    }
    the_method = Method.parse_dict_with_units(method_dictionary)
    basic_method_tests(the_method)

    # test-2 two dimensional two events method

    # parse dict with units
    event_dictionary = {
        "fraction": 0.5,
        "freq_contrib": freq_default,
        "magnetic_flux_density": "9.6 T",
        "rotor_frequency": "1 kHz",
        "rotor_angle": "54.735 deg",
    }
    dimension_dictionary = {
        "count": 1024,
        "spectral_width": "100 Hz",
        "reference_offset": "0 GHz",
        "events": [event_dictionary, event_dictionary],
    }
    method_dictionary = {
        "name": "test-1-d",
        "description": "Test-1",
        "channels": ["29Si"],
        "spectral_dimensions": [dimension_dictionary, dimension_dictionary],
    }
    the_method = Method.parse_dict_with_units(method_dictionary)

    # test experiment assignment
    assert the_method.experiment is None

    with pytest.raises(ValidationError, match="Unable to read the data."):
        the_method.experiment = "test"

    data = np.random.rand(100).reshape(10, 10)
    csdm_data = cp.as_csdm(data)

    csdm_data.dimensions[0] *= cp.ScalarQuantity("Hz")
    csdm_data.dimensions[1] *= cp.ScalarQuantity("Hz")
    the_method.experiment = csdm_data

    assert isinstance(the_method.experiment, cp.CSDM)

    csdm_dict = csdm_data.to_dict()
    the_method.experiment = csdm_dict
    assert isinstance(the_method.experiment, cp.CSDM)
    assert the_method.experiment == csdm_data

    # to_dict_with_unit()
    event_dictionary_ = {
        "fraction": 0.5,
        "transition_query": {
            "P": {
                "channel-1": [[-1.0]]
            }
        },
    }
    dimension_dictionary_ = {
        "count": 1024,
        "spectral_width": "100.0 Hz",
        "reference_offset": "0.0 Hz",
        "events": [event_dictionary_, event_dictionary_],
    }
    method_dictionary_ = {
        "name": "test-1-d",
        "description": "Test-1",
        "channels": ["29Si"],
        "magnetic_flux_density": "9.6 T",
        "rotor_frequency": "1000.0 Hz",
        "rotor_angle": "0.9553059660790962 rad",
        "spectral_dimensions": [dimension_dictionary_, dimension_dictionary_],
        "experiment": csdm_data.to_dict(),
    }
    assert the_method.json() == method_dictionary_

    # reduced_dict()
    event_dictionary_ = {
        "fraction": 0.5,
        "freq_contrib": freq_default,
        "magnetic_flux_density": 9.6,
        "rotor_frequency": 1000.0,
        "rotor_angle": 0.9553059660790962,
        "transition_query": {
            "P": {
                "channel-1": [[-1.0]]
            }
        },
    }
    dimension_dictionary_ = {
        "count": 1024,
        "spectral_width": 100.0,
        "reference_offset": 0.0,
        "events": [event_dictionary_, event_dictionary_],
    }
    method_dictionary_ = {
        "name": "test-1-d",
        "description": "Test-1",
        "channels": ["29Si"],
        "spectral_dimensions": [dimension_dictionary_, dimension_dictionary_],
        "experiment": csdm_data.to_dict(),
    }
    assert the_method.reduced_dict() == method_dictionary_

    # update_spectral_dimension_attributes_from_experiment
    the_method.update_spectral_dimension_attributes_from_experiment()
    for i in range(2):
        assert the_method.spectral_dimensions[i].count == 10
        assert the_method.spectral_dimensions[i].spectral_width == 10
        assert the_method.spectral_dimensions[i].reference_offset == 0
        assert the_method.spectral_dimensions[i].origin_offset == 0
예제 #5
0
def test_method():
    # test-1 single dimension method

    # parse dict with units
    method_dictionary = {
        "name": "test-1-d",
        "description": "Test-1",
        "channels": ["29Si"],
        "magnetic_flux_density": "9.6 T",
        "rotor_frequency": "1 kHz",
        "rotor_angle": "54.735 deg",
        "spectral_dimensions": [dimension_dictionary],
    }
    the_method = Method.parse_dict_with_units(method_dictionary)
    basic_method_tests(the_method)

    # test-2 two dimensional two events method

    # parse dict with units
    dimension_dictionary["events"].append(event_dictionary)

    method_dictionary = {
        "name": "test-1-d",
        "description": "Test-1",
        "channels": ["29Si"],
        "magnetic_flux_density": "9.6 T",
        "rotor_frequency": "1 kHz",
        "rotor_angle": "54.735 deg",
        "spectral_dimensions": [dimension_dictionary, dimension_dictionary],
    }
    the_method = Method.parse_dict_with_units(method_dictionary)

    # test experiment assignment
    assert the_method.experiment is None

    with pytest.raises(ValidationError, match="Unable to read the data."):
        the_method.experiment = "test"

    data = np.random.rand(100).reshape(10, 10)
    csdm_data = cp.as_csdm(data)

    csdm_data.x[0] *= cp.ScalarQuantity("Hz")
    csdm_data.x[1] *= cp.ScalarQuantity("Hz")

    the_method.experiment = csdm_data
    the_method.simulation = csdm_data
    assert isinstance(the_method.experiment, cp.CSDM)
    assert isinstance(the_method.simulation, cp.CSDM)

    csdm_dict = csdm_data.to_dict()
    the_method.experiment = csdm_dict
    assert isinstance(the_method.experiment, cp.CSDM)
    assert the_method.experiment == csdm_data

    the_method.simulation = csdm_dict
    assert isinstance(the_method.simulation, cp.CSDM)
    assert the_method.simulation == csdm_data

    # json()
    event_dictionary_ = {
        "fraction": 0.5,
        "transition_query": [{
            "ch1": {
                "P": [-1]
            }
        }]
    }
    dimension_dictionary_ = {
        "count": 1024,
        "spectral_width": "100.0 Hz",
        "events": [event_dictionary_, event_dictionary_],
    }
    method_dictionary_ = {
        "name": "test-1-d",
        "description": "Test-1",
        "channels": ["29Si"],
        "magnetic_flux_density": "9.6 T",
        "rotor_frequency": "1000.0 Hz",
        "rotor_angle": "0.9553059660790962 rad",
        "spectral_dimensions": [dimension_dictionary_, dimension_dictionary_],
        "simulation": csdm_data.to_dict(),
        "experiment": csdm_data.to_dict(),
    }
    serialize = the_method.json()
    serialize["simulation"]["csdm"].pop("timestamp")
    assert serialize == method_dictionary_

    # json(units=False)
    event_dictionary_ = {
        "fraction": 0.5,
        "transition_query": [{
            "ch1": {
                "P": [-1]
            }
        }]
    }
    dimension_dictionary_ = {
        "count": 1024,
        "spectral_width": 100.0,
        "events": [event_dictionary_, event_dictionary_],
    }
    method_dictionary_ = {
        "name": "test-1-d",
        "description": "Test-1",
        "channels": ["29Si"],
        "magnetic_flux_density": 9.6,
        "rotor_frequency": 1000.0,
        "rotor_angle": 0.9553059660790962,
        "spectral_dimensions": [dimension_dictionary_, dimension_dictionary_],
        "simulation": csdm_data.to_dict(),
        "experiment": csdm_data.to_dict(),
    }
    serialize = the_method.json(units=False)
    serialize["simulation"]["csdm"].pop("timestamp")
    assert serialize == method_dictionary_