- Timestamp:
- Mar 2, 2015 3:31:50 PM (10 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- c93122e
- Parents:
- e4f421c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/calculator/sas_gen.py
rfd5ac0d re3f77d8b 10 10 import copy 11 11 12 MF actor_AM = 2.853E-1213 MF actor_mT = 2.3164E-912 MFACTOR_AM = 2.853E-12 13 MFACTOR_MT = 2.3164E-9 14 14 METER2ANG = 1.0E+10 15 15 #Avogadro constant [1/mol] … … 21 21 sldm = Dm * mag where Dm = gamma * classical elec. radius/(2*Bohr magneton) 22 22 Dm ~ 2.853E-12 [A^(-2)] ==> Shouldn't be 2.90636E-12 [A^(-2)]??? 23 """ 23 """ 24 24 if v_unit == "A/m": 25 factor = MF actor_AM25 factor = MFACTOR_AM 26 26 elif v_unit == "mT": 27 factor = MF actor_mT27 factor = MFACTOR_MT 28 28 else: 29 29 raise ValueError, "Invalid valueunit" … … 33 33 def transform_center(pos_x, pos_y, pos_z): 34 34 """ 35 re-center 36 35 re-center 37 36 :return: posx, posy, posz [arrays] 38 """ 37 """ 39 38 posx = pos_x - (min(pos_x) + max(pos_x)) / 2.0 40 39 posy = pos_y - (min(pos_y) + max(pos_y)) / 2.0 41 posz = pos_z - (min(pos_z) + max(pos_z)) / 2.0 42 return posx, posy, posz 40 posz = pos_z - (min(pos_z) + max(pos_z)) / 2.0 41 return posx, posy, posz 43 42 44 43 class GenSAS(BaseComponent): … … 49 48 """ 50 49 Init 51 52 50 :Params sld_data: MagSLD object 53 51 """ … … 69 67 ## Define parameters 70 68 self.params = {} 71 self.params['scale'] 72 self.params['background'] 73 self.params['solvent_SLD'] 69 self.params['scale'] = 1.0 70 self.params['background'] = 0.0 71 self.params['solvent_SLD'] = 0.0 74 72 self.params['total_volume'] = 1.0 75 self.params['Up_frac_in'] 76 self.params['Up_frac_out'] 77 self.params['Up_theta'] 73 self.params['Up_frac_in'] = 1.0 74 self.params['Up_frac_out'] = 1.0 75 self.params['Up_theta'] = 0.0 78 76 self.description = 'GenSAS' 79 77 ## Parameter details [units, min, max] 80 78 self.details = {} 81 self.details['scale'] 79 self.details['scale'] = ['', None, None] 82 80 self.details['background'] = ['[1/cm]', None, None] 83 self.details['solvent_SLD'] 84 self.details['total_volume'] 85 self.details['Up_frac_in'] 86 self.details['Up_frac_out'] 81 self.details['solvent_SLD'] = ['1/A^(2)', None, None] 82 self.details['total_volume'] = ['A^(3)', None, None] 83 self.details['Up_frac_in'] = ['[u/(u+d)]', None, None] 84 self.details['Up_frac_out'] = ['[u/(u+d)]', None, None] 87 85 self.details['Up_theta'] = ['[deg]', None, None] 88 86 # fixed parameters 89 87 self.fixed = [] 90 91 def set_pixel_volumes(self, volume): 88 89 def set_pixel_volumes(self, volume): 92 90 """ 93 91 Set the volume of a pixel in (A^3) unit … … 97 95 raise 98 96 self.data_vol = volume 99 100 def set_is_avg(self, is_avg=False): 97 98 def set_is_avg(self, is_avg=False): 101 99 """ 102 100 Sets is_avg: [bool] 103 101 """ 104 102 self.is_avg = is_avg 105 103 106 104 def _gen(self, x, y, i): 107 105 """ … … 109 107 :Param x: array of x-values 110 108 :Param y: array of y-values 111 :Param i: array of initial i-value 109 :Param i: array of initial i-value 112 110 :return: function value 113 111 """ … … 122 120 sldn = copy.deepcopy(self.data_sldn) 123 121 sldn -= self.params['solvent_SLD'] 124 model = mod.new_GenI(len_x, pos_x, pos_y, pos_z, 125 sldn, self.data_mx, self.data_my, 122 model = mod.new_GenI(len_x, pos_x, pos_y, pos_z, 123 sldn, self.data_mx, self.data_my, 126 124 self.data_mz, self.data_vol, 127 self.params['Up_frac_in'], 128 self.params['Up_frac_out'], 125 self.params['Up_frac_in'], 126 self.params['Up_frac_out'], 129 127 self.params['Up_theta']) 130 128 if y == []: … … 135 133 return self.params['scale'] * vol_correction * i + \ 136 134 self.params['background'] 137 138 def set_sld_data(self, sld_data=None): 135 136 def set_sld_data(self, sld_data=None): 139 137 """ 140 138 Sets sld_data … … 152 150 self.data_total_volume = sum(sld_data.vol_pix) 153 151 self.params['total_volume'] = sum(sld_data.vol_pix) 154 152 155 153 def getProfile(self): 156 154 """ 157 Get SLD profile 158 155 Get SLD profile 159 156 : return: sld_data 160 157 """ 161 158 return self.sld_data 162 163 def run(self, x =0.0):164 """ 159 160 def run(self, x=0.0): 161 """ 165 162 Evaluate the model 166 163 :param x: simple value … … 173 170 i_out = numpy.zeros_like(x[0]) 174 171 # 1D I is found at y =0 in the 2D pattern 175 out = self._gen(x[0], [], i_out 172 out = self._gen(x[0], [], i_out) 176 173 return out 177 174 else: 178 175 msg = "Q must be given as list of qx's and qy's" 179 176 raise ValueError, msg 180 181 def runXY(self, x =0.0):182 """ 177 178 def runXY(self, x=0.0): 179 """ 183 180 Evaluate the model 184 181 :param x: simple value … … 193 190 msg = "Q must be given as list of qx's and qy's" 194 191 raise ValueError, msg 195 192 196 193 def evalDistribution(self, qdist): 197 194 """ 198 195 Evaluate a distribution of q-values. 199 * For 1D, a numpy array is expected as input: evalDistribution(q) where q is a numpy array. 200 * For 2D, a list of numpy arrays are expected: [qx_prime,qy_prime], where 1D arrays. 201 :param qdist: ndarray of scalar q-values or list [qx,qy] where qx,qy are 1D ndarrays 196 * For 1D, a numpy array is expected as input: 197 evalDistribution(q) where q is a numpy array. 198 * For 2D, a list of numpy arrays are expected: 199 [qx_prime,qy_prime], where 1D arrays. 200 :param qdist: ndarray of scalar q-values or list [qx,qy] 201 where qx,qy are 1D ndarrays 202 202 """ 203 203 if qdist.__class__.__name__ == 'list': … … 211 211 mesg += "a list [qx,qy] where qx,qy are arrays." 212 212 raise RuntimeError, mesg 213 214 class OMF2SLD :213 214 class OMF2SLD(object): 215 215 """ 216 216 Convert OMFData to MAgData 217 217 """ 218 def __init__(self): 218 def __init__(self): 219 219 """ 220 220 Init … … 230 230 self.output = None 231 231 self.omfdata = None 232 232 233 233 def set_data(self, omfdata, shape='rectangular'): 234 234 """ … … 237 237 self.omfdata = omfdata 238 238 length = int(omfdata.xnodes * omfdata.ynodes * omfdata.znodes) 239 pos_x = numpy.arange(omfdata.xmin, 240 omfdata.xnodes*omfdata.xstepsize + omfdata.xmin, 239 pos_x = numpy.arange(omfdata.xmin, 240 omfdata.xnodes*omfdata.xstepsize + omfdata.xmin, 241 241 omfdata.xstepsize) 242 pos_y = numpy.arange(omfdata.ymin, 243 omfdata.ynodes*omfdata.ystepsize + omfdata.ymin, 242 pos_y = numpy.arange(omfdata.ymin, 243 omfdata.ynodes*omfdata.ystepsize + omfdata.ymin, 244 244 omfdata.ystepsize) 245 pos_z = numpy.arange(omfdata.zmin, 246 omfdata.znodes*omfdata.zstepsize + omfdata.zmin, 245 pos_z = numpy.arange(omfdata.zmin, 246 omfdata.znodes*omfdata.zstepsize + omfdata.zmin, 247 247 omfdata.zstepsize) 248 248 self.pos_x = numpy.tile(pos_x, int(omfdata.ynodes * omfdata.znodes)) … … 254 254 self.mz = omfdata.mz 255 255 self.sld_n = numpy.zeros(length) 256 256 257 257 if omfdata.mx == None: 258 258 self.mx = numpy.zeros(length) … … 261 261 if omfdata.mz == None: 262 262 self.mz = numpy.zeros(length) 263 263 264 264 self._check_data_length(length) 265 265 self.remove_null_points(False, False) … … 286 286 except: 287 287 pass 288 self.output = MagSLD(self.pos_x[mask], self.pos_y[mask], 289 self.pos_z[mask], self.sld_n[mask], 288 self.output = MagSLD(self.pos_x[mask], self.pos_y[mask], 289 self.pos_z[mask], self.sld_n[mask], 290 290 self.mx[mask], self.my[mask], self.mz[mask]) 291 291 self.output.set_pix_type('pixel') 292 292 self.output.set_pixel_symbols('pixel') 293 293 294 294 def get_omfdata(self): 295 295 """ … … 297 297 """ 298 298 return self.omfdata 299 299 300 300 def get_output(self): 301 301 """ … … 303 303 """ 304 304 return self.output 305 305 306 306 def _check_data_length(self, length): 307 307 """ … … 322 322 if len(self.mz) != length: 323 323 raise ValueError, msg 324 324 325 325 def remove_null_points(self, remove=False, recenter=False): 326 326 """ … … 328 328 """ 329 329 if remove: 330 is_nonzero = (numpy.fabs(self.mx) + numpy.fabs(self.my) + 331 numpy.fabs(self.mz)).nonzero() 332 if len(is_nonzero[0]) > 0: 330 is_nonzero = (numpy.fabs(self.mx) + numpy.fabs(self.my) + 331 numpy.fabs(self.mz)).nonzero() 332 if len(is_nonzero[0]) > 0: 333 333 self.pos_x = self.pos_x[is_nonzero] 334 334 self.pos_y = self.pos_y[is_nonzero] … … 342 342 self.pos_y -= (min(self.pos_y) + max(self.pos_y)) / 2.0 343 343 self.pos_z -= (min(self.pos_z) + max(self.pos_z)) / 2.0 344 344 345 345 def get_magsld(self): 346 346 """ … … 348 348 """ 349 349 return self.output 350 351 352 class OMFReader :350 351 352 class OMFReader(object): 353 353 """ 354 354 Class to load omf/ascii files (3 columns w/header). … … 356 356 ## File type 357 357 type_name = "OMF ASCII" 358 358 359 359 ## Wildcards 360 360 type = ["OMF files (*.OMF, *.omf)|*.omf"] 361 361 ## List of allowed extensions 362 362 ext = ['.omf', '.OMF'] 363 363 364 364 def read(self, path): 365 365 """ … … 368 368 :return: x, y, z, sld_n, sld_mx, sld_my, sld_mz 369 369 """ 370 #data_conv_m = None371 370 desc = "" 372 371 mx = numpy.zeros(0) … … 374 373 mz = numpy.zeros(0) 375 374 try: 376 data_conv_val = None377 data_conv_mesh = None378 375 input_f = open(path, 'rb') 379 376 buff = input_f.read() … … 454 451 valuerangemaxmag = s_line[1].lstrip() 455 452 if s_line[0].lower().count("end") > 0: 456 #output.set_sldms(mx, my, mz)457 453 output.filename = os.path.basename(path) 458 454 output.oommf = oommf … … 487 483 raise RuntimeError, msg 488 484 489 class PDBReader :485 class PDBReader(object): 490 486 """ 491 487 PDB reader class: limited for reading the lines starting with 'ATOM' … … 495 491 type = ["pdb files (*.PDB, *.pdb)|*.pdb"] 496 492 ## List of allowed extensions 497 ext = ['.pdb', '.PDB'] 498 493 ext = ['.pdb', '.PDB'] 494 499 495 def read(self, path): 500 496 """ 501 497 Load data file 502 498 503 499 :param path: file path 504 505 500 :return: MagSLD 506 507 501 :raise RuntimeError: when the file can't be opened 508 502 """ … … 521 515 x_lines = [] 522 516 y_lines = [] 523 z_lines = [] 517 z_lines = [] 524 518 try: 525 519 input_f = open(path, 'rb') … … 527 521 lines = buff.split('\n') 528 522 input_f.close() 529 pre_num = 0530 523 num = 0 531 524 for line in lines: … … 535 528 line[0:6].strip() == 'ATOM': 536 529 # define fields of interest 537 atom_id = line[6:11].strip()538 530 atom_name = line[12:16].strip() 539 531 try: … … 543 535 if len(atom_name) == 4: 544 536 atom_name = atom_name[0].upper() 545 elif line[12] != ' ': 537 elif line[12] != ' ': 546 538 atom_name = atom_name[0].upper() + \ 547 atom_name[1].lower() 539 atom_name[1].lower() 548 540 else: 549 541 atom_name = atom_name[0].upper() 550 #res_name = line[17:20].strip()551 #chain_name = line[21:22].strip()552 542 _pos_x = float(line[30:38].strip()) 553 543 _pos_y = float(line[38:46].strip()) 554 544 _pos_z = float(line[46:54].strip()) 555 556 545 pos_x = numpy.append(pos_x, _pos_x) 557 546 pos_y = numpy.append(pos_y, _pos_y) 558 547 pos_z = numpy.append(pos_z, _pos_z) 559 548 try: 560 # sld in Ang unit (from fm)561 #val = formula(atom_name).atoms.keys()[0].neutron.b_c562 #val *= 1.0e-5563 549 val = nsf.neutron_sld(atom_name)[0] 564 550 # sld in Ang^-2 unit … … 571 557 except: 572 558 print "Error: set the sld of %s to zero"% atom_name 573 sld_n = numpy.append(sld_n, 0.0)559 sld_n = numpy.append(sld_n, 0.0) 574 560 sld_mx = numpy.append(sld_mx, 0) 575 561 sld_my = numpy.append(sld_my, 0) 576 562 sld_mz = numpy.append(sld_mz, 0) 577 563 pix_symbol = numpy.append(pix_symbol, atom_name) 578 elif line[0:6].strip().count('CONECT') > 0 564 elif line[0:6].strip().count('CONECT') > 0: 579 565 toks = line.split() 580 num = int(toks[1]) - 1 581 val_list = [] 566 num = int(toks[1]) - 1 567 val_list = [] 582 568 for val in toks[2:]: 583 569 try: … … 588 574 break 589 575 val_list.append(int_val) 590 #need val_list ordered 576 #need val_list ordered 591 577 for val in val_list: 592 578 index = val - 1 … … 599 585 z_line.append((pos_z[num], pos_z[index])) 600 586 if len(x_line) > 0: 601 x_lines.append(x_line) 587 x_lines.append(x_line) 602 588 y_lines.append(y_line) 603 z_lines.append(z_line) 589 z_lines.append(z_line) 604 590 except: 605 591 pass 606 #re-centering 607 #pos_x -= (min(pos_x) + max(pos_x)) / 2.0 608 #pos_y -= (min(pos_y) + max(pos_y)) / 2.0 609 #pos_z -= (min(pos_z) + max(pos_z)) / 2.0 610 592 611 593 output = MagSLD(pos_x, pos_y, pos_z, sld_n, sld_mx, sld_my, sld_mz) 612 594 output.set_conect_lines(x_line, y_line, z_line) … … 619 601 return output 620 602 except: 621 RuntimeError, "%s is not a sld file" % path603 raise RuntimeError, "%s is not a sld file" % path 622 604 623 605 def write(self, path, data): … … 625 607 Write 626 608 """ 627 #Not implemented628 609 print "Not implemented... " 629 610 630 class SLDReader :611 class SLDReader(object): 631 612 """ 632 613 Class to load ascii files (7 columns). … … 634 615 ## File type 635 616 type_name = "SLD ASCII" 636 637 617 ## Wildcards 638 618 type = ["sld files (*.SLD, *.sld)|*.sld", … … 641 621 ## List of allowed extensions 642 622 ext = ['.sld', '.SLD', '.txt', '.TXT', '.*'] 643 644 623 def read(self, path): 645 624 """ 646 625 Load data file 647 648 626 :param path: file path 649 650 627 :return MagSLD: x, y, z, sld_n, sld_mx, sld_my, sld_mz 651 652 628 :raise RuntimeError: when the file can't be opened 653 629 :raise ValueError: when the length of the data vectors are inconsistent … … 663 639 try: 664 640 # Use numpy to speed up loading 665 input_f = numpy.loadtxt(path, dtype='float', skiprows=1, 641 input_f = numpy.loadtxt(path, dtype='float', skiprows=1, 666 642 ndmin=1, unpack=True) 667 643 pos_x = numpy.array(input_f[0]) … … 676 652 vol_pix = numpy.array(input_f[7]) 677 653 elif ncols == 7: 678 654 vol_pix = None 679 655 except: 680 656 # For older version of numpy … … 693 669 _sld_my = float(toks[5]) 694 670 _sld_mz = float(toks[6]) 695 696 671 pos_x = numpy.append(pos_x, _pos_x) 697 672 pos_y = numpy.append(pos_y, _pos_y) … … 702 677 sld_mz = numpy.append(sld_mz, _sld_mz) 703 678 try: 704 _vol_pix = 679 _vol_pix = float(toks[7]) 705 680 vol_pix = numpy.append(vol_pix, _vol_pix) 706 681 except: … … 709 684 # Skip non-data lines 710 685 pass 711 712 # re-center 713 #pos_x -= (min(pos_x) + max(pos_x)) / 2.0 714 #pos_y -= (min(pos_y) + max(pos_y)) / 2.0 715 #pos_z -= (min(pos_z) + max(pos_z)) / 2.0 716 717 output = MagSLD(pos_x, pos_y, pos_z, sld_n, 686 output = MagSLD(pos_x, pos_y, pos_z, sld_n, 718 687 sld_mx, sld_my, sld_mz) 719 720 688 output.filename = os.path.basename(path) 721 689 output.set_pix_type('pixel') … … 725 693 return output 726 694 except: 727 RuntimeError, "%s is not a sld file" % path728 695 raise RuntimeError, "%s is not a sld file" % path 696 729 697 def write(self, path, data): 730 698 """ 731 699 Write sld file 732 733 :Param path: file path 700 :Param path: file path 734 701 :Param data: MagSLD data object 735 702 """ … … 738 705 if data == None: 739 706 raise ValueError, "Missing the data to save." 740 741 707 x_val = data.pos_x 742 708 y_val = data.pos_y 743 709 z_val = data.pos_z 744 710 vol_pix = data.vol_pix 745 746 711 length = len(x_val) 747 748 712 sld_n = data.sld_n 749 713 if sld_n == None: 750 sld_n = numpy.zerros(length) 751 714 sld_n = numpy.zeros(length) 752 715 sld_mx = data.sld_mx 753 716 if sld_mx == None: 754 sld_mx = numpy.zer ros(length)755 sld_my = numpy.zer ros(length)756 sld_mz = numpy.zer ros(length)717 sld_mx = numpy.zeros(length) 718 sld_my = numpy.zeros(length) 719 sld_mz = numpy.zeros(length) 757 720 else: 758 721 sld_my = data.sld_my 759 722 sld_mz = data.sld_mz 760 761 723 out = open(path, 'w') 762 724 # First Line: Column names 763 725 out.write("X Y Z SLDN SLDMx SLDMy SLDMz VOLUMEpix") 764 726 for ind in range(length): 765 out.write("\n%g %g %g %g %g %g %g %g" % (x_val[ind], y_val[ind], 766 z_val[ind], sld_n[ind], 767 sld_mx[ind], sld_my[ind], 768 sld_mz[ind], vol_pix[ind])) 769 out.close() 770 771 772 class OMFData: 727 out.write("\n%g %g %g %g %g %g %g %g" % \ 728 (x_val[ind], y_val[ind], z_val[ind], sld_n[ind], 729 sld_mx[ind], sld_my[ind], sld_mz[ind], vol_pix[ind])) 730 out.close() 731 732 733 class OMFData(object): 773 734 """ 774 735 OMF Data. … … 808 769 self.valuerangeminmag = 0 809 770 self.valuerangemaxmag = 0 810 771 811 772 def __str__(self): 812 773 """ 813 774 doc strings 814 775 """ 815 _str = 776 _str = "Type: %s\n" % self.__class__.__name__ 816 777 _str += "File: %s\n" % self.filename 817 778 _str += "OOMMF: %s\n" % self.oommf … … 823 784 _str += "ybase: %s [%s]\n" % (str(self.ybase), self.meshunit) 824 785 _str += "zbase: %s [%s]\n" % (str(self.zbase), self.meshunit) 825 _str += "xstepsize: %s [%s]\n" % (str(self.xstepsize), 786 _str += "xstepsize: %s [%s]\n" % (str(self.xstepsize), 826 787 self.meshunit) 827 _str += "ystepsize: %s [%s]\n" % (str(self.ystepsize), 788 _str += "ystepsize: %s [%s]\n" % (str(self.ystepsize), 828 789 self.meshunit) 829 _str += "zstepsize: %s [%s]\n" % (str(self.zstepsize), 790 _str += "zstepsize: %s [%s]\n" % (str(self.zstepsize), 830 791 self.meshunit) 831 792 _str += "xnodes: %s\n" % str(self.xnodes) … … 840 801 _str += "valueunit: %s\n" % self.valueunit 841 802 _str += "valuemultiplier: %s\n" % str(self.valuemultiplier) 842 _str += "ValueRangeMinMag:%s [%s]\n" % (str(self.valuerangeminmag), 843 844 _str += "ValueRangeMaxMag:%s [%s]\n" % (str(self.valuerangemaxmag), 845 803 _str += "ValueRangeMinMag:%s [%s]\n" % (str(self.valuerangeminmag), 804 self.valueunit) 805 _str += "ValueRangeMaxMag:%s [%s]\n" % (str(self.valuerangemaxmag), 806 self.valueunit) 846 807 return _str 847 808 848 809 def set_m(self, mx, my, mz): 849 810 """ … … 853 814 self.my = my 854 815 self.mz = mz 855 856 class MagSLD :816 817 class MagSLD(object): 857 818 """ 858 819 Magnetic SLD. … … 869 830 _sld_unit = '1/A^(2)' 870 831 _pix_type = 'pixel' 871 872 def __init__(self, pos_x, pos_y, pos_z, sld_n=None, 832 833 def __init__(self, pos_x, pos_y, pos_z, sld_n=None, 873 834 sld_mx=None, sld_my=None, sld_mz=None, vol_pix=None): 874 835 """ … … 907 868 self.set_sldms(sld_mx, sld_my, sld_mz) 908 869 self.set_nodes() 909 870 910 871 def __str__(self): 911 872 """ 912 873 doc strings 913 874 """ 914 _str = 875 _str = "Type: %s\n" % self.__class__.__name__ 915 876 _str += "File: %s\n" % self.filename 916 877 _str += "Axis_unit: %s\n" % self.pos_unit 917 878 _str += "SLD_unit: %s\n" % self.sld_unit 918 879 return _str 919 880 920 881 def set_pix_type(self, pix_type): 921 882 """ … … 924 885 """ 925 886 self.pix_type = pix_type 926 887 927 888 def set_sldn(self, sld_n): 928 889 """ … … 931 892 if sld_n.__class__.__name__ == 'float': 932 893 if self.is_data: 933 # For data, put the value to only the pixels w non-zero M 934 is_nonzero = (numpy.fabs(self.sld_mx) + 935 numpy.fabs(self.sld_my) +936 numpy.fabs(self.sld_mz)).nonzero()894 # For data, put the value to only the pixels w non-zero M 895 is_nonzero = (numpy.fabs(self.sld_mx) + 896 numpy.fabs(self.sld_my) + 897 numpy.fabs(self.sld_mz)).nonzero() 937 898 self.sld_n = numpy.zeros(len(self.pos_x)) 938 899 if len(self.sld_n[is_nonzero]) > 0: … … 945 906 else: 946 907 self.sld_n = sld_n 947 908 948 909 def set_sldms(self, sld_mx, sld_my, sld_mz): 949 910 """ 950 Sets ( \|m\|, m_theta, m_phi)911 Sets (|m|, m_theta, m_phi) 951 912 """ 952 913 if sld_mx.__class__.__name__ == 'float': … … 964 925 965 926 sld_m = numpy.sqrt(sld_mx * sld_mx + sld_my * sld_my + \ 966 sld_mz * sld_mz) 927 sld_mz * sld_mz) 967 928 self.sld_m = sld_m 968 969 def set_pixel_symbols(self, symbol='pixel'): 929 930 def set_pixel_symbols(self, symbol='pixel'): 970 931 """ 971 932 Set pixel … … 978 939 else: 979 940 self.pix_symbol = symbol 980 981 def set_pixel_volumes(self, vol): 941 942 def set_pixel_volumes(self, vol): 982 943 """ 983 944 Set pixel volumes … … 998 959 """ 999 960 return self.sld_n 1000 1001 def get_sld_mxyz(self): 1002 """ 1003 Returns (sld_mx, sldmy, sld_mz) 1004 """ 1005 return (self.sld_mx, self.sldmy, self.sld_mz) 1006 1007 def get_sld_m(self): 1008 """ 1009 Returns (sldm, sld_theta, sld_phi) 1010 """ 1011 return (self.sldm, self.sld_theta, self.sld_phi) 1012 961 1013 962 def set_nodes(self): 1014 963 """ … … 1032 981 self.ynodes = None 1033 982 self.znodes = None 1034 983 1035 984 def set_stepsize(self): 1036 985 """ … … 1083 1032 return 1084 1033 self.has_conect = True 1085 self.line_x = line_x 1086 self.line_y = line_y 1034 self.line_x = line_x 1035 self.line_y = line_y 1087 1036 self.line_z = line_z 1088 1089 1037 1090 1038 def test_load(): 1091 1039 from sas.plottools.arrow3d import Arrow3D 1092 import os 1093 dir = os.path.abspath(os.path.curdir) 1094 print dir 1040 current_dir = os.path.abspath(os.path.curdir) 1041 print current_dir 1095 1042 for i in range(6): 1096 dir, _ = os.path.split(dir)1097 tfile = os.path.join( dir, "test", "CoreXY_ShellZ.txt")1098 ofile = os.path.join( dir, "test", "A_Raw_Example-1.omf")1043 current_dir, _ = os.path.split(current_dir) 1044 tfile = os.path.join(current_dir, "test", "CoreXY_ShellZ.txt") 1045 ofile = os.path.join(current_dir, "test", "A_Raw_Example-1.omf") 1099 1046 if os.path.isfile(tfile): 1100 1047 tfpath = tfile … … 1109 1056 1110 1057 import matplotlib.pyplot as plt 1111 from mpl_toolkits.mplot3d import Axes3D1112 1058 fig = plt.figure() 1113 1059 ax = fig.gca(projection='3d') 1114 ax.plot(output.pos_x, output.pos_y, output.pos_z, '.', c="g", 1115 alpha = 0.7, markeredgecolor='gray',rasterized=True)1060 ax.plot(output.pos_x, output.pos_y, output.pos_z, '.', c="g", 1061 alpha=0.7, markeredgecolor='gray', rasterized=True) 1116 1062 gap = 7 1117 1063 max_mx = max(output.sld_mx) 1118 1064 max_my = max(output.sld_my) 1119 1065 max_mz = max(output.sld_mz) 1120 max_m = max(max_mx, max_my,max_mz)1066 max_m = max(max_mx, max_my, max_mz) 1121 1067 x2 = output.pos_x+output.sld_mx/max_m * gap 1122 1068 y2 = output.pos_y+output.sld_my/max_m * gap 1123 1069 z2 = output.pos_z+output.sld_mz/max_m * gap 1124 x_arrow = numpy.column_stack((output.pos_x, x2))1125 y_arrow = numpy.column_stack((output.pos_y, y2))1126 z_arrow = numpy.column_stack((output.pos_z, z2))1070 x_arrow = numpy.column_stack((output.pos_x, x2)) 1071 y_arrow = numpy.column_stack((output.pos_y, y2)) 1072 z_arrow = numpy.column_stack((output.pos_z, z2)) 1127 1073 unit_x2 = output.sld_mx / max_m 1128 1074 unit_y2 = output.sld_my / max_m … … 1131 1077 color_y = numpy.fabs(unit_y2 * 0.8) 1132 1078 color_z = numpy.fabs(unit_z2 * 0.8) 1133 colors = 1134 a = Arrow3D(None, x_arrow, y_arrow,z_arrow, colors, mutation_scale=10, lw=1,1135 arrowstyle="->", color="y", alpha = 0.5)1136 1079 colors = numpy.column_stack((color_x, color_y, color_z)) 1080 a = Arrow3D(None, x_arrow, y_arrow, z_arrow, colors, 1081 mutation_scale=10, lw=1, arrowstyle="->", 1082 color="y", alpha=0.5) 1137 1083 ax.add_artist(a) 1138 1084 plt.show() 1139 1140 1085 def test(): 1141 import os 1142 dir = os.path.abspath(os.path.curdir) 1086 current_dir = os.path.abspath(os.path.curdir) 1143 1087 for i in range(3): 1144 dir, _ = os.path.split(dir) 1145 #tfile = os.path.join(dir, "test", "C_Example_Converted.txt") 1146 ofile = os.path.join(dir, "test", "A_Raw_Example-1.omf") 1088 current_dir, _ = os.path.split(current_dir) 1089 ofile = os.path.join(current_dir, "test", "A_Raw_Example-1.omf") 1147 1090 if os.path.isfile(ofile): 1148 #tfpath = tfile1149 1091 ofpath = ofile 1150 1092 break 1151 #reader = SLDReader()1152 1093 oreader = OMFReader() 1153 #output = reader.read(tfpath)1154 1094 ooutput = oreader.read(ofpath) 1155 foutput = OMF2SLD() 1156 foutput.set_data(ooutput) 1157 writer = SLDReader()1158 writer.write(os.path.join(os.path.dirname(ofpath), "out.txt"), 1095 foutput = OMF2SLD() 1096 foutput.set_data(ooutput) 1097 writer = SLDReader() 1098 writer.write(os.path.join(os.path.dirname(ofpath), "out.txt"), 1159 1099 foutput.output) 1160 1100 model = GenSAS() 1161 model.set_sld_data(foutput.output) 1101 model.set_sld_data(foutput.output) 1162 1102 x = numpy.arange(1000)/10000. + 1e-5 1163 1103 y = numpy.arange(1000)/10000. + 1e-5 1164 #z = numpy.arange(1000)/10000. + 1e-51165 1104 i = numpy.zeros(1000) 1166 out = model.runXY([x,y,i])1167 1168 if __name__ == "__main__": 1105 model.runXY([x, y, i]) 1106 1107 if __name__ == "__main__": 1169 1108 test() 1170 1109 test_load()
Note: See TracChangeset
for help on using the changeset viewer.