예제 #1
0
파일: junction.py 프로젝트: xxxxHolic/pyrod
    def error(self, limit):

        mask = control.bragg_mask(ctr['q'], 6, 1, 'yin', limit)
        number = {i: mask.tolist().count(i) for i in mask}[1]

        yhat = np.log(abs(self.data)) * mask
        ydat = np.log(abs(ctr['shkl'])) * mask
        ybar = np.sum(ydat) / number

        ssreg = np.sum((yhat - ybar)**2)
        sstot = np.sum((ydat - ybar)**2)

        if ssreg >= sstot:
            r2 = sstot / ssreg
        else:
            r2 = ssreg / sstot

        self.r_square = r2

        return r2
예제 #2
0
    def pos_refine(self):

        # the used keys
        sub_key_list = ['absorption', 'roughness', 'scale']
        # input substrate ubr and substrate index
        sub_ubr, sub_index = tc.initialize_contral_vars(
            self.ubr_index, ['substrate'], sub_key_list)

        substrate_ctr = structure.substrate_ctr(self.var_list, self.iq,
                                                sub_ubr, sub_index,
                                                self.absorption, self.h,
                                                self.h, self.scale,
                                                sub_key_list)

        # roughness
        cr = roughness(self.iq)
        r = cr.robinson_roughness(self.roughness)

        s = np.multiply(self.intensity * substrate_ctr, np.mat(r).T)

        subr0 = self.var_table['posz_list']
        slab_index = 'posz'
        key_list = ['pos']

        mask = tc.bragg_mask(self.iq, 3, 1, mode='yin', limit=24)

        #        fig, ax = plt.subplots()

        def varience(subr):

            fctr = structure.film_ctr(self.var_list, self.var_table, subr,
                                      slab_index, self.iq, self.h, self.k,
                                      key_list)

            # roughness
            cr = roughness(self.iq)
            # interface roughness
            ir = cr.interface_roughness(self.iroughness)

            f = np.multiply(np.multiply(self.intensity * fctr,
                                        np.mat(r).T),
                            np.mat(ir).T)

            a = s + f

            e = np.sqrt(self.i)

            #            plt.cla()
            #            plt.plot(np.log10(e))
            #            plt.plot(np.log10(abs(a)))

            v = np.sum(
                abs(mask * (np.log10(abs(a) + 1e-6) - np.log10(e + 1e-6))))

            print(v)

            return v

        re = so.minimize(varience,
                         subr0,
                         method='Nelder-Mead',
                         options={'maxiter': 50})
        subr1 = re.x

        fctr1 = structure.film_ctr(self.var_list, self.var_table, subr1,
                                   slab_index, self.iq, self.h, self.k,
                                   key_list)

        # roughness
        cr = roughness(self.iq)
        # interface roughness
        ir = cr.interface_roughness(self.iroughness)

        return np.multiply(np.multiply(self.intensity * fctr1,
                                       np.mat(r).T),
                           np.mat(ir).T)
예제 #3
0
    def lattice_refine(self, p_mask=13, n_mask=3, weight=1):

        # Prepare the data mask
        # yin mask to mask the bragg peaks
        bn_mask = np.mat(tc.bragg_mask(self.iq, n_mask, 1, mode='yin'))
        # yang mask to refine the signal around bragg peaks only
        bp_mask = np.mat(tc.bragg_mask(self.iq, p_mask, 1, mode='yang'))
        bgm = np.asarray(np.multiply(bn_mask, bp_mask).T).reshape(-1) * weight

        # calculate ss tot.For only seveal signal points are calculate
        # mean value is np.sum(abs(i)*bgm)/np.sum(bgm)
        ss_tot = np.sum((abs(self.i)*bgm - \
                         np.sum(abs(self.i)*bgm)/np.sum(bgm))**2)

        # initialize the variable about the lattice
        slab_list = np.unique(self.var_table['slab_list']).tolist()
        subr, sindex = tc.initialize_contral_vars(self.ubr_index, slab_list,
                                                  ['lattice_abc'])
        # substrate ctr
        # the used keys
        key_list = ['absorption', 'roughness', 'scale']
        # input substrate ubr and substrate index
        sub_ubr, sub_index = tc.initialize_contral_vars(
            self.ubr_index, ['substrate'], key_list)

        substrate_ctr = structure.substrate_ctr(self.var_list, self.iq,
                                                sub_ubr, sub_index,
                                                self.absorption, self.h,
                                                self.h, self.scale, key_list)

        # roughness
        cr = roughness(self.iq)
        r = cr.robinson_roughness(self.roughness)

        def c_refine(c_var):

            # input lattice c variable into subr
            if len(slab_list) == 1:
                subr[2] = c_var
            elif len(slab_list) >= 2:
                for slabi in range(len(slab_list)):
                    subr[slabi * 3 - 1] = c_var[slabi]

            fctr = structure.film_ctr(self.var_list, self.var_table, subr,
                                      sindex, self.iq, self.h, self.k,
                                      ['lattice_abc'])

            ss = np.multiply(np.mat(fctr) + substrate_ctr,
                             np.mat(r).T) * self.intensity
            # calculate r square
            sa = np.asarray(ss).reshape(-1)
            ss_res = np.sum((abs(self.i) * bgm - abs(sa) * bgm)**2)
            varience = ss_res / ss_tot

            print(int(varience * 1e4) / 1e4)

            return varience

        # select properity optimized method for lattice_c optimize

        # if there are only one or two variables, step_brute method is a direct method
        if len(slab_list) == 1:
            print('lattice c optimising....\nOPT_STEP_BRUTE method is used')
            rec = tt.opt_step_brute(c_refine, [[0.8, 1.2]], grid_size=20)
        elif len(slab_list) == 2:
            print('lattice c optimising....\nOPT_STEP_BRUTE method is used')
            rec = tt.opt_step_brute(c_refine, [[0.8, 1.2], [0.8, 1.2]],
                                    grid_size=10)

        # for larger variable number, Nelder-Mead method is more comparable
        elif len(slab_list) >= 3:
            print('lattice c optimising....\nNELDER_MEAD method is used')
            c0 = np.ones(len(slab_list)).tolist()
            resc = so.minimize(c_refine, c0, method='Nelder-Mead', tol=10)
            rec = resc.x

        # update subr
        if len(slab_list) == 1:
            subr[2] = rec
        elif len(slab_list) >= 2:
            for slabi in range(len(slab_list)):
                subr[slabi * 3 - 1] = rec[0][slabi]

        # update ubr_index
        self.ubr_index = tc.refresh_index(self.ubr_index, subr, sindex)

        # plot the refined result

        fctr = structure.film_ctr(self.var_list, self.var_table, subr, sindex,
                                  self.iq, self.h, self.k, ['lattice_abc'])

        ss = np.multiply(np.mat(fctr) + substrate_ctr, np.mat(r).T)
        plt.plot(self.iq, np.log(abs(ss * self.intensity)))
        plt.plot(self.iq, np.log(np.sqrt(self.i)))
