コード例 #1
0
    def save(self, *args):
        self._ids = [id(a) for a in args]
        self._objs = list(args)
        self.search(self.symtable)
        self.fh = h5py.File(self.fname, 'a')
        try:
            self.fh.attrs['datatype'] = 'LarchSaveFile'
            self.fh.attrs['version'] = '1.0.0'
        except:
            pass
        for nam, obj in self.out.items():
            try:
                self.add_data(self.fh, nam, obj)
            except:
                print('Could not save ', nam)

        for obj in self._objs:
            nam = getattr(obj, '__name__', hex(id(obj)))
            nam = fixName(nam, allow_dot=False)
            if nam.startswith('0x'):
                nam = 'obj_%s' % nam[2:]
            try:
                self.add_data(self.fh, nam, obj)
            except:
                print('Could not save ', nam)

        self.fh.close()
コード例 #2
0
 def add_component(key, val, top):
     parents = [fixName(w, allow_dot=False) for w in key.split('/')]
     current = parents.pop()
     for word in parents:
         if not hasattr(top, word):
             setattr(top, word, group())
         top = getattr(top, word)
     tname = top.__name__
     if isinstance(val, h5py.Group):
         setattr(top, current, group(name="%s/%s" % (tname, current)))
         if len(val.attrs) > 0:
             getattr(top, current)._attrs = dict(val.attrs)
     else:
         dat = fh.get(key)
         try:
             if dat.dtype.type == numpy.string_:
                 if len(dat) == 1:
                     dat = dat.value
                 else:
                     dat = list(dat)
         except (ValueError, TypeError):
             pass
         setattr(top, current, dat)
         if len(val.attrs) > 0:
             setattr(top, "%s_attrs" % current, dict(val.attrs))
コード例 #3
0
ファイル: hdf5group.py プロジェクト: maurov/xraylarch
 def add_component(key, val, top):
     parents = [fixName(w, allow_dot=False) for w in key.split('/')]
     current = parents.pop()
     for word in parents:
         if not hasattr(top, word):
             setattr(top, word, group())
         top = getattr(top, word)
     tname = top.__name__
     if isinstance(val, h5py.Group):
         setattr(top, current,  group(name="%s/%s" % (tname, current)))
         if len(val.attrs) > 0:
             getattr(top, current)._attrs = dict(val.attrs)
     else:
         dat = fh.get(key)
         try:
             if dat.dtype.type == numpy.string_:
                 if len(dat) == 1:
                     dat = dat.value
                 else:
                     dat = list(dat)
         except (ValueError, TypeError):
             pass
         setattr(top, current, dat)
         if len(val.attrs) > 0:
             setattr(top, "%s_attrs" % current, dict(val.attrs))
コード例 #4
0
def colname(txt):
    return fixName(txt.strip().lower()).replace('.', '_')
コード例 #5
0
def colname(txt):
    return fixName(txt.strip().lower()).replace(".", "_")
コード例 #6
0
ファイル: columnfile.py プロジェクト: xraypy/_xraylarch_attic
def _read_ascii(fname, delim='#;*%', labels=None, _larch=None):
    """read a column ascii column file.
    The delim argument (#;* by default) sets the first character
    to mark the header lines.

    Header lines continue until a line with
       '#----' (any delimiter followed by 4 '-'
    The line immediately following that is read as column labels
    (space delimited)

    If the header is of the form
       # KEY : VAL   (ie delimiter, key ':' value)
    these will be parsed into a 'attributes' dictionary
    in the returned group.

    If labels is left the default value of None, column labels will be used
    as the variable names. Variables from extra, unnamed columns will be
    called 'col1', 'col2'.

    If labels=False, the 'data' variable will contain the 2-dimensional data.
    """
    finp = open(fname, 'r')
    kws = {'filename': fname}
    _labels = None
    text = finp.readlines()
    finp.close()
    data = []
    header_txt = []
    header_kws = {}
    islabel = False
    for iline, line in enumerate(text):
        line = line[:-1].strip()
        if line[0] in delim:
            if islabel:
                _labels = line[1:].strip()
                islabel = False
            elif line[2:].strip().startswith('---'):
                islabel = True
            else:
                words = line[1:].split(':', 1)
                key = fixName(words[0].strip())
                if key.startswith('_'):
                    key = key[1:]
                if len(words) == 1:
                    header_txt.append(words[0].strip())
                else:
                    header_kws[key] = words[1].strip()
        else:
            words = line.split()
            data.append([float(w) for w in words])

    kws['header'] = '\n'.join(header_txt)
    kws['attributes'] = header_kws
    kws['column_labels'] = []
    if labels is None:
        labels = _labels
    if labels is None:
        labels = header_txt.pop()
    data = numpy.array(data).transpose()
    ncol, nrow = data.shape
    if not labels:
        kws['data'] = data
    else:
        try:
            labels = labels.replace(',', ' ').split()
        except:
            labels = []
        for icol in range(ncol):
            colname = 'col%i' % (1 + icol)
            if icol < len(labels):
                colname = fixName(labels[icol].strip().lower())
            kws[colname] = data[icol]
            kws['column_labels'].append(colname)

    group = kws
    if _larch is not None:
        group = _larch.symtable.create_group(name='ascii_file %s' % fname)
        for key, val in kws.items():
            setattr(group, key, val)
    return group
