예제 #1
0
    def add_mean_measurements(self,
                              interpolate=True, substfunc='mean', mean_of_mean=False,
                              reference=None, ref_dtype='mag', norm_dtypes='all', vval=None, norm_method='max',
                              normalize_variable=False, dont_normalize=None,
                              ignore_stypes=False):
        """
        Creates mean measurements for all measurements and measurement types
        :param interpolate:
        :param substfunc:
        :param mean_of_mean:
        :param reference:
        :param ref_dtype:
        :param norm_dtypes:
        :param vval:
        :param norm_method:
        :param normalize_variable:
        :param dont_normalize:
        :param ignore_stypes:
        :return:
        """
        ignore_stypes = RockPy3._to_tuple(ignore_stypes)

        mean_measurements = []
        # separate the different mtypes
        for mtype in self.mtypes:
            # all measurements with that mtype
            measurements = self.get_measurement(mtype=mtype, mean=False)
            # use first measurement as template to check for series
            while measurements:
                m = measurements[0]
                if isinstance(m, RockPy3.Packages.Generic.Measurements.parameters.Parameter):
                    break
                if ignore_stypes == 'all':
                    mlist = measurements
                else:
                    # get measurements with the same series
                    mlist = [measurement for measurement in measurements if m.equal_series(measurement, ignore_stypes=ignore_stypes)]

                # remove the measurements from the measurements list so they don't get averaged twice
                measurements = [m for m in measurements if m not in mlist]

                if not mlist:
                    self.log.debug('NO MORE measurements in mlist')
                    break

                if self.mean_measurement_exists(mlist):
                    self.log.warning('MEAN measurement already exists for these measurements:\n\t\t{}'.format(mlist))
                    mean_measurements.extend(self.mean_measurement_exists(mlist))
                    continue

                mean_measurements.append(self.create_mean_measurement(mlist=mlist,
                                                                      ignore_stypes=ignore_stypes,
                                                                      interpolate=interpolate, substfunc=substfunc,
                                                                      reference=reference, ref_dtype=ref_dtype,
                                                                      norm_dtypes=norm_dtypes,
                                                                      vval=vval,
                                                                      norm_method=norm_method,
                                                                      normalize_variable=normalize_variable,
                                                                      dont_normalize=dont_normalize))
        return mean_measurements
예제 #2
0
 def get_measurement_block(self):
     block = deepcopy(self.storage[0])
     block[2] = [abbreviate_name(mtype).upper() for mtype in RockPy3._to_tuple(block[2]) if mtype]
     block[3] = abbreviate_name(block[3]).upper()
     if not all(block[1:]):
         raise ImportError('sname, mtype, ftype needed for minfo to be generated')
     return '_'.join((self.tuple2str(b) for b in block))
예제 #3
0
 def measurement_infos(self):
     idict = {'fpath': self.fpath, 'ftype': self.ftype, 'idx': self.suffix, 'series': self.series}
     samples = RockPy3._to_tuple(self.samples)
     for i in samples:
         for j in self.mtypes:
             mtype = RockPy3.abbrev_to_classname(j)
             idict.update({'mtype': mtype, 'sample': i})
             yield idict
예제 #4
0
    def sample_infos(self):
        sdict = dict(mass=self.mass, diameter=self.diameter, height=self.height,
                     mass_unit=self.massunit, height_unit=self.heightunit, diameter_unit=self.diameterunit,
                     samplegroup=self.sgroups)

        samples = RockPy3._to_tuple(self.samples)
        for i in samples:
            sdict.update({'name': i})
            yield sdict
예제 #5
0
    def combine_measurements(self,
                             others,
                             remove_others=False,
                             normalize_to_last=False):
        """
        it combines several measurements into the specified one. e.g. a second cooling run at the end of the measurement
        Parameters
        ----------

        others: list
            the measurements to appended to the self.data dictionary. The names are chosen according to the total number of heating and cooling runs

        remove_others: bool
            default: False
            if True the measurements are removed from the sample after it is combined with the measurement
            if False the measurements only combined

        normalize_to_last:
            default: False
            nomalizes the data to be combined to the last point of the last segment. So that different calibrations do not affect the measurement

        Returns
        -------

        """
        others = RockPy3._to_tuple(others)
        self.log.info('COMBINING << {} >> with {}'.format(self, others))
        cool_idx = sum(1 for i in self.data if 'cool' in i)
        warm_idx = sum(1 for i in self.data if 'warm' in i)
        c, w = cool_idx - 1, warm_idx - 1  # old maximum indices

        nfactor = None
        last_segment = list(self.data.keys())[-1]

        if normalize_to_last:
            nfactor = self.data[last_segment]['mag'].v[-1]

        for m in others:
            m = deepcopy(m)
            if normalize_to_last:
                m.normalize('cooling00')
                m.normalize(norm_factor=1 / nfactor)

            for dtype in m.data:
                if 'cool' in dtype:
                    self.data.update({'cooling%02i' % cool_idx: m.data[dtype]})
                    cool_idx += 1
                if 'warming' in dtype:
                    self.data.update({'warming%02i' % warm_idx: m.data[dtype]})
                    warm_idx += 1

            if remove_others:
                self.log.info('REMOVING << {} >> from sample << {} >>'.format(
                    self, self.sobj))
                self.sobj.remove_measurement(mobj=m)

        return self
