123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- """
- Created on Sun Aug 26 21:15:03 2018
- @author: USER
- """
- from __future__ import absolute_import
- """junction of xrr and ctr data"""
- from exceptions import (Data_Load_Exception)
- import pickle
- import os
- import numpy as np
- import scipy.interpolate as si
- import matplotlib.pyplot as plt
- from tool import control
- CTR_PATH = os.path.abspath(os.path.dirname('ctr_optimised_result.pickle')) +\
- '/data/ctr_optimised_result.pickle'
-
- XRR_PATH = os.path.abspath(os.path.dirname('xrr_optimised_result.pickle')) +\
- '/data/xrr_optimised_result.pickle'
- FIT_PATH = os.path.abspath(os.path.dirname('fit.pickle')) +\
- '/data/fit.pickle'
-
-
- ctr = {}
- xrr = {}
- try:
-
-
- with open(CTR_PATH, 'rb') as c:
- unpickler = pickle.Unpickler(c)
- ctr = unpickler.load()
-
-
- with open(XRR_PATH, 'rb') as x:
- unpickler = pickle.Unpickler(x)
- xrr = unpickler.load()
-
- except Data_Load_Exception:
- print('Loading ctr or xrr optimised result error!')
- PLINE = np.ones([len(ctr['shkl']), 3])
- PLINE[:,0] = abs(ctr['substrate_ctr'] + ctr['slab_ctr'])
- PLINE[:,1] = abs(xrr['rt']*xrr['inten'] + ctr['substrate_ctr'])
- PLINE[:,2] = ctr['shkl']
-
- def pick_points(line, number):
-
-
- fig, ax = plt.subplots()
-
- ax.set_title('pick points', picker = True)
-
- l1,l2,l3, = ax.plot(np.log(line), 'o', picker = number)
-
-
- x = []
- y = []
-
-
- def click(event):
-
-
- tline = event.artist
- ind = event.ind
-
-
- xdata = np.take(tline.get_xdata(), ind)[0]
- ydata = np.take(tline.get_ydata(), ind)[0]
-
-
- x.append(xdata)
- y.append(ydata)
-
-
- print('x = ' + str(xdata))
- print('y = ' + str(ydata))
-
-
- fig.canvas.mpl_connect('pick_event', click)
-
-
- while True:
-
- if len(x) < number:
-
- plt.pause(0.5)
-
- else:
- break
-
- plt.close(fig)
-
- return x,y
- class connection(object):
-
- def __init__(self, bond):
-
- self.bond = bond
- self.data = np.zeros(len(ctr['q']))
- self.r_square = 0
-
-
- def show(self):
-
- fig, ax = plt.subplots()
-
- line1, = ax.plot(ctr['q'], np.log(PLINE[:,0]), 'g')
- line2, = ax.plot(ctr['q'], np.log(PLINE[:,1]), 'y')
- line3, = ax.plot(ctr['q'], np.log(PLINE[:,2]), 'r')
-
- plt.legend((line1, line2, line3), ('ctr','xrr','raw data'))
-
-
- def connect(self, b = 0.98):
-
- self.bond = b
-
- con_data = np.zeros(len(ctr['q']))
- location = np.argmin(abs(ctr['q'] - self.bond))
-
- con_data[0: location] = PLINE[0: location, 1]
- con_data[location:, ] = PLINE[location:, 0]
-
- con_f = si.interp1d(ctr['q'], con_data)
- self.data = con_f(ctr['q'])
-
- plt.scatter(ctr['q'], np.log(PLINE[:,2]), s = 5, c = 'r')
- plt.plot(ctr['q'], np.log(PLINE[:,2]), c = 'r')
- plt.plot(ctr['q'], np.log(self.data))
-
-
-
- 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
-
-
- def save(self):
-
- fit = {'q': ctr['q'],
- 'fit_y': self.data,
- 'shkl': ctr['shkl'],
- 'r_square': self.r_square}
-
- f = open(FIT_PATH, 'wb')
- pickle.dump(fit, f)
- f.close()
|