Changeset dbc5dc9b in sasview for sanscalculator
- Timestamp:
- Dec 21, 2012 10:37:59 AM (12 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:
- ab7e3a4
- Parents:
- 46be5e6
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sanscalculator/src/sans/calculator/sans_gen.py
r318b5bbb rdbc5dc9b 234 234 self.pos_z[mask], self.sld_n[mask], 235 235 self.mx[mask], self.my[mask], self.mz[mask]) 236 self.output.set_pix_type('pixel') 237 self.output.set_pixel_symbols('pixel') 236 238 237 239 def get_omfdata(self): … … 429 431 raise RuntimeError, msg 430 432 433 class PDBReader: 434 """ 435 PDB reader class: limited for reading the lines starting with 'ATOM' 436 """ 437 type_name = "PDB" 438 ## Wildcards 439 type = ["pdb files (*.PDB, *.pdb)|*.pdb"] 440 ## List of allowed extensions 441 ext = ['.pdb', '.PDB'] 442 443 def read(self, path): 444 """ 445 Load data file 446 447 :param path: file path 448 449 :return: MagSLD 450 451 :raise RuntimeError: when the file can't be opened 452 """ 453 pos_x = numpy.zeros(0) 454 pos_y = numpy.zeros(0) 455 pos_z = numpy.zeros(0) 456 sld_n = numpy.zeros(0) 457 sld_mx = numpy.zeros(0) 458 sld_my = numpy.zeros(0) 459 sld_mz = numpy.zeros(0) 460 pix_symbol = numpy.zeros(0)#numpy.array([]) 461 try: 462 input_f = open(path, 'rb') 463 buff = input_f.read() 464 #buff.replace(' ', '') 465 lines = buff.split('\n') 466 input_f.close() 467 for line in lines: 468 try: 469 toks = line.split() 470 # check if line starts with "ATOM" 471 if toks[0].count('ATM') > 0 or toks[0] == 'ATOM': 472 # define fields of interest 473 atom_id = toks[1] 474 atom_name = toks[2] 475 atom_name = atom_name[0].upper() + atom_name[1:].lower() 476 try: 477 float(toks[3]) 478 ind = 3 479 except: 480 ind = 4 481 res_name = toks[ind] 482 #chain_name = toks[3].strip() 483 #residue_id = toks[4].strip() 484 try: 485 float(toks[4]) 486 except: 487 ind += 1 488 ind += 1 489 _pos_x = float(toks[ind]) 490 ind += 1 491 _pos_y = float(toks[ind]) 492 ind += 1 493 _pos_z = float(toks[ind]) 494 pos_x = numpy.append(pos_x, _pos_x) 495 pos_y = numpy.append(pos_y, _pos_y) 496 pos_z = numpy.append(pos_z, _pos_z) 497 sld_n = numpy.append(sld_n, 7e-06) 498 sld_mx = numpy.append(sld_mx, 0) 499 sld_my = numpy.append(sld_my, 0) 500 sld_mz = numpy.append(sld_mz, 0) 501 pix_symbol = numpy.append(pix_symbol, atom_name) 502 except: 503 pass 504 #print pix_symbol, pos_x 505 pos_x -= (min(pos_x) + max(pos_x)) / 2.0 506 pos_y -= (min(pos_y) + max(pos_y)) / 2.0 507 pos_z -= (min(pos_z) + max(pos_z)) / 2.0 508 509 output = MagSLD(pos_x, pos_y, pos_z, sld_n, sld_mx, sld_my, sld_mz) 510 #output.set_sldms(0, 0, 0) 511 output.filename = os.path.basename(path) 512 output.set_pix_type('atom') 513 output.set_pixel_symbols(pix_symbol) 514 output.set_nodes() 515 output.sld_unit = 'A' 516 return output 517 except: 518 RuntimeError, "%s is not a sld file" % path 519 520 def write(self, path, data): 521 """ 522 Write 523 """ 524 #Not implemented 525 print "Not implemented... " 526 431 527 class SLDReader: 432 528 """ … … 448 544 :param path: file path 449 545 450 :return : x, y, z, sld_n, sld_mx, sld_my, sld_mz546 :return MagSLD: x, y, z, sld_n, sld_mx, sld_my, sld_mz 451 547 452 548 :raise RuntimeError: when the file can't be opened … … 492 588 output = MagSLD(pos_x, pos_y, pos_z, sld_n, sld_mx, sld_my, sld_mz) 493 589 output.filename = os.path.basename(path) 590 output.set_pix_type('pixel') 591 output.set_pixel_symbols('pixel') 494 592 return output 495 593 except: … … 636 734 _pos_unit = 'A' 637 735 _sld_unit = '1/A^(2)' 736 _pix_type = 'pixel' 638 737 639 738 def __init__(self, pos_x, pos_y, pos_z, sld_n=None, … … 654 753 self.pos_unit = self._pos_unit 655 754 self.sld_unit = self._sld_unit 755 self.pix_type = 'pixel' 656 756 self.pos_x = pos_x 657 757 self.pos_y = pos_y … … 664 764 self.sld_phi = None 665 765 self.sld_theta = None 766 self.pix_symbol = None 666 767 if sld_mx != None and sld_my != None and sld_mz != None: 667 768 self.set_sldms(sld_mx, sld_my, sld_mz) … … 678 779 return _str 679 780 781 def set_pix_type(self, pix_type): 782 """ 783 Set pixel type 784 :Param pix_type: string, 'pixel' or 'atom' 785 """ 786 self.pix_type = pix_type 787 680 788 def set_sldn(self, sld_n): 681 789 """ … … 685 793 if self.is_data: 686 794 # For data, put the value to only the pixels w non-zero M 795 is_nonzero = (numpy.fabs(self.sld_mx) + 796 numpy.fabs(self.sld_my) + 797 numpy.fabs(self.sld_mz)).nonzero() 687 798 self.sld_n = numpy.zeros(len(self.pos_x)) 688 i s_nonzero = (numpy.fabs(self.sld_mx) +689 numpy.fabs(self.sld_my) +690 numpy.fabs(self.sld_mz)).nonzero()691 self.sld_n[is_nonzero] = sld_n799 if len(self.sld_n[is_nonzero]) > 0: 800 self.sld_n[is_nonzero] = sld_n 801 else: 802 self.sld_n.fill(sld_n) 692 803 else: 693 804 # For non-data, put the value to all the pixels … … 716 827 sld_mz * sld_mz) 717 828 self.sld_m = sld_m 718 829 830 def set_pixel_symbols(self, symbol='pixel'): 831 """ 832 Set pixel 833 :Params pixel: str; pixel or atomic symbol, or array of strings 834 """ 835 if self.sld_n == None: 836 return 837 if symbol.__class__.__name__ == 'str': 838 self.pix_symbol = numpy.repeat(symbol, len(self.sld_n)) 839 else: 840 self.pix_symbol = symbol 841 719 842 def get_sldn(self): 720 843 """ … … 739 862 Set xnodes, ynodes, and znodes 740 863 """ 741 if not self.has_stepsize: 742 self.set_stepsize() 743 try: 744 xdist = (max(self.pos_x) - min(self.pos_x)) / self.xstepsize 745 ydist = (max(self.pos_y) - min(self.pos_y)) / self.ystepsize 746 zdist = (max(self.pos_z) - min(self.pos_z)) / self.zstepsize 747 self.xnodes = int(xdist) + 1 748 self.ynodes = int(ydist) + 1 749 self.znodes = int(zdist) + 1 750 except: 864 self.set_stepsize() 865 if self.pix_type == 'pixel': 866 try: 867 xdist = (max(self.pos_x) - min(self.pos_x)) / self.xstepsize 868 ydist = (max(self.pos_y) - min(self.pos_y)) / self.ystepsize 869 zdist = (max(self.pos_z) - min(self.pos_z)) / self.zstepsize 870 self.xnodes = int(xdist) + 1 871 self.ynodes = int(ydist) + 1 872 self.znodes = int(zdist) + 1 873 except: 874 self.xnodes = None 875 self.ynodes = None 876 self.znodes = None 877 else: 751 878 self.xnodes = None 752 879 self.ynodes = None … … 757 884 Set xtepsize, ystepsize, and zstepsize 758 885 """ 759 try: 760 xpos_pre = self.pos_x[0] 761 ypos_pre = self.pos_y[0] 762 zpos_pre = self.pos_z[0] 763 for x_pos in self.pos_x: 764 if xpos_pre != x_pos: 765 self.xstepsize = numpy.fabs(x_pos - xpos_pre) 766 break 767 for y_pos in self.pos_y: 768 if ypos_pre != y_pos: 769 self.ystepsize = numpy.fabs(y_pos - ypos_pre) 770 break 771 for z_pos in self.pos_z: 772 if zpos_pre != z_pos: 773 self.zstepsize = numpy.fabs(z_pos - zpos_pre) 774 break 775 self.has_stepsize = True 776 except: 886 if self.pix_type == 'pixel': 887 try: 888 xpos_pre = self.pos_x[0] 889 ypos_pre = self.pos_y[0] 890 zpos_pre = self.pos_z[0] 891 for x_pos in self.pos_x: 892 if xpos_pre != x_pos: 893 self.xstepsize = numpy.fabs(x_pos - xpos_pre) 894 break 895 for y_pos in self.pos_y: 896 if ypos_pre != y_pos: 897 self.ystepsize = numpy.fabs(y_pos - ypos_pre) 898 break 899 for z_pos in self.pos_z: 900 if zpos_pre != z_pos: 901 self.zstepsize = numpy.fabs(z_pos - zpos_pre) 902 break 903 self.has_stepsize = True 904 except: 905 self.xstepsize = None 906 self.ystepsize = None 907 self.zstepsize = None 908 self.has_stepsize = False 909 else: 777 910 self.xstepsize = None 778 911 self.ystepsize = None 779 912 self.zstepsize = None 780 self.has_stepsize = False913 self.has_stepsize = True 781 914 return self.xstepsize, self.ystepsize, self.zstepsize 782 915
Note: See TracChangeset
for help on using the changeset viewer.