예제 #6
0
    def measurement_block(self, block):
        sgroups, samples, mtypes, ftype = block.split('_')
        # names with , need to be replaced
        if not '(' in samples and ',' in samples:
            samples = samples.replace(',', '.')
            RockPy3.logger.warning('sample name %s contains \',\' will be replaced with \'.\'' % samples)

        self.sgroups, self.samples, self.mtypes, self.ftype = self.extract_tuple(sgroups), self.extract_tuple(
            samples), self.extract_tuple(mtypes), ftype
        self.mtypes = tuple(RockPy3.abbrev_to_classname(mtype) for mtype in RockPy3._to_tuple(self.mtypes))
        self.ftype = RockPy3.abbrev_to_classname(ftype)
예제 #7
0
 def get_measurement_block(self):
     block = deepcopy(self.storage[0])
     block[2] = [
         abbreviate_name(mtype).upper()
         for mtype in RockPy3._to_tuple(block[2]) if mtype
     ]
     block[3] = abbreviate_name(block[3]).upper()
     if not all(block[1:]):
         raise ImportError(
             'sname, mtype, ftype needed for minfo to be generated')
     return '_'.join((self.tuple2str(b) for b in block))
예제 #8
0
    def combine_measurements(self, others, remove_others = False, normalize_to_last=False):
        """
        it combines several measurements into the specified one. e.g. a second cooling run at the end of the measurement
        Parameters
        ----------

        others: list
            the measurements to appended to the self.data dictionary. The names are chosen according to the total number of heating and cooling runs

        remove_others: bool
            default: False
            if True the measurements are removed from the sample after it is combined with the measurement
            if False the measurements only combined

        normalize_to_last:
            default: False
            nomalizes the data to be combined to the last point of the last segment. So that different calibrations do not affect the measurement

        Returns
        -------

        """
        others = RockPy3._to_tuple(others)
        self.log.info('COMBINING << {} >> with {}'.format(self, others))
        cool_idx = sum(1 for i in self.data if 'cool' in i)
        warm_idx = sum(1 for i in self.data if 'warm' in i)
        c,w = cool_idx-1, warm_idx-1 # old maximum indices

        nfactor = None
        last_segment = list(self.data.keys())[-1]

        if normalize_to_last:
            nfactor = self.data[last_segment]['mag'].v[-1]

        for m in others:
            m = deepcopy(m)
            if normalize_to_last:
                m.normalize('cooling00')
                m.normalize(norm_factor=1/nfactor)

            for dtype in m.data:
                if 'cool' in dtype:
                    self.data.update({'cooling%02i'%cool_idx: m.data[dtype]})
                    cool_idx += 1
                if 'warming' in dtype:
                    self.data.update({'warming%02i'%warm_idx: m.data[dtype]})
                    warm_idx += 1

            if remove_others:
                self.log.info('REMOVING << {} >> from sample << {} >>'.format(self, self.sobj))
                self.sobj.remove_measurement(mobj=m)

        return self
예제 #9
0
    def sample_infos(self):
        sdict = dict(mass=self.mass,
                     diameter=self.diameter,
                     height=self.height,
                     mass_unit=self.massunit,
                     height_unit=self.heightunit,
                     diameter_unit=self.diameterunit,
                     samplegroup=self.sgroups)

        samples = RockPy3._to_tuple(self.samples)
        for i in samples:
            sdict.update({'name': i})
            yield sdict
예제 #10
0
 def measurement_infos(self):
     idict = {
         'fpath': self.fpath,
         'ftype': self.ftype,
         'idx': self.suffix,
         'series': self.series
     }
     samples = RockPy3._to_tuple(self.samples)
     for i in samples:
         for j in self.mtypes:
             mtype = RockPy3.abbrev_to_classname(j)
             idict.update({'mtype': mtype, 'sample': i})
             yield idict
