示例#1
0
    def average_results(self, **parameter):
        """
        makes averages of all calculations for all samples in group. Only samples with same series are averaged

        prams: parameter are calculation parameters, has to be a dictionary
        """
        substfunc = parameter.pop('substfunc', 'mean')
        out = None
        stype_results = self.stype_results(**parameter)
        for stype in stype_results:
            for sval in sorted(stype_results[stype].keys()):
                aux = stype_results[stype][sval]
                aux.define_alias('variable', 'stype ' + stype)
                aux = condense(aux, substfunc=substfunc)
                if out == None:
                    out = {stype: aux}
                else:
                    out[stype] = out[stype].append_rows(aux)
        return out
示例#2
0
    def average_results(self, **parameter):
        """
        makes averages of all calculations for all samples in group. Only samples with same series are averaged

        prams: parameter are calculation parameters, has to be a dictionary
        """
        substfunc = parameter.pop('substfunc', 'mean')
        out = None
        stype_results = self.stype_results(**parameter)
        for stype in stype_results:
            for sval in sorted(stype_results[stype].keys()):
                aux = stype_results[stype][sval]
                aux.define_alias('variable', 'stype ' + stype)
                aux = condense(aux, substfunc=substfunc)
                if out == None:
                    out = {stype: aux}
                else:
                    out[stype] = out[stype].append_rows(aux)
        return out
示例#3
0
    def mean_measurement(self,
                         mtype=None, stype=None, sval=None, sval_range=None, mlist=None,
                         interpolate=True, recalc_mag=False,
                         substfunc='mean',
                         reference=None, ref_dtype='mag', norm_dtypes='all', vval=None, norm_method='max',
                         normalize_variable=False, dont_normalize=None,
                         add2list=True):

        """
        takes a list of measurements and creates a mean measurement out of all measurements data

        Parameters
        ----------
           mlist:
           interpolate:
           recalc_mag:
           add2list: bool
              will add measurement to the mean_measurements list

        Returns
        -------
           RockPy.Measurement
              The mean measurement that fits to the specified lookup


        """
        if not mtype:
            # raise ValueError('No mtype specified')

            self.logger.error('NO mtype specified. Please specify mtype')
            return

        mlist = self.get_measurements(mtypes=mtype, stypes=stype, svals=sval, sval_range=sval_range, mean=False)

        if reference:
            mlist = [m.normalize(reference=reference, ref_dtype=ref_dtype, norm_dtypes=norm_dtypes, vval=vval,
                                 norm_method=norm_method, normalize_variable=normalize_variable,
                                 dont_normalize=dont_normalize) for m in mlist]
        # get common series
        series = list(set(['%s_%.3f_%s' %(s.stype, s.value, s.unit) for m in mlist for s in m.series]))
        series = [i.split('_') for i in series]

        if not mlist:
            self.logger.warning('NO measurement found >> %s, %s, %f >>' %(mtype, stype, sval))
            return None

        if len(mlist) == 1:
            self.logger.warning('Only one measurement found returning measurement')
            return mlist[0]

        # use first measurement as base
        dtypes = get_common_dtypes_from_list(mlist=mlist)

        # create a base measurement
        base_measurement = RockPy.Functions.general.create_dummy_measurement(mtype=mlist[0].mtype,
                                                                             machine=mlist[0].machine,
                                                                             mdata=deepcopy(mlist[0].data), sample=self)
        # delete uncommon dtype from base_measurement
        for key in base_measurement.data:
            if key not in dtypes:
                base_measurement.data.pop(key)

        for dtype in dtypes:  # cycle through all dtypes e.g. 'down_field', 'up_field' for hysteresis
            dtype_list = [m.data[dtype] for m in mlist] # get all data for dtype in one list

            if interpolate:
                varlist = self.__get_variable_list(dtype_list, var='temp') #todo check why var=temp
                if len(varlist) > 1:
                    dtype_list = [m.interpolate(varlist) for m in dtype_list]

            if len(dtype_list) > 1:  # for single measurements
                base_measurement.data[dtype] = condense(dtype_list, substfunc=substfunc)
                base_measurement.data[dtype] = base_measurement.data[dtype].sort('variable')

            if recalc_mag:
                base_measurement.data[dtype].define_alias('m', ('x', 'y', 'z'))
                base_measurement.data[dtype]['mag'].v = base_measurement.data[dtype].magnitude('m')

        # check if there is an initial state measurement and mean them, too.
        if base_measurement.initial_state:
            for dtype in base_measurement.initial_state.data:
                dtype_list = [m.initial_state.data[dtype] for m in mlist if m.initial_state]
                base_measurement.initial_state.data[dtype] = condense(dtype_list, substfunc=substfunc)
                base_measurement.initial_state.data[dtype] = base_measurement.initial_state.data[dtype].sort('variable')
                if recalc_mag:
                    base_measurement.initial_state.data[dtype].define_alias('m', ('x', 'y', 'z'))
                    base_measurement.initial_state.data[dtype]['mag'].v = base_measurement.initial_state.data[
                        dtype].magnitude(
                        'm')

        base_measurement.sample_obj = self

        # add all comon series to mean_m_object
        for s in series:
            base_measurement.add_sval(stype=s[0], sval=float(s[1]), unit=s[2])

        # add to self.mean_measurements if specified
        if add2list:
            self.mean_measurements.append(base_measurement)
            self.add_m2_mdict(mobj=base_measurement, mdict_type='mean_mdict')
        return base_measurement