コード例 #7
0
ファイル: columnfile.py プロジェクト: newville/xraylarch
def _read_ascii(fname, commentchar="#;%", labels=None, sort=True, sort_column=0, _larch=None):
    """read a column ascii column file.
    The commentchar argument (#;% by default) sets the valid comment characters:
    if the the first character in a line matches one of these, the line is marked
    as a  header lines.

    Header lines continue until a line with
       '#----' (any commentchar followed by 4 '-'
    The line immediately following that is read as column labels
    (space delimited)

    If the header is of the form
       # KEY : VAL   (ie commentchar key ':' value)
    these will be parsed into a 'attributes' dictionary
    in the returned group.

    If labels is left the default value of None, column labels will be used
    as the variable names. Variables from extra, unnamed columns will be
    called 'col1', 'col2'.

    If labels=False, the 'data' variable will contain the 2-dimensional data.
    """
    finp = open(fname, "r")
    kws = {"filename": fname}
    _labels = None
    text = finp.readlines()
    finp.close()
    data = []
    header_txt = []
    header_kws = {}
    islabel = False
    for iline, line in enumerate(text):
        line = line[:-1].strip()
        if line[0] in commentchar:
            if islabel:
                _labels = line[1:].strip()
                islabel = False
            elif line[2:].strip().startswith("---"):
                islabel = True
            else:
                words = line[1:].split(":", 1)
                key = fixName(words[0].strip())
                if key.startswith("_"):
                    key = key[1:]
                if len(words) == 1:
                    header_txt.append(words[0].strip())
                else:
                    header_kws[key] = words[1].strip()
        else:
            words = line.split()
            data.append([float(w) for w in words])

    if len(header_txt) > 0:
        header_kws["header"] = "\n".join(header_txt)
    kws["attributes"] = header_kws
    kws["column_labels"] = []
    if labels is None:
        labels = _labels
    if labels is None:
        labels = header_txt.pop()
    data = np.array(data).transpose()
    ncol, nrow = data.shape
    if sort and sort_column >= 0 and sort_column < nrow:
        data = data[:, np.argsort(data[sort_column])]

    if not labels:
        kws["data"] = data
    else:
        try:
            labels = labels.replace(",", " ").split()
        except:
            labels = []
        for icol in range(ncol):
            colname = "col%i" % (1 + icol)
            if icol < len(labels):
                colname = fixName(labels[icol].strip().lower())
            kws[colname] = data[icol]
            kws["column_labels"].append(colname)

    group = kws

    if _larch is not None:
        group = _larch.symtable.create_group(name="ascii_file %s" % fname)
        atgrp = _larch.symtable.create_group(name="header attributes from %s" % fname)
        for key, val in kws["attributes"].items():
            setattr(atgrp, key, val)

        kws["attributes"] = atgrp
        for key, val in kws.items():
            setattr(group, key, val)
    return group
コード例 #8
0
ファイル: columnfile.py プロジェクト: xraypy/_xraylarch_attic
def _read_ascii(fname, delim='#;*%', labels=None, _larch=None):
    """read a column ascii column file.
    The delim argument (#;* by default) sets the first character
    to mark the header lines.

    Header lines continue until a line with
       '#----' (any delimiter followed by 4 '-'
    The line immediately following that is read as column labels
    (space delimited)

    If the header is of the form
       # KEY : VAL   (ie delimiter, key ':' value)
    these will be parsed into a 'attributes' dictionary
    in the returned group.

    If labels is left the default value of None, column labels will be used
    as the variable names. Variables from extra, unnamed columns will be
    called 'col1', 'col2'.

    If labels=False, the 'data' variable will contain the 2-dimensional data.
    """
    finp = open(fname, 'r')
    kws = {'filename': fname}
    _labels = None
    text = finp.readlines()
    finp.close()
    data = []
    header_txt = []
    header_kws = {}
    islabel = False
    for iline, line in enumerate(text):
        line = line[:-1].strip()
        if line[0] in delim:
            if islabel:
                _labels = line[1:].strip()
                islabel = False
            elif line[2:].strip().startswith('---'):
                islabel = True
            else:
                words = line[1:].split(':', 1)
                key = fixName(words[0].strip())
                if key.startswith('_'):
                    key = key[1:]
                if len(words) == 1:
                    header_txt.append(words[0].strip())
                else:
                    header_kws[key] = words[1].strip()
        else:
            words = line.split()
            data.append([float(w) for w in words])


    kws['header'] = '\n'.join(header_txt)
    kws['attributes'] = header_kws
    kws['column_labels'] = []
    if labels is None:
        labels = _labels
    if labels is None:
        labels = header_txt.pop()
    data = numpy.array(data).transpose()
    ncol, nrow = data.shape
    if not labels:
        kws['data'] = data
    else:
        try:
            labels = labels.replace(',', ' ').split()
        except:
            labels = []
        for icol in range(ncol):
            colname = 'col%i' % (1+icol)
            if icol < len(labels):
                colname = fixName(labels[icol].strip().lower())
            kws[colname] = data[icol]
            kws['column_labels'].append(colname)

    group = kws
    if _larch is not None:
        group = _larch.symtable.create_group(name='ascii_file %s' % fname)
        for key, val in kws.items():
            setattr(group, key, val)
    return group
コード例 #9
0
ファイル: columnfile.py プロジェクト: bruceravel/xraylarch
def colname(txt):
    return fixName(txt.strip().lower()).replace('.', '_')