예제 #11
0
    def measurement_block(self, block):
        sgroups, samples, mtypes, ftype = block.split('_')
        # names with , need to be replaced
        if not '(' in samples and ',' in samples:
            samples = samples.replace(',', '.')
            RockPy3.logger.warning(
                'sample name %s contains \',\' will be replaced with \'.\'' %
                samples)

        self.sgroups, self.samples, self.mtypes, self.ftype = self.extract_tuple(
            sgroups), self.extract_tuple(samples), self.extract_tuple(
                mtypes), ftype
        self.mtypes = tuple(
            RockPy3.abbrev_to_classname(mtype)
            for mtype in RockPy3._to_tuple(self.mtypes))
        self.ftype = RockPy3.abbrev_to_classname(ftype)
예제 #12
0
파일: sample.py 프로젝트: MikeVolk/RockPy3
    def remove_measurement(self,
                           mtype=None,
                           series=None,
                           stype=None,
                           sval=None,
                           sval_range=None,
                           mean=False,
                           invert=False,
                           id=None,
                           mobj=None):
        """
        Removes measurements from the sample

        Parameters
        ----------
        mtype
        series
        stype
        sval
        sval_range
        mean
        invert
        id
        mobj

        Returns
        -------

        """
        if mobj:
            mlist = RockPy3._to_tuple(mobj)
        else:
            mlist = self.get_measurement(mtype=mtype,
                                         series=series,
                                         stype=stype,
                                         sval=sval,
                                         sval_range=sval_range,
                                         mean=mean,
                                         invert=invert,
                                         id=id)
        for m in mlist:
            if not mean:
                self.measurements = np.delete(self.measurements, m._idx)
            else:
                self.mean_measurements = np.delete(self.mean_measurements,
                                                   m._idx)
예제 #13
0
    def tuple2str(tup):
        """
        takes a tuple and converts it to text, if more than one element, brackets are put around it
        """
        if tup is None:
            return ''

        tup = RockPy3._to_tuple(tup)

        # if type(tup) == list:
        #     if len(tup) == 1:
        #         tup = tup[0]
        #     else:
        #         tup = tuple(tup)
        if len(tup) == 1:
            return str(tup[0])
        else:
            return str(tup).replace('\'', ' ').replace(' ', '')
예제 #14
0
    def tuple2str(tup):
        """
        takes a tuple and converts it to text, if more than one element, brackets are put around it
        """
        if tup is None:
            return ''

        tup = RockPy3._to_tuple(tup)

        # if type(tup) == list:
        #     if len(tup) == 1:
        #         tup = tup[0]
        #     else:
        #         tup = tuple(tup)
        if len(tup) == 1:
            return str(tup[0])
        else:
            return str(tup).replace('\'', ' ').replace(' ', '')
예제 #15
0
    def add_visual(self, visual, name=None, data=None,
                   plot_groupmean=None, plot_samplemean=None, plot_samplebase=None, plot_groupbase=None,
                   plot_other=None, base_alpha=None, result_from_means=None,
                   xlabel=None, ylabel=None,
                   **visual_opt):
        """
        adds a visual to the plot. This creates a new subplot.

        Parameters
        ----------

           visual: list, str
              name of visual to add.

        """
        # calculation_parameters, kwargs = RockPy3.core.utils.separate_calculation_parameter_from_kwargs(rpobj=self, **visual_opt)
        # convert visual to list
        visuals = RockPy3._to_tuple(visual)
        # for easy checking convert the names to lower case
        visuals = map(str.lower, visuals)
        for visual in visuals:
            # check if visual exists otherwise don't create it
            if visual in RockPy3.implemented_visuals:
                self.log.debug('VISUAL << %s > found' % visual)
                self.__log_implemented()
                if not name:
                    name = visual
                n = self._n_visuals
                # create instance of visual by dynamically calling from implemented_visuals dictionary
                visual_obj = RockPy3.implemented_visuals[visual](
                    data=data, plt_index=n, fig=self, name=name,
                    plot_groupmean=plot_groupmean, plot_groupbase=plot_groupbase,
                    plot_samplemean=plot_samplemean, plot_samplebase=plot_samplebase,
                    plot_other=plot_other, base_alpha=base_alpha, result_from_means=result_from_means,
                    xlabel=xlabel, ylabel=ylabel,
                    **visual_opt)
                self._visuals.append([name, visual, visual_obj])
                self._n_visuals += 1
            else:
                self.log.warning('VISUAL << %s >> not implemented yet' % visual)
                self.__log_implemented(ignore_once=True)
                return
        return visual_obj
