예제 #1
0
def matfile_generator(params, mat_file):
    """
    run through matlab cells and generate AT elements

    KEYWORDS
        mat_file        name of the .mat file
        mat_key         name of the Matlab variable containing the lattice.
                        Default: Matlab variable name if there is only one,
                        otherwise 'RING'
        check=True      if False, skip the coherence tests
        quiet=False     If True, suppress the warning for non-standard classes
    """
    m = scipy.io.loadmat(params.setdefault('mat_file', mat_file))
    matvars = [varname for varname in m if not varname.startswith('__')]
    default_key = matvars[0] if (len(matvars) == 1) else 'RING'
    key = params.setdefault('mat_key', default_key)
    check = params.pop('check', True)
    quiet = params.pop('quiet', False)
    cell_array = m[key].flat
    for index, mat_elem in enumerate(cell_array):
        element_array = mat_elem[0, 0]
        kwargs = {}
        for field_name in element_array.dtype.fields:
            # Remove any surplus dimensions in arrays.
            data = numpy.squeeze(element_array[field_name])
            # Convert strings in arrays back to strings.
            if data.dtype.type is numpy.unicode_:
                data = str(data)
            kwargs[field_name] = data

        yield element_from_dict(kwargs, index=index, check=check, quiet=quiet)
예제 #2
0
파일: matfile.py 프로젝트: T-Nicholls/at
def matfile_generator(params, mat_file):
    """
    run through matlab cells and generate AT elements

    KEYWORDS
        mat_file        name of the .mat file
        mat_key         name of the Matlab variable containing the lattice.
                        Default: Matlab variable name if there is only one,
                        otherwise 'RING'
        check=True      if False, skip the coherence tests
        quiet=False     If True, suppress the warning for non-standard classes
    """
    def mclean(data):
        if data.dtype.type is numpy.str_:
            # Convert strings in arrays back to strings.
            return str(data[0])
        elif data.size == 1:
            v = data[0, 0]
            if issubclass(v.dtype.type, numpy.void):
                for f in v.dtype.fields:
                    v[f] = mclean(v[f])
            # Return a scalar
            return v
        else:
            # Remove any surplus dimensions in arrays.
            return numpy.squeeze(data)

    m = scipy.io.loadmat(params.setdefault('mat_file', mat_file))
    matvars = [varname for varname in m if not varname.startswith('__')]
    default_key = matvars[0] if (len(matvars) == 1) else 'RING'
    key = params.setdefault('mat_key', default_key)
    if key not in m.keys():
        kok = [k for k in m.keys() if '__' not in k]
        raise AtError('Selected mat_key does not exist, '
                      'please select in: {}'.format(kok))
    check = params.pop('check', True)
    quiet = params.pop('quiet', False)
    cell_array = m[key].flat
    for index, mat_elem in enumerate(cell_array):
        elem = mat_elem[0, 0]
        kwargs = {f: mclean(elem[f]) for f in elem.dtype.fields}
        yield element_from_dict(kwargs, index=index, check=check, quiet=quiet)
예제 #3
0
 def var_generator(params, latt):
     for elem in latt:
         yield element_from_dict(elem)
예제 #4
0
def test_sanitise_class_error(elem_kwargs):
    with pytest.raises(AttributeError):
        element_from_dict(elem_kwargs)