Ejemplo n.º 1
0
def _convert_run_describer_v1_like_dict_to_v0_like_dict(
        new_desc_dict: RunDescriberV1Dict) -> Dict[str, Any]:
    """
    This function takes the given dict which is expected to be
    representation of `RunDescriber` with `InterDependencies_` (underscore!)
    object and without "version" field, and converts it to a dict that is a
    representation of the `RunDescriber` object with `InterDependencies`
    (no underscore!) object and without "version" field.
    """
    new_desc_dict = new_desc_dict.copy()
    # We intend to use conversion methods from `serialization` module,
    # but those work only with RunDescriber representations that have
    # "version" field. So first, the "version" field with correct value is
    # added.
    new_desc_dict['version'] = 1
    # Out of that dict we create RunDescriber object of the current version
    # (regardless of what the current version is).
    new_desc = serial.from_dict_to_current(new_desc_dict)
    # The RunDescriber of the current version gets converted to a dictionary
    # that represents a RunDescriber object of version 0 - this is the one
    # that has InterDependencies object in it (not the InterDependencies_ one).
    old_desc_dict = cast(Dict[str, Any],
                         serial.to_dict_as_version(new_desc, 0))
    # Lastly, the "version" field is removed.
    old_desc_dict.pop('version')
    return old_desc_dict
Ejemplo n.º 2
0
def test_construct_current_rundescriber_from_v3(some_interdeps):
    interdeps_ = some_interdeps[0]
    interdeps = new_to_old(interdeps_)

    v3 = RunDescriberV3Dict(interdependencies=interdeps._to_dict(),
                            interdependencies_=interdeps_._to_dict(),
                            version=3,
                            shapes=None)
    rds1 = RunDescriber._from_dict(v3)
    rds_upgraded = from_dict_to_current(v3)
    assert rds1._to_dict() == v3
    assert rds_upgraded._to_dict() == v3
Ejemplo n.º 3
0
def test_construct_currect_rundesciber_from_v2(some_interdeps):
    interdeps_ = some_interdeps[0]
    interdeps = new_to_old(interdeps_)

    v2 = RunDescriberV2Dict(interdependencies=interdeps._to_dict(),
                            interdependencies_=interdeps_._to_dict(),
                            version=2)
    rds1 = RunDescriber._from_dict(v2)
    rds2 = from_dict_to_current(v2)

    assert rds1._to_dict() == v2
    assert rds2._to_dict() == v2
Ejemplo n.º 4
0
def test_construct_currect_rundesciber_from_v1(some_interdeps):
    interdeps_ = some_interdeps[0]
    interdeps = new_to_old(interdeps_)

    v1 = RunDescriberV1Dict(interdependencies=interdeps_._to_dict(), version=1)
    rds1 = RunDescriber._from_dict(v1)
    rds2 = from_dict_to_current(v1)

    expected_v2_dict = RunDescriberV2Dict(
        interdependencies=interdeps._to_dict(),
        interdependencies_=interdeps_._to_dict(),
        version=2)
    assert rds1._to_dict() == expected_v2_dict
    assert rds2._to_dict() == expected_v2_dict
Ejemplo n.º 5
0
def test_construct_currect_rundesciber_from_fake_v3(some_interdeps):
    interdeps_ = some_interdeps[0]
    interdeps = new_to_old(interdeps_)

    v3 = RunDescriberV2Dict(interdependencies=interdeps._to_dict(),
                            interdependencies_=interdeps_._to_dict(),
                            version=3)
    v3['foobar'] = {"foo": ["bar"]}
    rds1 = RunDescriber._from_dict(v3)
    rds2 = from_dict_to_current(v3)
    v2 = v3.copy()
    v2.pop('foobar')
    v2['version'] = 2
    assert rds1._to_dict() == v2
    assert rds2._to_dict() == v2
Ejemplo n.º 6
0
def test_construct_current_rundescriber_from_v1(some_interdeps):
    interdeps_ = some_interdeps[0]
    interdeps = new_to_old(interdeps_)

    v1 = RunDescriberV1Dict(interdependencies=interdeps_._to_dict(), version=1)
    rds1 = RunDescriber._from_dict(v1)
    rds_upgraded = from_dict_to_current(v1)

    expected_v3_dict = RunDescriberV3Dict(
        interdependencies=interdeps._to_dict(),
        interdependencies_=interdeps_._to_dict(),
        version=3,
        shapes=None,
    )
    assert rds1._to_dict() == expected_v3_dict
    assert rds_upgraded._to_dict() == expected_v3_dict