예제 #16
0
    def remove_measurement(self,
                           mtype=None,
                           series=None,
                           stype=None, sval=None, sval_range=None,
                           mean=False,
                           invert=False,
                           id=None, mobj=None):
        """
        Removes measurements from the sample

        Parameters
        ----------
        mtype
        series
        stype
        sval
        sval_range
        mean
        invert
        id
        mobj

        Returns
        -------

        """
        if mobj:
            mlist = RockPy3._to_tuple(mobj)
        else:
            mlist = self.get_measurement(mtype=mtype,
                                         series=series, stype=stype, sval=sval, sval_range=sval_range,
                                         mean=mean, invert=invert, id=id)
        for m in mlist:
            if not mean:
                self.measurements = np.delete(self.measurements, m._idx)
            else:
                self.mean_measurements = np.delete(self.mean_measurements, m._idx)
예제 #17
0
def get_significant_digits(values):
    values = RockPy3._to_tuple(values)
    values = map(str, values)
    digits = [len(s.split(".")[-1]) for s in values]
    return digits if len(digits) > 1 else digits[0]
예제 #18
0
파일: figure.py 프로젝트: MikeVolk/RockPy3
    def add_visual(self,
                   visual,
                   name=None,
                   data=None,
                   plot_groupmean=None,
                   plot_samplemean=None,
                   plot_samplebase=None,
                   plot_groupbase=None,
                   plot_other=None,
                   base_alpha=None,
                   result_from_means=None,
                   xlabel=None,
                   ylabel=None,
                   **visual_opt):
        """
        adds a visual to the plot. This creates a new subplot.

        Parameters
        ----------

           visual: list, str
              name of visual to add.

        """
        # calculation_parameters, kwargs = RockPy3.core.utils.separate_calculation_parameter_from_kwargs(rpobj=self, **visual_opt)
        # convert visual to list
        visuals = RockPy3._to_tuple(visual)
        # for easy checking convert the names to lower case
        visuals = map(str.lower, visuals)
        for visual in visuals:
            # check if visual exists otherwise don't create it
            if visual in RockPy3.implemented_visuals:
                self.log.debug('VISUAL << %s > found' % visual)
                self.__log_implemented()
                if not name:
                    name = visual
                n = self._n_visuals
                # create instance of visual by dynamically calling from implemented_visuals dictionary
                visual_obj = RockPy3.implemented_visuals[visual](
                    data=data,
                    plt_index=n,
                    fig=self,
                    name=name,
                    plot_groupmean=plot_groupmean,
                    plot_groupbase=plot_groupbase,
                    plot_samplemean=plot_samplemean,
                    plot_samplebase=plot_samplebase,
                    plot_other=plot_other,
                    base_alpha=base_alpha,
                    result_from_means=result_from_means,
                    xlabel=xlabel,
                    ylabel=ylabel,
                    **visual_opt)
                self._visuals.append([name, visual, visual_obj])
                self._n_visuals += 1
            else:
                self.log.warning('VISUAL << %s >> not implemented yet' %
                                 visual)
                self.__log_implemented(ignore_once=True)
                return
        return visual_obj
예제 #19
0
파일: sample.py 프로젝트: MikeVolk/RockPy3
    def add_mean_measurements(self,
                              interpolate=True,
                              substfunc='mean',
                              mean_of_mean=False,
                              reference=None,
                              ref_dtype='mag',
                              norm_dtypes='all',
                              vval=None,
                              norm_method='max',
                              normalize_variable=False,
                              dont_normalize=None,
                              ignore_stypes=False):
        """
        Creates mean measurements for all measurements and measurement types
        :param interpolate:
        :param substfunc:
        :param mean_of_mean:
        :param reference:
        :param ref_dtype:
        :param norm_dtypes:
        :param vval:
        :param norm_method:
        :param normalize_variable:
        :param dont_normalize:
        :param ignore_stypes:
        :return:
        """
        ignore_stypes = RockPy3._to_tuple(ignore_stypes)

        mean_measurements = []
        # separate the different mtypes
        for mtype in self.mtypes:
            # all measurements with that mtype
            measurements = self.get_measurement(mtype=mtype, mean=False)
            # use first measurement as template to check for series
            while measurements:
                m = measurements[0]
                if isinstance(
                        m, RockPy3.Packages.Generic.Measurements.parameters.
                        Parameter):
                    break
                if ignore_stypes == 'all':
                    mlist = measurements
                else:
                    # get measurements with the same series
                    mlist = [
                        measurement for measurement in measurements
                        if m.equal_series(measurement,
                                          ignore_stypes=ignore_stypes)
                    ]

                # remove the measurements from the measurements list so they don't get averaged twice
                measurements = [m for m in measurements if m not in mlist]

                if not mlist:
                    self.log.debug('NO MORE measurements in mlist')
                    break

                if self.mean_measurement_exists(mlist):
                    self.log.warning(
                        'MEAN measurement already exists for these measurements:\n\t\t{}'
                        .format(mlist))
                    mean_measurements.extend(
                        self.mean_measurement_exists(mlist))
                    continue

                mean_measurements.append(
                    self.create_mean_measurement(
                        mlist=mlist,
                        ignore_stypes=ignore_stypes,
                        interpolate=interpolate,
                        substfunc=substfunc,
                        reference=reference,
                        ref_dtype=ref_dtype,
                        norm_dtypes=norm_dtypes,
                        vval=vval,
                        norm_method=norm_method,
                        normalize_variable=normalize_variable,
                        dont_normalize=dont_normalize))
        return mean_measurements
