ctr.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Aug 16 00:07:08 2018
  4. @author: USER
  5. """
  6. # Codes are free to use. Do whatever you want
  7. from __future__ import absolute_import
  8. """CTR data fitting"""
  9. ####################### LIBRARY #############################
  10. # exceptions library
  11. # Python stdlib imports
  12. import pickle
  13. import os
  14. # data processing library
  15. import numpy as np
  16. import pandas as pd
  17. import scipy.optimize as so
  18. import matplotlib.pyplot as plt
  19. import openpyxl as ox
  20. # pyrod library
  21. from read.read_parameters import initialization_parameters
  22. from read.read_raw_data import initialization_rhkl
  23. import tool.control as tc
  24. import tool.tools as tt
  25. from tool.properties import (roughness, vacancy, relax)
  26. import structure
  27. ####################### CONSTANT ############################
  28. # constant
  29. data_path = os.path.abspath(os.path.dirname('ctr_optimised_result.pickle')) +\
  30. '/data/ctr_optimised_result.pickle'
  31. ####################### FUNCTIONS ###########################
  32. ######################## CLASSS #############################
  33. class var_refine(object):
  34. # class for modulate the parameters maunally or opimisly
  35. def __init__(self, parameters, experiment_data):
  36. # init var_list and table
  37. self.parameters = parameters
  38. self.para_path = os.path.abspath(os.path.dirname(parameters)) + parameters
  39. self.experiment_data = experiment_data
  40. self.data_path = os.path.abspath(os.path.dirname(experiment_data)) + experiment_data
  41. init_para = initialization_parameters(self.parameters)
  42. init_para._check_parameters()
  43. init_para._check_sheetname()
  44. init_data = initialization_rhkl(self.experiment_data)
  45. init_data._check_filename()
  46. # real time read xlsx, real time refresh
  47. self.var_list = init_para._var_list()
  48. self.var_table = init_para._var_table()
  49. # import factors
  50. self.properties = pd.read_excel(self.para_path,sheet_name = 'factors',index_col = 0)
  51. # init experiment data iq and intensity
  52. self.iq,self.i = init_data._read_rhkl(experiment_data)
  53. # whick rod
  54. self.h = self.properties.at['h','data']
  55. self.k = self.properties.at['k','data']
  56. # inti parameters
  57. self.absorption = self.properties.at['absorption','data']
  58. self.roughness = self.properties.at['roughness','data']
  59. self.scale = self.properties.at['scale','data']
  60. self.intensity = self.properties.at['intensity','data']
  61. # interface roughness
  62. self.iroughness = self.properties.at['interface roughness','data']
  63. # contral parameters
  64. self.ubr_index = tc.contral_vars(self.var_list)
  65. def track_difference(self):
  66. var_list_rei = initialization_parameters._var_list(self.parameters)
  67. track_flag = 0
  68. try:
  69. for slabi in var_list_rei:
  70. for keyi in var_list_rei[slabi]:
  71. # compare every key value in new excel and old excel
  72. if keyi in ['absorption','atoms_num','layers_num','roughness']:
  73. # refreshed data and old data
  74. value_rei = var_list_rei[slabi][keyi]
  75. value_old = self.var_list[slabi][keyi]
  76. # if same,continue. if change,flag = 1
  77. if np.equal(value_rei,value_old):
  78. continue
  79. else:
  80. track_flag = 1
  81. # these parameters are matrix
  82. elif keyi in ['dis','dw','ions','lattice','nel','ocu','pos']:
  83. # refreshed matrix and old matrix
  84. if False in np.equal(var_list_rei[slabi][keyi],
  85. self.var_list[slabi][keyi]).as_matrix():
  86. track_flag = 1
  87. break
  88. else:
  89. continue
  90. # beak from the inner loop
  91. if track_flag:
  92. break
  93. # break from the outer loop
  94. if track_flag:
  95. break
  96. except:
  97. track_flag = 1
  98. properties = pd.read_excel(self.para_path,sheet_name = 'factors',index_col = 0)
  99. if False in np.equal(properties,self.properties).as_matrix():
  100. track_flag = 1
  101. # refresh
  102. if track_flag == 1:
  103. init_para = initialization_parameters(self.parameters)
  104. init_data = initialization_rhkl(self.experiment_data)
  105. self.var_list = init_para._var_list()
  106. self.var_table = init_para._var_table()
  107. # import factors
  108. self.properties = pd.read_excel(self.para_path,sheet_name = 'factors',index_col = 0)
  109. # init experiment data iq and intensity
  110. self.iq,self.i = init_data._read_rhkl(self.experiment_data)
  111. # whick rod
  112. self.h = self.properties.at['h','data']
  113. self.k = self.properties.at['k','data']
  114. # inti parameters
  115. self.absorption = self.properties.at['absorption','data']
  116. self.roughness = self.properties.at['roughness','data']
  117. self.scale = self.properties.at['scale','data']
  118. self.intensity = self.properties.at['intensity','data']
  119. # interface roughness
  120. self.iroughness = self.properties.at['interface roughness','data']
  121. # contral parameters
  122. self.ubr_index = tc.contral_vars(self.var_list)
  123. return self,track_flag
  124. def re(self):
  125. init_para = initialization_parameters(self.parameters)
  126. init_data = initialization_rhkl(self.experiment_data)
  127. self.var_list = init_para._var_list()
  128. self.var_table = init_para._var_table()
  129. init_para = initialization_parameters(self.parameters)
  130. init_data = initialization_rhkl(self.experiment_data)
  131. # import factors
  132. self.properties = pd.read_excel(self.para_path,sheet_name = 'factors',index_col = 0)
  133. # init experiment data iq and intensity
  134. self.iq,self.i = init_data._read_rhkl(self.experiment_data)
  135. # whick rod
  136. self.h = self.properties.at['h','data']
  137. self.k = self.properties.at['k','data']
  138. # inti parameters
  139. self.absorption = self.properties.at['absorption','data']
  140. self.roughness = self.properties.at['roughness','data']
  141. self.scale = self.properties.at['scale','data']
  142. self.intensity = self.properties.at['intensity','data']
  143. # interface roughness
  144. self.iroughness = self.properties.at['interface roughness','data']
  145. def disp(self):
  146. # list all the self parameters
  147. print("--------------------------------------------")
  148. print("parameters: %s\n" % self.parameters)
  149. print("experiment_data: %s\n" % self.experiment_data)
  150. print("h k qz: %s %s %s\n" % (int(self.h),
  151. int(self.k),
  152. [np.min(self.iq),np.max(self.iq)]))
  153. print("intensity: %s\n" % self.intensity)
  154. print("absorption: %s\n" % self.absorption)
  155. print("roughness: %s\n" % self.roughness)
  156. print("scale: %s\n" % self.scale)
  157. print("var_list: %s\n" %self.var_list.keys())
  158. print('var_table: %s' %self.var_table.keys())
  159. print("roughness interface: %s" %self.iroughness)
  160. print('--------------------------------------------')
  161. def sub_ctr(self):
  162. # the used keys
  163. key_list = ['absorption', 'roughness', 'scale']
  164. # input substrate ubr and substrate index
  165. subr,sindex = tc.initialize_contral_vars(self.ubr_index,
  166. ['substrate'],
  167. key_list)
  168. substrate_ctr = structure.substrate_ctr(self.var_list,
  169. self.iq,
  170. subr,
  171. sindex,
  172. self.absorption,
  173. self.h,
  174. self.h,
  175. self.scale,
  176. key_list)
  177. # roughness
  178. cr = roughness(self.iq)
  179. r = cr.robinson_roughness(self.roughness)
  180. return np.multiply(self.intensity*substrate_ctr, np.mat(r).T)
  181. def slab_ctr(self):
  182. # used keys
  183. key_list = ['lattice_abc','dw']
  184. # input slab ubr and slab index
  185. slab_ubr,slab_index = tc.initialize_contral_vars(self.ubr_index,
  186. np.unique(self.var_table['slab_list']).tolist(),
  187. key_list)
  188. fctr = structure.film_ctr(self.var_list,
  189. self.var_table,
  190. slab_ubr,
  191. slab_index,
  192. self.iq,
  193. self.h,
  194. self.k,
  195. key_list)
  196. # roughness
  197. cr = roughness(self.iq)
  198. r = cr.robinson_roughness(self.roughness)
  199. # interface roughness
  200. ir = cr.interface_roughness(self.iroughness)
  201. return np.multiply(np.multiply(self.intensity*fctr, np.mat(r).T),np.mat(ir).T)
  202. def lattice_refine(self, p_mask = 13, n_mask = 3, weight = 1):
  203. # Prepare the data mask
  204. # yin mask to mask the bragg peaks
  205. bn_mask = np.mat(tc.bragg_mask(self.iq,
  206. n_mask,1,
  207. mode = 'yin'))
  208. # yang mask to refine the signal around bragg peaks only
  209. bp_mask = np.mat(tc.bragg_mask(self.iq,
  210. p_mask,1,
  211. mode = 'yang'))
  212. bgm = np.asarray(np.multiply(bn_mask, bp_mask).T).reshape(-1)*weight
  213. # calculate ss tot.For only seveal signal points are calculate
  214. # mean value is np.sum(abs(i)*bgm)/np.sum(bgm)
  215. ss_tot = np.sum((abs(self.i)*bgm - \
  216. np.sum(abs(self.i)*bgm)/np.sum(bgm))**2)
  217. # initialize the variable about the lattice
  218. slab_list = np.unique(self.var_table['slab_list']).tolist()
  219. subr,sindex = tc.initialize_contral_vars(self.ubr_index,
  220. slab_list,
  221. ['lattice_abc'])
  222. # substrate ctr
  223. # the used keys
  224. key_list = ['absorption', 'roughness', 'scale']
  225. # input substrate ubr and substrate index
  226. sub_ubr,sub_index = tc.initialize_contral_vars(self.ubr_index,
  227. ['substrate'],
  228. key_list)
  229. substrate_ctr = structure.substrate_ctr(self.var_list,
  230. self.iq,
  231. sub_ubr,
  232. sub_index,
  233. self.absorption,
  234. self.h,
  235. self.h,
  236. self.scale,
  237. key_list)
  238. # roughness
  239. cr = roughness(self.iq)
  240. r = cr.robinson_roughness(self.roughness)
  241. def c_refine(c_var):
  242. # input lattice c variable into subr
  243. if len(slab_list) == 1:
  244. subr[2] = c_var
  245. elif len(slab_list) >= 2:
  246. for slabi in range(len(slab_list)):
  247. subr[slabi*3-1] = c_var[slabi]
  248. fctr = structure.film_ctr(self.var_list,
  249. self.var_table,
  250. subr,
  251. sindex,
  252. self.iq,
  253. self.h,
  254. self.k,
  255. ['lattice_abc'])
  256. ss = np.multiply(np.mat(fctr) + substrate_ctr,np.mat(r).T)*self.intensity
  257. # calculate r square
  258. sa = np.asarray(ss).reshape(-1)
  259. ss_res = np.sum((abs(self.i)*bgm - abs(sa)*bgm)**2)
  260. varience = ss_res/ss_tot
  261. print(int(varience*1e4)/1e4)
  262. return varience
  263. # select properity optimized method for lattice_c optimize
  264. # if there are only one or two variables, step_brute method is a direct method
  265. if len(slab_list) == 1:
  266. print('lattice c optimising....\nOPT_STEP_BRUTE method is used')
  267. rec = tt.opt_step_brute(c_refine,[[0.8,1.2]],grid_size = 20)
  268. elif len(slab_list) == 2:
  269. print('lattice c optimising....\nOPT_STEP_BRUTE method is used')
  270. rec = tt.opt_step_brute(c_refine,[[0.8,1.2],[0.8,1.2]],grid_size = 10)
  271. # for larger variable number, Nelder-Mead method is more comparable
  272. elif len(slab_list) >= 3:
  273. print('lattice c optimising....\nNELDER_MEAD method is used')
  274. c0 = np.ones(len(slab_list)).tolist()
  275. resc = so.minimize(c_refine,c0,method = 'Nelder-Mead',tol=10)
  276. rec = resc.x
  277. # update subr
  278. if len(slab_list) == 1:
  279. subr[2] = rec
  280. elif len(slab_list) >= 2:
  281. for slabi in range(len(slab_list)):
  282. subr[slabi*3-1] = rec[0][slabi]
  283. # update ubr_index
  284. self.ubr_index = tc.refresh_index(self.ubr_index,subr,sindex)
  285. # plot the refined result
  286. fctr = structure.film_ctr(self.var_list,
  287. self.var_table,
  288. subr,
  289. sindex,
  290. self.iq,
  291. self.h,
  292. self.k,
  293. ['lattice_abc'])
  294. ss = np.multiply(np.mat(fctr) + substrate_ctr,np.mat(r).T)
  295. plt.plot(self.iq,np.log(abs(ss*self.intensity)))
  296. plt.plot(self.iq,np.log(np.sqrt(self.i)))
  297. def dw_refine(self,
  298. p_mask = 13,
  299. n_mask = 3,
  300. weight = 1,
  301. key_list = ['dw']):
  302. # Prepare the data mask
  303. # yin mask to mask the bragg peaks
  304. bn_mask = np.mat(tc.bragg_mask(self.iq,
  305. n_mask,1,
  306. mode = 'yin'))
  307. # yang mask to refine the signal around bragg peaks only
  308. bp_mask = np.mat(tc.bragg_mask(self.iq,
  309. p_mask,1,
  310. mode = 'yang'))
  311. bgm = np.asarray(np.multiply(bn_mask, bp_mask).T).reshape(-1)*weight
  312. # calculate ss tot.For only seveal signal points are calculate
  313. # mean value is np.sum(abs(i)*bgm)/np.sum(bgm)
  314. ss_tot = np.sum((abs(self.i)*bgm -
  315. np.sum(abs(self.i)*bgm)/np.sum(bgm))**2)
  316. # initialize the variable about the lattice
  317. slab_list = np.unique(self.var_table['slab_list']).tolist()
  318. ubr_index = tc.contral_vars(self.var_list)
  319. subr,sindex = tc.initialize_contral_vars(ubr_index,
  320. slab_list,
  321. ['dw'])
  322. # roughness
  323. cr = roughness(self.iq)
  324. r = cr.robinson_roughness(self.roughness)
  325. # substrate ctr
  326. # the used keys
  327. key_list = ['absorption', 'roughness', 'scale']
  328. # input substrate ubr and substrate index
  329. sub_ubr,sub_index = tc.initialize_contral_vars(self.ubr_index,
  330. ['substrate'],
  331. key_list)
  332. substrate_ctr = structure.substrate_ctr(self.var_list,
  333. self.iq,
  334. sub_ubr,
  335. sub_index,
  336. self.absorption,
  337. self.h,
  338. self.h,
  339. self.scale,
  340. key_list)
  341. def d_refine(d_var):
  342. fctr = structure.film_ctr(self.var_list,
  343. self.var_table,
  344. d_var,
  345. sindex,
  346. self.iq,
  347. self.h,
  348. self.k,
  349. ['dw'])
  350. ss = np.multiply(np.mat(fctr) +
  351. substrate_ctr,np.mat(r).T)*self.intensity
  352. # calculate r square
  353. sa = np.asarray(ss).reshape(-1)
  354. ss_res = np.sum((abs(self.i)*bgm - abs(sa)*bgm)**2)
  355. varience = ss_res/ss_tot
  356. print(int(varience*1e4)/1e4)
  357. return varience
  358. d0 = subr
  359. re = so.minimize(d_refine,d0,method = 'Nelder-Mead',tol=1)
  360. self.ubr_index = tc.refresh_index(self.ubr_index,re.x,sindex)
  361. # plot the refined result
  362. fctr = structure.film_ctr(self.var_list,
  363. self.var_table,
  364. re.x,
  365. sindex,
  366. self.iq,
  367. self.h,
  368. self.k,
  369. ['dw'])
  370. ss = np.multiply(np.mat(fctr) + substrate_ctr,np.mat(r).T)
  371. plt.plot(self.iq,np.log(abs(ss*self.intensity)))
  372. plt.plot(self.iq,np.log(np.sqrt(self.i)))
  373. # return the updated ubr_index, some parameters are list in ubr_index
  374. def update_var(self):
  375. # pl--parameters location in excel file
  376. pl = {'absorption':'B3',
  377. 'layers_num':'B4',
  378. 'lattice':'B6:G6',
  379. 'lattice_abc':'B6:D6',
  380. 'ocu':'B10:F10',
  381. 'dw':'B12:F14',
  382. 'dis':'B16:F18',
  383. 'pos':'B22:F24'}
  384. #p x--parameters xlsx workbook
  385. px = ox.load_workbook(self.para_path)
  386. # check the slab in ubr_index
  387. for slabi in self.ubr_index:
  388. # intensity is not a slab should be picked out
  389. if slabi == 'intensity':
  390. continue
  391. # start update the slab data
  392. else:
  393. # parameters loop
  394. for keyi in self.ubr_index[slabi]:
  395. # only several parameters are updated from ubr_index
  396. # scale roughness vacancy veta are not
  397. if keyi in ['scale','roughness','vacancy','beta']:
  398. pass
  399. else:
  400. # key is the parameters from ubr_index
  401. key = np.mat(self.ubr_index[slabi][keyi])
  402. # psheet is the slabi sheet
  403. psheet = px[slabi]
  404. # cell is the cell of parameters in slabi sheet
  405. cell = psheet[pl[keyi]]
  406. # parameters absorptio is a singe value
  407. if keyi == 'absorption':
  408. # key_var is the value of parameters from var_list
  409. key_var = self.var_list[slabi][keyi]
  410. # key is the contral variable from ubr_index
  411. # key_var is the parameter value from excel file
  412. cell.value = key[0,0]*key_var
  413. elif keyi == 'lattice_abc':
  414. # print(key)
  415. # only apply lattice abc
  416. key_var = self.var_list[slabi]['lattice']
  417. # print(key_var)
  418. ci = 0
  419. # loop cell and update value
  420. # in openpyxl, cell can only be valued in this way
  421. for row in cell:
  422. for element in row:
  423. if ci <= 2:
  424. element.value = key[0, ci]*key_var.as_matrix()[0,ci]
  425. print(element.value)
  426. ci += 1
  427. else:
  428. pass
  429. else:
  430. # for matrix parameters
  431. key_var = self.var_list[slabi][keyi]
  432. # the row index
  433. r = 0
  434. # the column index
  435. c = 0
  436. for row in cell:
  437. for element in row:
  438. # apply the contral variable to origin data
  439. element.value = key[r, c]*key_var.as_matrix()[r, c]
  440. # re init the column index
  441. if c == key.shape[1]-1:
  442. c = c - key.shape[1]+1
  443. else:
  444. # loop column
  445. c += 1
  446. # loop row
  447. r += 1
  448. try:
  449. px.save(self.para_path)
  450. except:
  451. print('Error!!Please close the excel file %s and try again'%self.parameters)
  452. def pos_modulate(self, bc, sc, cl):
  453. subr0 = self.var_table['posz_list']
  454. slab_index = 'posz'
  455. subr0 = relax.strain_relax(subr0, cl, bc, sc)
  456. key_list = ['pos']
  457. print(subr0)
  458. fctr = structure.film_ctr(self.var_list,
  459. self.var_table,
  460. subr0,
  461. slab_index,
  462. self.iq,
  463. self.h,
  464. self.k,
  465. key_list)
  466. # roughness
  467. cr = roughness(self.iq)
  468. r = cr.robinson_roughness(self.roughness)
  469. # interface roughness
  470. ir = cr.interface_roughness(self.iroughness)
  471. return np.multiply(np.multiply(self.intensity*fctr, np.mat(r).T),np.mat(ir).T)
  472. def surface_modulate(self,thrface = 0.9,secface = 0.9,surface = 1.1):
  473. subr0 = self.var_table['posz_list']
  474. slab_index = 'posz'
  475. subr0[-3] = thrface
  476. subr0[-1] = surface
  477. subr0[-2] = secface
  478. key_list = ['pos']
  479. self.var_table['posz_list'][ 0] = thrface
  480. self.var_table['posz_list'][-1] = surface
  481. self.var_table['posz_list'][ 1] = secface
  482. # print(subr0)
  483. fctr = structure.film_ctr(self.var_list,
  484. self.var_table,
  485. subr0,
  486. slab_index,
  487. self.iq,
  488. self.h,
  489. self.k,
  490. key_list)
  491. # roughness
  492. cr = roughness(self.iq)
  493. r = cr.robinson_roughness(self.roughness)
  494. # interface roughness
  495. ir = cr.interface_roughness(self.iroughness)
  496. return np.multiply(np.multiply(self.intensity*fctr, np.mat(r).T),np.mat(ir).T)
  497. def pos_refine(self):
  498. # the used keys
  499. sub_key_list = ['absorption', 'roughness', 'scale']
  500. # input substrate ubr and substrate index
  501. sub_ubr,sub_index = tc.initialize_contral_vars(self.ubr_index,
  502. ['substrate'],
  503. sub_key_list)
  504. substrate_ctr = structure.substrate_ctr(self.var_list,
  505. self.iq,
  506. sub_ubr,
  507. sub_index,
  508. self.absorption,
  509. self.h,
  510. self.h,
  511. self.scale,
  512. sub_key_list)
  513. # roughness
  514. cr = roughness(self.iq)
  515. r = cr.robinson_roughness(self.roughness)
  516. s = np.multiply(self.intensity*substrate_ctr, np.mat(r).T)
  517. subr0 = self.var_table['posz_list']
  518. slab_index = 'posz'
  519. key_list = ['pos']
  520. mask = tc.bragg_mask(self.iq,3,1,mode = 'yin',limit = 24)
  521. # fig, ax = plt.subplots()
  522. def varience(subr):
  523. fctr = structure.film_ctr(self.var_list,
  524. self.var_table,
  525. subr,
  526. slab_index,
  527. self.iq,
  528. self.h,
  529. self.k,
  530. key_list)
  531. # roughness
  532. cr = roughness(self.iq)
  533. # interface roughness
  534. ir = cr.interface_roughness(self.iroughness)
  535. f = np.multiply(np.multiply(self.intensity*fctr, np.mat(r).T),np.mat(ir).T)
  536. a = s + f
  537. e = np.sqrt(self.i)
  538. # plt.cla()
  539. # plt.plot(np.log10(e))
  540. # plt.plot(np.log10(abs(a)))
  541. v = np.sum(abs(mask*(np.log10(abs(a)+1e-6) - np.log10(e+1e-6))))
  542. print(v)
  543. return v
  544. re = so.minimize(varience,subr0,
  545. method = 'Nelder-Mead',
  546. options = {'maxiter':50})
  547. subr1 = re.x
  548. fctr1 = structure.film_ctr(self.var_list,
  549. self.var_table,
  550. subr1,
  551. slab_index,
  552. self.iq,
  553. self.h,
  554. self.k,
  555. key_list)
  556. # roughness
  557. cr = roughness(self.iq)
  558. # interface roughness
  559. ir = cr.interface_roughness(self.iroughness)
  560. return np.multiply(np.multiply(self.intensity*fctr1, np.mat(r).T),np.mat(ir).T)
  561. # return the initlatised parameters
  562. def return_self(self):
  563. return self
  564. class vr(var_refine):
  565. # plot experiment data
  566. def p_rhkl(self):
  567. plt.plot(self.iq, np.log(abs(self.i) + 1e-5))
  568. # plot sqrt experiment data
  569. def p_shkl(self):
  570. plt.plot(self.iq, np.log(np.sqrt(abs(self.i) + 1e-5)),'o')
  571. # plot substrate data
  572. def p_sctr(self):
  573. plt.plot(self.iq, np.log(abs(var_refine.sub_ctr(self))))
  574. # plot slab data
  575. def p_fctr(self):
  576. plt.plot(self.iq, np.log(abs(var_refine.slab_ctr(self))))
  577. # plot sumed data -- substrate data + slab data
  578. def p_actr(self):
  579. plt.plot(self.iq, np.log(abs(var_refine.sub_ctr(self) + var_refine.slab_ctr(self))))
  580. # return the complex data of substrate, slab. The sqrt data of experiment data
  581. # compare the fitting data and experiment data
  582. def p_c(self):
  583. plt.plot(self.iq, np.log(abs(var_refine.sub_ctr(self) + var_refine.slab_ctr(self))))
  584. plt.scatter(self.iq, np.log(np.sqrt(abs(self.i) + 1e-5)),s = 5, c = 'r')
  585. plt.plot(self.iq, np.log(np.sqrt(abs(self.i) + 1e-5)), c = 'r')
  586. # plot the pos_modulate result
  587. def p_p(self, bc, sc, cl):
  588. plt.plot(self.iq, np.log(abs(var_refine.sub_ctr(self) + var_refine.pos_modulate(self, bc, sc, cl))))
  589. plt.scatter(self.iq, np.log(np.sqrt(abs(self.i) + 1e-5)),s = 5, c = 'r')
  590. # plot the surface modulate result
  591. def p_s(self,thrface,secface,surface):
  592. plt.plot(self.iq, np.log(abs(var_refine.sub_ctr(self) + var_refine.surface_modulate(self, thrface, secface, surface))))
  593. plt.scatter(self.iq, np.log(np.sqrt(abs(self.i) + 1e-5)),s = 5, c = 'r')
  594. plt.plot(self.iq, np.log(np.sqrt(abs(self.i) + 1e-5)), c = 'r')
  595. # plot the pos_refine result
  596. def p_r(self):
  597. plt.plot(self.iq, np.log(abs(var_refine.sub_ctr(self) + var_refine.pos_refine(self))))
  598. plt.scatter(self.iq, np.log(np.sqrt(abs(self.i) + 1e-5)),s = 5, c = 'r')
  599. plt.plot(self.iq, np.log(np.sqrt(abs(self.i) + 1e-5)), c = 'r')
  600. # return the fitting and sqrt experiment data
  601. def rdata(self):
  602. return {'iq':np.asarray(self.iq).reshape(-1),
  603. 'shkl':np.asarray(np.sqrt(self.i) + 1e-5).reshape(-1),
  604. 'substrate_ctr':np.asarray(var_refine.sub_ctr(self)).reshape(-1),
  605. 'slab_ctr':np.asarray(var_refine.slab_ctr(self)).reshape(-1)}
  606. # save modulated data to folder /data
  607. def save(self):
  608. ctr_optimised_result = {'q':np.asarray(self.iq).reshape(-1),
  609. 'shkl':np.asarray(np.sqrt(self.i) + 1e-5).reshape(-1),
  610. 'substrate_ctr':np.asarray(var_refine.sub_ctr(self)).reshape(-1),
  611. 'slab_ctr':np.asarray(var_refine.slab_ctr(self)).reshape(-1)}
  612. f = open(data_path, 'wb')
  613. pickle.dump(ctr_optimised_result, f)
  614. f.close()