Ejemplo n.º 7
0
def test_construct_current_rundescriber_from_fake_v4(some_interdeps):
    interdeps_ = some_interdeps[0]
    interdeps = new_to_old(interdeps_)

    v4 = RunDescriberV3Dict(interdependencies=interdeps._to_dict(),
                            interdependencies_=interdeps_._to_dict(),
                            version=4,
                            shapes=None)
    v4['foobar'] = {"foo": ["bar"]}
    rds1 = RunDescriber._from_dict(v4)
    rds_upgraded = from_dict_to_current(v4)
    v3 = v4.copy()
    v3.pop('foobar')
    v3['version'] = 3
    assert rds1._to_dict() == v3
    assert rds_upgraded._to_dict() == v3
Ejemplo n.º 8
0
def test_construct_currect_rundesciber_from_v0(some_paramspecs):

    pgroup1 = some_paramspecs[1]

    interdeps = InterDependencies(pgroup1['ps1'], pgroup1['ps2'],
                                  pgroup1['ps3'], pgroup1['ps4'],
                                  pgroup1['ps6'])
    v0 = RunDescriberV0Dict(interdependencies=interdeps._to_dict(), version=0)
    rds1 = RunDescriber._from_dict(v0)

    rds2 = from_dict_to_current(v0)

    expected_v2_dict = RunDescriberV2Dict(
        interdependencies=interdeps._to_dict(),
        interdependencies_=old_to_new(interdeps)._to_dict(),
        version=2)
    assert DeepDiff(rds1._to_dict(), expected_v2_dict, ignore_order=True) == {}
    assert DeepDiff(rds2._to_dict(), expected_v2_dict, ignore_order=True) == {}
Ejemplo n.º 9
0
def get_parameter_data(conn: ConnectionPlus,
                       run_table_name: str,
                       run_description: dict,
                       columns: Sequence[str] = ()):
    """
    Get data for one or more parameters and its dependencies. The data
    is returned as numpy arrays within 2 layers of nested dicts. The keys of
    the outermost dict are the requested parameters and the keys of the second
    level are the loaded parameters (requested parameter followed by its
    dependencies).

    Start and End allows one to specify a range of rows to
    be returned (1-based indexing, both ends are included). The range filter
    is applied AFTER the NULL values have been filtered out.
    Be aware that different parameters that are independent of each other
    may return a different number of rows.

    Note that this assumes that all array type parameters have the same length.
    This should always be the case for a parameter and its dependencies.
    Note that all numeric data will at the moment be returned as floating point
    values.

    Args:
        conn: database connection
        table_name: name of the table
        columns: list of columns. If no columns are provided, all parameters
            are returned.
    """

    rd = from_dict_to_current(run_description)
    interdeps = rd.interdeps

    datadict = {}
    if len(columns) == 0:
        columns = [ps.name for ps in interdeps.non_dependencies]

    # loop over all the requested parameters
    for param in columns:
        param_spec = interdeps._id_to_paramspec[param]
        # find all the dependencies of this param
        paramspecs = [param_spec] \
                   + list(interdeps.dependencies.get(param_spec, ()))
        param_names = [param.name for param in paramspecs]
        types = [param.type for param in paramspecs]

        results = get_parameter_tree_values(conn, run_table_name, param,
                                            *param_names[1:])

        # if we have array type parameters expand all other parameters
        # to arrays
        if 'array' in types and ('numeric' in types or 'text' in types
                                 or 'complex' in types):
            first_array_element = types.index('array')
            numeric_elms = [i for i, x in enumerate(types) if x == "numeric"]
            complex_elms = [i for i, x in enumerate(types) if x == 'complex']
            text_elms = [i for i, x in enumerate(types) if x == "text"]
            for row in results:
                for element in numeric_elms:
                    row[element] = np.full_like(row[first_array_element],
                                                row[element],
                                                dtype=np.float)

                for element in complex_elms:
                    row[element] = np.full_like(row[first_array_element],
                                                row[element],
                                                dtype=np.complex)
                for element in text_elms:
                    strlen = len(row[element])
                    row[element] = np.full_like(row[first_array_element],
                                                row[element],
                                                dtype=f'U{strlen}')

        results_t = map(list, zip(*results))

        datadict[param] = {
            name: np.array(column_data)
            for name, column_data in zip(param_names, results_t)
        }

    return datadict