예제 #20
0
def get_significant_digits(values):
    values = RockPy3._to_tuple(values)
    values = map(str, values)
    digits = [len(s.split('.')[-1]) for s in values]
    return digits if len(digits)>1 else digits[0]
예제 #21
0
def QuickFig(data, visuals, **kwargs):
    f = RockPy3.Figure(data=data)
    visuals = RockPy3._to_tuple(visuals)
    for v in visuals:
        f.add_visual(visual=v)
    f.show(**kwargs)
예제 #22
0
def QuickFig(data, visuals, **kwargs):
    f = RockPy3.Figure(data=data)
    visuals = RockPy3._to_tuple(visuals)
    for v in visuals:
        f.add_visual(visual=v)
    f.show(**kwargs)
예제 #23
0
    def __init__(self, fpath,
                 sgroups=None, samples=None,
                 mtypes=None, ftype=None,
                 mass=None, height=None, diameter=None,
                 massunit=None, lengthunit=None, heightunit=None, diameterunit=None,
                 series=None, comment=None, folder=None, suffix=None,
                 read_fpath=True, **kwargs):

        """

        Parameters
        ----------
        fpath
        sgroups
        samples
        mtypes
        ftype
        mass
        height
        diameter
        massunit
        lengthunit
        heightunit
        diameterunit
        series
        comment
        folder
        suffix
        read_fpath: bool
            if true the path will be read for info
        kwargs
        """
        if 'mtype' in kwargs and not mtypes:
            mtypes = kwargs.pop('mtype')
        if 'sgroup' in kwargs and not sgroups:
            mtypes = kwargs.pop('sgroup')
        if 'sample' in kwargs and not samples:
            mtypes = kwargs.pop('sample')

        blocks = (self.measurement_block, self.sample_block, self.series_block, self.add_block, self.comment_block)
        additional = tuple()

        sgroups = RockPy3._to_tuple(sgroups)
        sgroups = tuple([sg if sg != 'None' else None for sg in sgroups])

        if mtypes:
            mtypes = tuple(RockPy3.abbrev_to_classname(mtype) for mtype in RockPy3._to_tuple(mtypes))
        if ftype:
            ftype = RockPy3.abbrev_to_classname(ftype)

        self.__dict__.update({i: None for i in ('sgroups', 'samples', 'mtypes', 'ftype',
                                                'mass', 'height', 'diameter',
                                                'massunit', 'lengthunit', 'heightunit', 'diameterunit',
                                                'series', 'additional', 'comment', 'folder', 'suffix')
                              })
        self.fpath = fpath

        if read_fpath and fpath:  # todo add check for if path is readable
            self.folder = os.path.dirname(fpath)
            f, self.suffix = os.path.splitext(os.path.basename(fpath))
            self.suffix = self.suffix.strip('.')
            splits = f.split('#')

            # check if RockPy compatible e.g. first part must be len(4)
            if not len(splits[0]) == 4:
                pass
            for i, block in enumerate(blocks[:len(splits)]):
                if splits[i]:
                    try:
                        block(splits[i])
                    except (ValueError,):
                        pass
        for i in ('sgroups', 'samples', 'mtypes', 'ftype',
                  'mass', 'height', 'diameter',
                  'massunit', 'lengthunit', 'heightunit', 'diameterunit',
                  'series', 'additional', 'comment', 'folder'):

            if locals()[i]:
                if isinstance(locals()[i], (tuple, list, set)):
                    if not all(locals()[i]):
                        continue
                setattr(self, i, locals()[i])

        if self.additional is None:
            self.additional = ''
        if kwargs:
            for k, v in kwargs.items():
                if v:
                    print(k, v, self.additional)
                    self.additional += '{}:{}'.format(k, v)

        if suffix:
            self.suffix = suffix

        if type(self.suffix) == int:
            self.suffix = '%03i' % self.suffix

        if not self.suffix:
            self.suffix = '000'

        if not self.sgroups: self.sgroups = None

        self.storage = [[self.sgroups, self.samples, self.mtypes, self.ftype],
                        [[self.mass, self.massunit], [self.height, self.heightunit],
                         [self.diameter, self.diameterunit], ],
                        self.series,
                        (self.additional,),
                        self.comment]