예제 #4
0
    def dw_refine(self, p_mask=13, n_mask=3, weight=1, key_list=['dw']):

        # Prepare the data mask
        # yin mask to mask the bragg peaks
        bn_mask = np.mat(tc.bragg_mask(self.iq, n_mask, 1, mode='yin'))
        # yang mask to refine the signal around bragg peaks only
        bp_mask = np.mat(tc.bragg_mask(self.iq, p_mask, 1, mode='yang'))
        bgm = np.asarray(np.multiply(bn_mask, bp_mask).T).reshape(-1) * weight

        # calculate ss tot.For only seveal signal points are calculate
        # mean value is np.sum(abs(i)*bgm)/np.sum(bgm)
        ss_tot = np.sum(
            (abs(self.i) * bgm - np.sum(abs(self.i) * bgm) / np.sum(bgm))**2)

        # initialize the variable about the lattice
        slab_list = np.unique(self.var_table['slab_list']).tolist()
        ubr_index = tc.contral_vars(self.var_list)
        subr, sindex = tc.initialize_contral_vars(ubr_index, slab_list, ['dw'])

        # roughness
        cr = roughness(self.iq)
        r = cr.robinson_roughness(self.roughness)

        # substrate ctr
        # the used keys
        key_list = ['absorption', 'roughness', 'scale']
        # input substrate ubr and substrate index
        sub_ubr, sub_index = tc.initialize_contral_vars(
            self.ubr_index, ['substrate'], key_list)

        substrate_ctr = structure.substrate_ctr(self.var_list, self.iq,
                                                sub_ubr, sub_index,
                                                self.absorption, self.h,
                                                self.h, self.scale, key_list)

        def d_refine(d_var):

            fctr = structure.film_ctr(self.var_list, self.var_table, d_var,
                                      sindex, self.iq, self.h, self.k, ['dw'])

            ss = np.multiply(np.mat(fctr) + substrate_ctr,
                             np.mat(r).T) * self.intensity
            # calculate r square
            sa = np.asarray(ss).reshape(-1)
            ss_res = np.sum((abs(self.i) * bgm - abs(sa) * bgm)**2)
            varience = ss_res / ss_tot

            print(int(varience * 1e4) / 1e4)

            return varience

        d0 = subr
        re = so.minimize(d_refine, d0, method='Nelder-Mead', tol=1)

        self.ubr_index = tc.refresh_index(self.ubr_index, re.x, sindex)

        # plot the refined result

        fctr = structure.film_ctr(self.var_list, self.var_table, re.x, sindex,
                                  self.iq, self.h, self.k, ['dw'])

        ss = np.multiply(np.mat(fctr) + substrate_ctr, np.mat(r).T)
        plt.plot(self.iq, np.log(abs(ss * self.intensity)))
        plt.plot(self.iq, np.log(np.sqrt(self.i)))
예제 #5
0
파일: phase.py 프로젝트: Khan-Xu/Pyrod
CTR_ELECTR_PATH = os.path.abspath(os.path.dirname('ctr_optimised_result.pickle')) +\
                    '/ctr_optimised_result_electron.pickle'

ctr_factor = {}
ctr_electr = {}

with open(CTR_FACTOR_PATH, 'rb') as cf, open(CTR_ELECTR_PATH, 'rb') as ce:

    unpicklerf = pickle.Unpickler(cf)
    unpicklere = pickle.Unpickler(ce)

    ctr_factor = unpicklerf.load()
    ctr_electr = unpicklere.load()

mask = tc.bragg_mask(ctr_factor['q'], 3, 1, 'yin')
amask = tc.bragg_mask(ctr_factor['q'], 3, 1, 'yang')
locate = np.where(mask != 0)[0].tolist()

factor_a = ctr_factor['substrate_ctr'] + ctr_factor['slab_ctr']
electr_a = ctr_electr['substrate_ctr'] + ctr_electr['slab_ctr']

q_mask = ctr_factor['q'][locate]
factor_m = factor_a[locate]
electr_m = electr_a[locate]
data_m = ctr_factor['shkl'][locate]

trans = abs(factor_m) / abs(electr_m)
data_e = data_m / trans

trans_s = tt.savitzky_golay(trans, 7, 1)