示例#4
0
文件: data.py 项目: yinyongqi/RockPy
def test():
    # define some data for tutorials.rst
    testdata = ((1, 2, 3, 4),
                (2, 6, 7, 8),
                (9, 10, 11, 12))

    testdata2 = ((1, 1),
                 (2, 2),
                 (19, 3))

    # create a rockpydata object with named columns and filled with testdata
    d = RockPyData(column_names=('F', 'Mx', 'My', 'Mz'), row_names=('1.Zeile', '2.Zeile', '3.Zeile'),
                   units=('T', 'mT', 'fT', 'pT'), data=testdata)

    d_json = numpyson.dumps(d)
    print d_json
    dd = numpyson.loads(d_json)
    print repr(dd)

    print "dd:", dd

    #d = d.eliminate_duplicate_variable_rows(substfunc='last')
    #print d._find_unique_variable_rows()
    print('d:\n%s' % d)

    e = RockPyData(column_names=('F', 'Mx'), row_names=('1.Zeile', '2.Zeile', '3.Zeile'),
                   units=('T', 'mT', 'fT', 'pT'), data=testdata2)

    print('e:\n%s' % e)

    print('e+d:\n%s' % (e+d))
    print('e-d:\n%s' % (e-d))
    print('e/d:\n%s' % (e/d))
    print('e*d:\n%s' % (e*d))

    print('d/e:\n%s' % (d/e))
    print('d*e:\n%s' % (d*e))
    print('d+e:\n%s' % (d+e))
    print('d-e:\n%s' % (d-e))

    print d.units
    # define as many aliases as you want
    d.define_alias('M', ('Mx', 'My', 'Mz'))
    d.define_alias('Mzx', ('Mz', 'Mx'))

    # show some data
    # aliases 'all', 'variable' and 'dep_var' are predefined
    print('all:\n%s' % d['all'])
    print('Mzx:\n%s' % d['Mzx'])

    # lets alter some data
    d['Mx'] = np.array((13, 24, 35))

    # show M with modified Mx component
    print('M:\n%s' % d['M'])
    # show Mx
    print('Mx:\n%s' % d['Mx'])
    # we can also alter several columns at once
    d['M'] = ((2, 3, 4),
              (18, 88, 98),
              (39, 89, 99))
    print('M:\n%s' % d['M'])

    # some math fun
    # calculate magnitude of vector 'M' and save it as new column 'magM'
    d = d.append_columns('magM', d.magnitude('M'))

    # calculate values of 'magM' normalized to 100
    #d.append_columns('normM', d.normalize('magM', 100))

    # we can also add arbitrary data in a new column
    d = d.append_columns(("T",), np.array((1, 2, 3)))

    # we can also add an empty column
    d = d.append_columns(("empty",))

    # renaming a column
    d.rename_column('T', 'temp')

    # show all data again, now including magM and T as the last two columns
    print d

    # do a plot of F vs magM
    # plt.plot(d['F'], d['magM'])
    # plt.show()

    # fancy filtering of data
    tf_array = (d['Mx'].v > 10) & (d['Mx'].v < 20)
    print 'filtering:'
    filtered_d = d.filter(tf_array)
    print filtered_d['Mx']

    # arithmetic operations
    e = deepcopy(d)
    # mutlipy one column with value
    e['Mx'].v *= 2
    # calculate difference of two rockpydata objects
    #c = e - d
    #print c

    #c = e + d
    #print c

    #c = e / d
    #print c

    #c = e * d
    #print c

    #print repr(c)

    # test single line object
    l = RockPyData(column_names=('A', 'B', 'C', 'D'), row_names=('1.Zeile',),
                   units=('T', 'mT', 'fT', 'pT'), data=((1, 2, 3, 4),))
    l = l.append_columns('X', (5,))
    print l

    print l['X']

    #print d.mean()
    print d
    print d + (1, 2, 3, 4, 5, 6)

    print d.interpolate(np.arange(2, 10, .5), includesourcedata=True)
    #d.define_alias('variable', 'Mx')
    #print d.interpolate(np.arange(0, 10, .5))

    print "condense:"
    print condense([d, d*2, d*3], substfunc='median')