예제 #24
0
    def import_folder(self,
                      folder, sname=None, sgroup=None,
                      mtype=None,
                      automatic_results = True,
                      ):  # todo specify samples, mtypes and series for selective import of folder
        """
        imports all files in the specified folder
        Parameters
        ----------
        folder
        gname

        Returns
        -------

        """
        files = [os.path.join(folder, i) for i in os.listdir(folder)
                 if not i.startswith('#')
                 if not i.startswith(".")
                 if not i.endswith("pynb")
                 if not i.endswith("log")
                 if not os.path.isdir(os.path.join(folder, i))
                 ]

        measurements = []
        start = time.clock()
        minfos = (RockPy3.core.file_operations.minfo(f) for f in files)
        minfos = (minfo for minfo in minfos if minfo.is_readable())

        if mtype:
            mtype = RockPy3._to_tuple(mtype)
            mtype = [RockPy3.abbrev_to_classname(mt) for mt in mtype]

        for mi in minfos:
            RockPy3.logger.info('importing {}'.format(mi.fpath))
            # dont import samples that are not in the measurement info
            if sname:
                sname = RockPy3._to_tuple(sname)
                if not any(s in mi.samples for s in sname):
                    continue

            # dont import samples that are not included in a certain samplegroup in the measurement info
            if sgroup:
                sgroup = RockPy3._to_tuple(sgroup)
                if not any(sg in mi.sgroups for sg in sgroup):
                    continue

            # check if the filename has not been imported, yet
            if mi.fpath in self.imported_files:
                continue
            else:
                self.imported_files.append(mi.fpath)

            for sinfo in mi.sample_infos:
                s = self.add_sample(warnings=False, **sinfo)

                for minfo in mi.measurement_infos:
                    if mtype and minfo['mtype'].lower() not in mtype:
                        continue

                    if minfo['sample'] == sinfo['name']:
                        measurements.append(s.add_measurement(**minfo))

        end = time.clock()
        print('IMPORT generated {} measurements: finished in {:.02e}s'.format(len(measurements), end - start))
        return measurements
예제 #25
0
    def __init__(self,
                 fpath,
                 sgroups=None,
                 samples=None,
                 mtypes=None,
                 ftype=None,
                 mass=None,
                 height=None,
                 diameter=None,
                 massunit=None,
                 lengthunit=None,
                 heightunit=None,
                 diameterunit=None,
                 series=None,
                 comment=None,
                 folder=None,
                 suffix=None,
                 read_fpath=True,
                 **kwargs):
        """

        Parameters
        ----------
        fpath
        sgroups
        samples
        mtypes
        ftype
        mass
        height
        diameter
        massunit
        lengthunit
        heightunit
        diameterunit
        series
        comment
        folder
        suffix
        read_fpath: bool
            if true the path will be read for info
        kwargs
        """
        if 'mtype' in kwargs and not mtypes:
            mtypes = kwargs.pop('mtype')
        if 'sgroup' in kwargs and not sgroups:
            mtypes = kwargs.pop('sgroup')
        if 'sample' in kwargs and not samples:
            mtypes = kwargs.pop('sample')

        blocks = (self.measurement_block, self.sample_block, self.series_block,
                  self.add_block, self.comment_block)
        additional = tuple()

        sgroups = RockPy3._to_tuple(sgroups)
        sgroups = tuple([sg if sg != 'None' else None for sg in sgroups])

        if mtypes:
            mtypes = tuple(
                RockPy3.abbrev_to_classname(mtype)
                for mtype in RockPy3._to_tuple(mtypes))
        if ftype:
            ftype = RockPy3.abbrev_to_classname(ftype)

        self.__dict__.update({
            i: None
            for i in ('sgroups', 'samples', 'mtypes', 'ftype', 'mass',
                      'height', 'diameter', 'massunit', 'lengthunit',
                      'heightunit', 'diameterunit', 'series', 'additional',
                      'comment', 'folder', 'suffix')
        })
        self.fpath = fpath

        if read_fpath and fpath:  # todo add check for if path is readable
            self.folder = os.path.dirname(fpath)
            f, self.suffix = os.path.splitext(os.path.basename(fpath))
            self.suffix = self.suffix.strip('.')
            splits = f.split('#')

            # check if RockPy compatible e.g. first part must be len(4)
            if not len(splits[0]) == 4:
                pass
            for i, block in enumerate(blocks[:len(splits)]):
                if splits[i]:
                    try:
                        block(splits[i])
                    except (ValueError, ):
                        pass
        for i in ('sgroups', 'samples', 'mtypes', 'ftype', 'mass', 'height',
                  'diameter', 'massunit', 'lengthunit', 'heightunit',
                  'diameterunit', 'series', 'additional', 'comment', 'folder'):

            if locals()[i]:
                if isinstance(locals()[i], (tuple, list, set)):
                    if not all(locals()[i]):
                        continue
                setattr(self, i, locals()[i])

        if self.additional is None:
            self.additional = ''
        if kwargs:
            for k, v in kwargs.items():
                if v:
                    print(k, v, self.additional)
                    self.additional += '{}:{}'.format(k, v)

        if suffix:
            self.suffix = suffix

        if type(self.suffix) == int:
            self.suffix = '%03i' % self.suffix

        if not self.suffix:
            self.suffix = '000'

        if not self.sgroups: self.sgroups = None

        self.storage = [[self.sgroups, self.samples, self.mtypes, self.ftype],
                        [
                            [self.mass, self.massunit],
                            [self.height, self.heightunit],
                            [self.diameter, self.diameterunit],
                        ], self.series, (self.additional, ), self.comment]
예제 #26
0
    def get_measurement(self,
                        mtype=None,
                        series=None,
                        stype=None, sval=None, sval_range=None,
                        mean=False,
                        invert=False,
                        id=None,
                        result=None
                        ):
        """
        Returns a list of measurements of type = mtypes

        Parameters
        ----------
           mtypes: list, str
              mtypes to be returned
           series: list(tuple)
              list of tuples, to search for several sepcific series. e.g. [('mtime',4),('gc',2)] will only return
              mesurements that fulfill both criteria.
           stypes: list, str
              series type
           sval_range: list, str
              series range e.g. sval_range = [0,2] will give all from 0 to 2 including 0,2
              also '<2', '<=2', '>2', and '>=2' are allowed.
           svals: float
              series value to be searched for.
              caution:
                 will be overwritten when sval_range is given
           invert:
              if invert true it returns only measurements that do not meet criteria
           sval_range:
              can be used to look up measurements within a certain range. if only one value is given,
                     it is assumed to be an upper limit and the range is set to [0, sval_range]
           mean: bool
           id: list(int)
            search for given measurement id

        Returns
        -------
            if no arguments are passed all sample.measurements
            list of RockPy.Measurements that meet search criteria or if invert is True, do not meet criteria.
            [] if none are found

        Note
        ----
            there is no connection between stype and sval. This may cause problems. I you have measurements with
               M1: [pressure, 0.0, GPa], [temperature, 100.0, C]
               M2: [pressure, 1.0, GPa], [temperature, 100.0, C]
            and you search for stypes=['pressure','temperature'], svals=[0,100]. It will return both M1 and M2 because
            both M1 and M2 have [temperature, 100.0, C].

        """

        if mean:
            mlist = self.mean_measurements
        else:
            mlist = self.measurements

        if id is not None:
            id = RockPy3._to_tuple(id)
            mlist = filter(lambda x: x.id in id, mlist)
            return list(mlist)

        if mtype:
            mtype = RockPy3._to_tuple(mtype)
            mtype = tuple(RockPy3.abbrev_to_classname(mt) for mt in mtype)
            mlist = filter(lambda x: x.mtype in mtype, mlist)

        if stype:
            mlist = filter(lambda x: x.has_stype(stype=stype, method='any'), mlist)

        if sval_range is not None:
            sval_range = self._convert_sval_range(sval_range=sval_range, mean=mean)

            if not sval:
                sval = sval_range
            else:
                sval = RockPy3._to_tuple()
                sval += RockPy3._to_tuple(sval_range)

        if sval is not None:
            mlist = filter(lambda x: x.has_sval(sval=sval, method='any'), mlist)

        if series:
            series = RockPy3.core.utils.tuple2list_of_tuples(series)
            mlist = (x for x in mlist if x.has_series(series=series, method='all'))

        if result:
            mlist = filter(lambda x: x.has_result(result=result), mlist)

        if invert:
            if mean:
                mlist = filter(lambda x: x not in mlist, self.mean_measurements)
            else:
                mlist = filter(lambda x: x not in mlist, self.measurements)

        return list(mlist)
예제 #27
0
    def import_folder(
        self,
        folder,
        sname=None,
        sgroup=None,
        mtype=None,
        automatic_results=True,
    ):  # todo specify samples, mtypes and series for selective import of folder
        """
        imports all files in the specified folder
        Parameters
        ----------
        folder
        gname

        Returns
        -------

        """
        files = [
            os.path.join(folder, i) for i in os.listdir(folder)
            if not i.startswith('#') if not i.startswith(".")
            if not i.endswith("pynb") if not i.endswith("log")
            if not os.path.isdir(os.path.join(folder, i))
        ]

        measurements = []
        start = time.clock()
        minfos = (RockPy3.core.file_operations.minfo(f) for f in files)
        minfos = (minfo for minfo in minfos if minfo.is_readable())

        if mtype:
            mtype = RockPy3._to_tuple(mtype)
            mtype = [RockPy3.abbrev_to_classname(mt) for mt in mtype]

        for mi in minfos:
            RockPy3.logger.info('importing {}'.format(mi.fpath))
            # dont import samples that are not in the measurement info
            if sname:
                sname = RockPy3._to_tuple(sname)
                if not any(s in mi.samples for s in sname):
                    continue

            # dont import samples that are not included in a certain samplegroup in the measurement info
            if sgroup:
                sgroup = RockPy3._to_tuple(sgroup)
                if not any(sg in mi.sgroups for sg in sgroup):
                    continue

            # check if the filename has not been imported, yet
            if mi.fpath in self.imported_files:
                continue
            else:
                self.imported_files.append(mi.fpath)

            for sinfo in mi.sample_infos:
                s = self.add_sample(warnings=False, **sinfo)

                for minfo in mi.measurement_infos:
                    if mtype and minfo['mtype'].lower() not in mtype:
                        continue

                    if minfo['sample'] == sinfo['name']:
                        measurements.append(s.add_measurement(**minfo))

        end = time.clock()
        print('IMPORT generated {} measurements: finished in {:.02e}s'.format(
            len(measurements), end - start))
        return measurements
예제 #28
0
파일: sample.py 프로젝트: MikeVolk/RockPy3
    def get_measurement(self,
                        mtype=None,
                        series=None,
                        stype=None,
                        sval=None,
                        sval_range=None,
                        mean=False,
                        invert=False,
                        id=None,
                        result=None):
        """
        Returns a list of measurements of type = mtypes

        Parameters
        ----------
           mtypes: list, str
              mtypes to be returned
           series: list(tuple)
              list of tuples, to search for several sepcific series. e.g. [('mtime',4),('gc',2)] will only return
              mesurements that fulfill both criteria.
           stypes: list, str
              series type
           sval_range: list, str
              series range e.g. sval_range = [0,2] will give all from 0 to 2 including 0,2
              also '<2', '<=2', '>2', and '>=2' are allowed.
           svals: float
              series value to be searched for.
              caution:
                 will be overwritten when sval_range is given
           invert:
              if invert true it returns only measurements that do not meet criteria
           sval_range:
              can be used to look up measurements within a certain range. if only one value is given,
                     it is assumed to be an upper limit and the range is set to [0, sval_range]
           mean: bool
           id: list(int)
            search for given measurement id

        Returns
        -------
            if no arguments are passed all sample.measurements
            list of RockPy.Measurements that meet search criteria or if invert is True, do not meet criteria.
            [] if none are found

        Note
        ----
            there is no connection between stype and sval. This may cause problems. I you have measurements with
               M1: [pressure, 0.0, GPa], [temperature, 100.0, C]
               M2: [pressure, 1.0, GPa], [temperature, 100.0, C]
            and you search for stypes=['pressure','temperature'], svals=[0,100]. It will return both M1 and M2 because
            both M1 and M2 have [temperature, 100.0, C].

        """

        if mean:
            mlist = self.mean_measurements
        else:
            mlist = self.measurements

        if id is not None:
            id = RockPy3._to_tuple(id)
            mlist = filter(lambda x: x.id in id, mlist)
            return list(mlist)

        if mtype:
            mtype = RockPy3._to_tuple(mtype)
            mtype = tuple(RockPy3.abbrev_to_classname(mt) for mt in mtype)
            mlist = filter(lambda x: x.mtype in mtype, mlist)

        if stype:
            mlist = filter(lambda x: x.has_stype(stype=stype, method='any'),
                           mlist)

        if sval_range is not None:
            sval_range = self._convert_sval_range(sval_range=sval_range,
                                                  mean=mean)

            if not sval:
                sval = sval_range
            else:
                sval = RockPy3._to_tuple()
                sval += RockPy3._to_tuple(sval_range)

        if sval is not None:
            mlist = filter(lambda x: x.has_sval(sval=sval, method='any'),
                           mlist)

        if series:
            series = RockPy3.core.utils.tuple2list_of_tuples(series)
            mlist = (x for x in mlist
                     if x.has_series(series=series, method='all'))

        if result:
            mlist = filter(lambda x: x.has_result(result=result), mlist)

        if invert:
            if mean:
                mlist = filter(lambda x: x not in mlist,
                               self.mean_measurements)
            else:
                mlist = filter(lambda x: x not in mlist, self.measurements)

        return list(mlist)