def getValidation(self): core_dic = CifFile.CifDic(self.core_dic_filepath) mpod_dic = CifFile.CifDic(self.mpod_dic_filepath) for fil in self.filets[:]: filepath=os.path.join(self.cifs_dir, fil) df=CifFile.ReadCif(filepath) val_report = CifFile.validate(filepath, diclist=[self.core_dic_filepath, self.mpod_dic_filepath]) result = CifFile.validate_report(val_report) #self.resultFiles.append(result); rf = result.find('VALID') k=df.keys() #print result #print k if rf>-1: self.codeListValid.append(k[0]) self.resultListVaild.append(result) else: self.codeListInvalid.append(k[0]) self.resultListInvalid.append(result)
def get_geometry_vals(self, sub_tag, count=None, geometry='distances'): assert geometry == 'distances' or 'angles', 'geometry must be "distances" or "angles"' if count is None: cif = pycif.ReadCif( f'{self.path}/{sub_tag}/shelx/{self.tag}_{sub_tag}.cif') elif count == 'targ': cif = pycif.ReadCif( f'{self.path}/{sub_tag}/shelx/{self.tag}_targ.cif') else: cif = pycif.ReadCif( f'{self.path}/{sub_tag}/shelx/{self.tag}_{sub_tag}_count_{count}.cif' ) if geometry == 'angles': key = '_geom_angle' else: key = '_geom_bond_distance' vk = cif.visible_keys[0] x = dict(cif[vk])[key] vals, errs = np.zeros(len(x)), np.zeros(len(x)) for i, xi in enumerate(x): val, err = strerr2floaterrr(xi) vals[i] = val errs[i] = err return vals, errs
def test_attributes(self): """Test that attributes of complex expressions come out OK""" # We need to do a scary funky attribute of a key lookup ourdic = CifFile.CifDic("testdic2") testblock = CifFile.CifFile("test_data.cif")["testdata"] self.parser.loopable_cats = ['geom','position'] # teststrg = """ LineList = [] PointList = [] With p as position Loop g as geom { If (g.type == "point") { PointList += Tuple(g.vertex1_id,p[g.vertex1_id].vector_xyz) } #Else if (g.type == "line") { # LineList ++= Tuple(Tuple(g.vertex1_id, g.vertex2_id), # Tuple(p[g.vertex1_id].vector_xyz, # p[g.vertex2_id].vector_xyz)) #} } """ self.parser.target_id = 'PointList' res = self.parser.parse(teststrg+"\n",lexer=self.lexer) realfunc = drel_yacc.make_func(res,"myfunc","PointList") print "Function -> \n" + realfunc exec realfunc retval = myfunc(ourdic,testblock,"LineList") print "testdic2 return value" + `retval` print "Value for comparison with docs: %s" % `retval[0]`
def getValidation(self): core_dic = CifFile.CifDic(self.core_dic_filepath) mpod_dic = CifFile.CifDic(self.mpod_dic_filepath) for fil in self.filets[:]: filepath = os.path.join(self.cifs_dir, fil) df = CifFile.ReadCif(filepath) val_report = CifFile.validate( filepath, diclist=[self.core_dic_filepath, self.mpod_dic_filepath]) result = CifFile.validate_report(val_report) #self.resultFiles.append(result); rf = result.find('VALID') k = df.keys() print result print k if rf > -1: self.codeListValid.append(k[0]) self.resultListVaild.append(result) else: self.codeListInvalid.append(k[0]) self.resultListInvalid.append(result)
def get_xyzs(self, sub_tag, count=None): if count is None: cif = pycif.ReadCif( f'{self.path}/{sub_tag}/shelx/{self.tag}_{sub_tag}.cif') elif count == 'targ': cif = pycif.ReadCif( f'{self.path}/{sub_tag}/shelx/{self.tag}_targ.cif') else: cif = pycif.ReadCif( f'{self.path}/{sub_tag}/shelx/{self.tag}_{sub_tag}_count_{count}.cif' ) vk = cif.visible_keys[0] a = strerr2floaterrr(dict(cif[vk])['_cell_length_a'])[0] b = strerr2floaterrr(dict(cif[vk])['_cell_length_a'])[0] c = strerr2floaterrr(dict(cif[vk])['_cell_length_b'])[0] xstrs = dict(cif[vk])['_atom_site_fract_x'] ystrs = dict(cif[vk])['_atom_site_fract_y'] zstrs = dict(cif[vk])['_atom_site_fract_z'] xyzs = np.zeros((len(xstrs), 3)) for i, (fracx, fracy, fracz) in enumerate(zip(xstrs, ystrs, zstrs)): x = strerr2floaterrr(fracx)[0] * a y = strerr2floaterrr(fracy)[0] * b z = strerr2floaterrr(fracz)[0] * c xyzs[i, :] = [x, y, z] return xyzs
def setUp(self): """Write out a file, then read it in again. Non alphabetic ordering to check order preservation.""" # fill up the block with stuff items = (('_item_1', 'Some data'), ('_item_3', '34.2332'), ( '_item_4', 'Some very long data which we hope will overflow the single line and force printing of another line aaaaa bbbbbb cccccc dddddddd eeeeeeeee fffffffff hhhhhhhhh iiiiiiii jjjjjj' ), ('_item_2', 'Some_underline_data'), ('_item_empty', ''), ( '_item_quote', "'ABC" ), ('_item_apost', '"def'), ( '_item_sws', " \n " ), (('_item_5', '_item_7', '_item_6'), ([1, 2, 3, 4], [ 'a', 'b', 'c', 'd' ], [5, 6, 7, 8])), (('_string_1', '_string_2'), ([ ';this string begins with a semicolon', 'this string is way way too long and should overflow onto the next line eventually if I keep typing for long enough', ';just_any_old_semicolon-starting-string' ], [ 'a string with a final quote"', 'a string with a " and a safe\';', 'a string with a final \'' ]))) # save block items as well s_items = (('_sitem_1', 'Some save data'), ( '_sitem_2', 'Some_underline_data' ), ('_sitem_3', '34.2332'), ( '_sitem_4', 'Some very long data which we hope will overflow the single line and force printing of another line aaaaa bbbbbb cccccc dddddddd eeeeeeeee fffffffff hhhhhhhhh iiiiiiii jjjjjj' ), ( ( '_sitem_5', '_sitem_6', '_sitem_7'), ([1, 2, 3, 4], [5, 6, 7, 8], ['a', 'b', 'c', 'd']) ), (('_string_1', '_string_2'), ([ ';this string begins with a semicolon', 'this string is way way too long and should overflow onto the next line eventually if I keep typing for long enough', ';just_any_old_semicolon-starting-string' ], [ 'a string with a final quote"', 'a string with a " and a safe\';', 'a string with a final \'' ]))) self.cf = CifFile.CifBlock(items) self.save_block = CifFile.CifBlock(s_items) self.cf["saves"]["test_save_frame"] = self.save_block self.cfs = self.cf["saves"]["test_save_frame"] cif = CifFile.CifFile() cif['testblock'] = self.cf outfile = open('test.cif', 'w') outfile.write(str(cif)) outfile.close() self.ef = CifFile.CifFile('test.cif') self.df = self.ef['testblock'] self.dfs = self.df["saves"]["test_save_frame"] flfile = CifFile.ReadCif('test.cif', scantype="flex") self.flf = flfile['testblock'] self.flfs = self.flf["saves"]["test_save_frame"]
def __init__(self, filename, occupancy_tolerance=1.): self._occupancy_tolerance = occupancy_tolerance if isinstance(filename, basestring): with zopen(filename, "r") as f: # We use this round-about way to clean up the CIF first. stream = cStringIO.StringIO(_clean_cif(f.read())) self._cif = CifFile.ReadCif(stream) else: self._cif = CifFile.ReadCif(filename)
def testBlockName(self): """Make sure long block names cause errors""" df = CifFile.CifBlock() cf = CifFile.CifFile() try: cf['a_very_long_block_name_which_should_be_rejected_out_of_hand123456789012345678'] = df except CifFile.CifError: pass else: self.fail()
def setUp(self): #self.ddl1dic = CifFile.CifFile("dictionaries/cif_core.dic") #items = (("_atom_site_label","S1"), # ("_atom_site_fract_x","0.74799(9)"), # ("_atom_site_adp_type","Umpe"), # ("_this_is_not_in_dict","not here")) bl = CifFile.CifBlock() self.cf = CifFile.ValidCifFile(dic=ddl1dic) self.cf["test_block"] = bl self.cf["test_block"].AddCifItem( ("_atom_site_label", ["C1", "Cr2", "H3", "U4"]))
def testBlockOverwrite(self): """Upper/lower case should be seen as identical""" df = CifFile.CifBlock() ef = CifFile.CifBlock() cf = CifFile.CifFile() df['_random_1'] = 'oldval' ef['_random_1'] = 'newval' cf['_lowercaseblock'] = df cf['_LowerCaseBlock'] = ef assert (cf['_Lowercaseblock']['_random_1'] == 'newval') assert (len(cf) == 1)
def cif_by_ftp(ftp_ptr,store=True,directory="."): # print "Opening %s" % ftp_ptr if store: new_fn = os.path.split(urllib.url2pathname(ftp_ptr))[1] target = os.path.join(directory,new_fn) if target != ftp_ptr: urllib.urlretrieve(ftp_ptr,target) # print "Stored %s as %s" % (ftp_ptr,target) ret_cif = CifFile.CifFile(target) else: ret_cif = CifFile.ReadCif(ftp_ptr) return ret_cif
def setUp(self): self.offdic = CifFile.CifFile("dictionaries/dict_official") self.adic = CifFile.CifFile("dictionaries/dict_A") self.bdic = CifFile.CifFile("dictionaries/dict_B") self.cdic = CifFile.CifFile("dictionaries/dict_C") self.cvdica = CifFile.CifFile("dictionaries/cvdica.dic") self.cvdicb = CifFile.CifFile("dictionaries/cvdicb.dic") self.cvdicc = CifFile.CifFile("dictionaries/cvdicc.dic") self.cvdicd = CifFile.CifFile("dictionaries/cvdicd.dic") self.testcif = CifFile.CifFile("dictionaries/merge_test.cif")
def setUp(self): #create our lexer and parser self.lexer = drel_lex.lexer self.parser = drel_yacc.parser #use a simple dictionary self.testdic = CifFile.CifDic("testdic") self.testblock = CifFile.CifFile("testdic")["DDL_DIC"] #create the global namespace self.namespace = self.testblock.keys() self.namespace = dict(map(None,self.namespace,self.namespace)) self.parser.special_id = [self.namespace] self.parser.withtable = {} self.parser.target_id = None self.parser.indent = ""
def write_cif(filename, struct): """Q'n'D Cif writer. Uses PyCifRW. length: Angstrom Parameters ---------- filename : str name of output .cif file struct : Structure, length units Angstrom assumed """ ffmt = "%.16e" cf = pycifrw_CifFile.CifFile() block = pycifrw_CifFile.CifBlock() block['_cell_length_a'] = frepr(struct.cryst_const[0], ffmt=ffmt) block['_cell_length_b'] = frepr(struct.cryst_const[1], ffmt=ffmt) block['_cell_length_c'] = frepr(struct.cryst_const[2], ffmt=ffmt) block['_cell_angle_alpha'] = frepr(struct.cryst_const[3], ffmt=ffmt) block['_cell_angle_beta'] = frepr(struct.cryst_const[4], ffmt=ffmt) block['_cell_angle_gamma'] = frepr(struct.cryst_const[5], ffmt=ffmt) block['_symmetry_space_group_name_H-M'] = 'P 1' block['_symmetry_Int_Tables_number'] = 1 # assigning a list produces a "loop_" block['_symmetry_equiv_pos_as_xyz'] = ['x,y,z'] # atoms # # _atom_site_label: We just use symbols, which is then = # _atom_site_type_symbol, but we *could* use that to number atoms of each # specie, e.g. Si1, Si2, ..., Al1, Al2, ... data_names = [ '_atom_site_label', '_atom_site_fract_x', '_atom_site_fract_y', '_atom_site_fract_z', '_atom_site_type_symbol' ] _xyz2str = lambda arr: [ffmt % x for x in arr] data = [ struct.symbols, _xyz2str(struct.coords_frac[:, 0]), _xyz2str(struct.coords_frac[:, 1]), _xyz2str(struct.coords_frac[:, 2]), struct.symbols ] # "loop_" with multiple columns block.AddCifItem([[data_names], [data]]) cf['pwtools'] = block # maxoutlength = 2048 is default for cif 1.1 standard (which is default in # pycifrw 3.x). Reset default wraplength=80 b/c ASE's cif reader cannot # handle wrapped lines. common.file_write(filename, cf.WriteOut(wraplength=2048))
def __init__(self, filename, occupancy_tolerance=1.): """ Args: filename: Cif filename. bzipped or gzipped cifs are fine too. occupancy_tolerance: If total occupancy of a site is between 1 and occupancy_tolerance, the occupancies will be scaled down to 1. """ self._occupancy_tolerance = occupancy_tolerance if isinstance(filename, basestring): with zopen(filename, "r") as f: self._cif = CifFile.ReadCif(f) else: self._cif = CifFile.ReadCif(filename)
def cifToAtomAndCoordinateList(self): listOut = [] cifObj = CifFile.CifFile(self.filenameIn) keys = cifObj.keys() #for key in keys: # if cifObj[key]['_atom_site_label'] cifBlock = cifObj[ keys[0]] # for now just assume the data is in the first block #deal with the fact that many cif's are incomplete or are not done correctly if cifBlock.has_key('_atom_site_type_symbol'): atomTypes = cifBlock['_atom_site_type_symbol'] elif cifBlock.has_key('_atom_site_label'): atomTypes = cifBlock['_atom_site_label'] atomTypes = self._removeLabels(atomTypes) atomXs = cifBlock['_atom_site_fract_x'] atomYs = cifBlock['_atom_site_fract_y'] atomZs = cifBlock['_atom_site_fract_z'] for i in range(len(atomTypes)): listOut.append([ atomTypes[i], self._removeParens(atomXs[i]), self._removeParens(atomYs[i]), self._removeParens(atomZs[i]) ]) return listOut
def extract_cif(infile, folder, nodes_export_subfolder="nodes", aiida_export_subfolder="aiida", silent=False): """ Extract the nodes to be imported from a TCOD CIF file. TCOD CIFs, exported by AiiDA, may contain an importable subset of AiiDA database, which can be imported. This function prepares SandboxFolder with files required for import. :param infile: file path :param folder: a SandboxFolder, used to extract the file tree :param nodes_export_subfolder: name of the subfolder for AiiDA nodes :param aiida_export_subfolder: name of the subfolder for AiiDA data inside the TCOD CIF internal file tree :param silent: suppress debug print """ import os import urllib2 import CifFile from aiida.common.exceptions import ValidationError from aiida.common.utils import md5_file, sha1_file from aiida.tools.dbexporters.tcod import decode_textfield values = CifFile.ReadCif(infile) values = values[values.keys()[0]] # taking the first datablock in CIF for i in range(0,len(values['_tcod_file_id'])-1): name = values['_tcod_file_name'][i] if not name.startswith(aiida_export_subfolder+os.sep): continue dest_path = os.path.relpath(name,aiida_export_subfolder) if name.endswith(os.sep): if not os.path.exists(folder.get_abs_path(dest_path)): folder.get_subfolder(folder.get_abs_path(dest_path),create=True) continue contents = values['_tcod_file_contents'][i] if contents == '?' or contents == '.': uri = values['_tcod_file_uri'][i] if uri is not None and uri != '?' and uri != '.': contents = urllib2.urlopen(uri).read() encoding = values['_tcod_file_content_encoding'][i] if encoding == '.': encoding = None contents = decode_textfield(contents,encoding) if os.path.dirname(dest_path) != '': folder.get_subfolder(os.path.dirname(dest_path)+os.sep,create=True) with open(folder.get_abs_path(dest_path),'w') as f: f.write(contents) f.flush() md5 = values['_tcod_file_md5sum'][i] if md5 is not None: if md5_file(folder.get_abs_path(dest_path)) != md5: raise ValidationError("MD5 sum for extracted file '{}' is " "different from given in the CIF " "file".format(dest_path)) sha1 = values['_tcod_file_sha1sum'][i] if sha1 is not None: if sha1_file(folder.get_abs_path(dest_path)) != sha1: raise ValidationError("SHA1 sum for extracted file '{}' is " "different from given in the CIF " "file".format(dest_path))
def generateAllCoordinates(self): atomNCoords = self.cifToAtomAndCoordinateList() cifObj = CifFile.CifFile(self.filenameIn) keys = cifObj.keys() cifBlock = cifObj[ keys[0]] # for now just assume the data is in the first block ops = cifBlock['_symmetry_equiv_pos_as_xyz'] newPositions = [] for entry in atomNCoords: newEntryPositions = [] #operate on all these atoms with the symmetry operations for op in ops: # first parse the expressions newPosition = self.evaluateCrystOperation(op, entry[1:4]) # now bring it back inside the unit cell newPosition = self._backInUnitCell(newPosition) # add it to the list and move on newEntryPositions.append([entry[0]] + newPosition) # remove the non-_unique ones uniqueEntryPositions = self._unique(newEntryPositions) # add these to the total number of atoms in the unit cell newPositions.append(uniqueEntryPositions) # now make sure the total number of atoms is unique uniqueNewPositions = self._unique(self.flatten(newPositions)) return uniqueNewPositions
def read_file(file): try: readable = CifFile.ReadCif("./ICSD_111/" + file) except: readable = False print("Error reading" + file) return readable
def __init__(self, path, qmax=None, fill_peaks=False, rotk=None, rottheta=None, skip_sym=False, atomic=False): starcif = pycif.ReadCif(f'{path}') vk = starcif.visible_keys[0] cif_dict = dict(starcif[vk]) self._get_cell_params(cif_dict) self._get_cell_vecs(rotk=rotk, rottheta=rottheta) if not atomic: self._calc_scat(cif_dict, qmax=qmax, fill_peaks=fill_peaks, skip_sym=skip_sym) else: self._scat_bragg = None self._scat_rect = None self._scat_sph = None
def cif_handler(cif_file, pozycje_atomowe, stale_sieciowe, grupa_przestrzenna, dlugosc_fali): my_cif = CifFile.CifFile(cif_file) info_1 = my_cif.keys() info_2 = [] if pozycje_atomowe: atom_positions = my_cif[info_1[2]].GetLoop('_atom_site_fract_x') for item in atom_positions: item = item[0:5] info_2.append(item) return info_2 if stale_sieciowe: a_axis = my_cif[info_1[2]]['_cell_length_a'] b_axis = my_cif[info_1[2]]['_cell_length_b'] c_axis = my_cif[info_1[2]]['_cell_length_c'] wynik = "stałe sieciowe: a={} , b={} , c={}".format( a_axis, b_axis, c_axis) return wynik if grupa_przestrzenna: crystal_system = my_cif[info_1[2]]['_space_group_crystal_system'] space_group = my_cif[info_1[2]]['_space_group_name_H-M_alt'] wynik = "układ krystalograficzny: {}; grupa przestrzenna: {}".format( crystal_system, space_group) return wynik if dlugosc_fali: pass
def setUp(self): self.cf = CifFile.CifBlock() self.names = (('_item_name_1', '_item_name#2', '_item_%$#3'), ) self.values = (((1, 2, 3, 4), ('hello', 'good_bye', 'a space', '# 4'), (15.462, -99.34, 10804, 0.0001)), ) self.cf.AddCifItem((self.names, self.values)) self.cf['_non_loop_item'] = 'Non loop string item' self.cf['_number_item'] = 15.65
def testAOverlay(self): newdic = CifFile.merge_dic([self.offdic, self.adic], mergemode='overlay') # print newdic.__str__() self.assertRaises(CifFile.ValidCifError, CifFile.ValidCifFile, datasource="dictionaries/merge_test.cif", dic=newdic)
def __init__(self, filename, occupancy_tolerance=1.): """ Args: filename: Cif filename. bzipped or gzipped cifs are fine too. occupancy_tolerance: If total occupancy of a site is between 1 and occupancy_tolerance, the occupancies will be scaled down to 1. """ self._occupancy_tolerance = occupancy_tolerance if isinstance(filename, basestring): with zopen(filename, "r") as f: # We use this round-about way to clean up the CIF first. stream = cStringIO.StringIO(_clean_cif(f.read())) self._cif = CifFile.ReadCif(stream) else: self._cif = CifFile.ReadCif(filename)
def twins(folder, filename): cf = CifFile.ReadCif(os.path.join(folder, filename)) cb = cf[[key for key in cf.keys() \ if cf[key].get('_cell_length_a') is not None][0]] cif_dict = dict(cb.items()) cif_dict = {k.replace('.','_'):v for k,v in cif_dict.items()} if ('_twin_individual_id' in cif_dict): twin_ids = cif_dict['_twin_individual_id'] n_var = len(twin_ids) else: n_var = 1 if ('_twin_individual_mass_fraction_refined' in cif_dict): twin_mf = cif_dict['_twin_individual_mass_fraction_refined'] twin_mf = [float(re.sub(r'\([^()]*\)', '', mf)) for mf in twin_mf] T11s = cif_dict['_twin_individual_twin_matrix_11'] T12s = cif_dict['_twin_individual_twin_matrix_12'] T13s = cif_dict['_twin_individual_twin_matrix_13'] T21s = cif_dict['_twin_individual_twin_matrix_21'] T22s = cif_dict['_twin_individual_twin_matrix_22'] T23s = cif_dict['_twin_individual_twin_matrix_23'] T31s = cif_dict['_twin_individual_twin_matrix_31'] T32s = cif_dict['_twin_individual_twin_matrix_32'] T33s = cif_dict['_twin_individual_twin_matrix_33'] T11s = [float(re.sub(r'\([^()]*\)', '', T11)) for T11 in T11s] T12s = [float(re.sub(r'\([^()]*\)', '', T12)) for T12 in T12s] T13s = [float(re.sub(r'\([^()]*\)', '', T13)) for T13 in T13s] T21s = [float(re.sub(r'\([^()]*\)', '', T21)) for T21 in T21s] T22s = [float(re.sub(r'\([^()]*\)', '', T22)) for T22 in T22s] T23s = [float(re.sub(r'\([^()]*\)', '', T23)) for T23 in T23s] T31s = [float(re.sub(r'\([^()]*\)', '', T31)) for T31 in T31s] T32s = [float(re.sub(r'\([^()]*\)', '', T32)) for T32 in T32s] T33s = [float(re.sub(r'\([^()]*\)', '', T33)) for T33 in T33s] else: twin_mf = [1.0] T11s = [1.0] T12s = [0.0] T13s = [0.0] T21s = [0.0] T22s = [1.0] T23s = [0.0] T31s = [0.0] T32s = [0.0] T33s = [1.0] weight = np.array(twin_mf) U = np.stack((T11s,T12s,T13s, T21s,T22s,T23s, T31s,T32s,T33s)).T.reshape(n_var,3,3) return U, weight
def pycifrw_from_cif(datablocks, loops=None, names=None): """ Constructs PyCifRW's CifFile from an array of CIF datablocks. :param datablocks: an array of CIF datablocks :param loops: optional list of lists of CIF tag loops. :param names: optional list of datablock names :return: CifFile """ import CifFile from CifFile import CifBlock if loops is None: loops = dict() cif = CifFile.CifFile() try: cif.set_grammar("1.1") except AttributeError: # if no grammar can be set, we assume it's 1.1 (widespread standard) pass if names and len(names) < len(datablocks): raise ValueError("Not enough names supplied for " "datablocks: {} (names) < " "{} (datablocks)".format(len(names), len(datablocks))) for i, values in enumerate(datablocks): name = str(i) if names: name = names[i] datablock = CifBlock() cif[name] = datablock for loopname in loops.keys(): loopdata = ([[]], [[]]) row_size = None for tag in loops[loopname]: if tag in values: tag_values = values.pop(tag) if not isinstance(tag_values, list): tag_values = [tag_values] if row_size is None: row_size = len(tag_values) elif row_size != len(tag_values): raise ValueError("Number of values for tag " "'{}' is different from " "the others in the same " "loop".format(tag)) loopdata[0][0].append(tag) loopdata[1][0].append(tag_values) if row_size is not None and row_size > 0: datablock.AddCifItem(loopdata) for tag in sorted(values.keys()): datablock[tag] = values[tag] # create automatically a loop for non-scalar values if isinstance(values[tag], (tuple, list)) and tag not in loops.keys(): datablock.CreateLoop([tag]) return cif
def execute_with_options(options,args): if options.dictnames: diclist = map(lambda a:os.path.join(options.dirname,a),options.dictnames) print "Using following local dictionaries to validate:" for dic in diclist: print "%s" % dic fulldic = CifFile.merge_dic(diclist,mergemode='overlay') else: # print "Locating dictionaries using registry at %s" % options.registry dics = map(None,options.iucr_names,options.versions) dicurls = map(lambda a:locate_dic(a[0],a[1],regloc=options.registry,store_dir=options.dirname),dics) diccifs = map(lambda a:cif_by_ftp(a,options.store_flag,options.dirname),dicurls) fulldic = CifFile.merge_dic(diccifs) diclist = dicurls # for use in reporting later # open the cif file cf = CifFile.CifFile(args[0]) output_header(options.use_html,args[0],diclist) print CifFile.validate_report(CifFile.validate(cf,dic= fulldic,isdic=options.dict_flag),use_html=options.use_html) output_footer(options.use_html)
def get_shelx_rf(self, sub_tag, count=None): if count is None: cif = pycif.ReadCif( f'{self.path}/{sub_tag}/shelx/{self.tag}_{sub_tag}.cif') elif count == 'targ': cif = pycif.ReadCif( f'{self.path}/{sub_tag}/shelx/{self.tag}_targ.cif') else: cif = pycif.ReadCif( f'{self.path}/{sub_tag}/shelx/{self.tag}_{sub_tag}_count_{count}.cif' ) vk = cif.visible_keys[0] # _refine_ls_r_factor_all, _refine_ls_wr_factor_ref, _refine_ls_wr_factor_gt rf = dict(cif[vk])['_refine_ls_r_factor_all'] return float(rf)
def values(self): """ PyCifRW structure, representing the CIF datablocks. .. note:: requires PyCifRW module. """ if self._values is None: import CifFile self._values = CifFile.ReadCif(self.get_file_abs_path()) return self._values
def testAddSaveFrame(self): """Test adding a save frame""" s_items = (('_sitem_1', 'Some save data'), ( '_sitem_2', 'Some_underline_data' ), ('_sitem_3', '34.2332'), ( '_sitem_4', 'Some very long data which we hope will overflow the single line and force printing of another line aaaaa bbbbbb cccccc dddddddd eeeeeeeee fffffffff hhhhhhhhh iiiiiiii jjjjjj' ), (('_sitem_5', '_sitem_6', '_sitem_7'), ([1, 2, 3, 4], [5, 6, 7, 8], ['a', 'b', 'c', 'd']))) bb = CifFile.CifBlock(s_items) self.cf["saves"]["some_name"] = bb
def execute_method(self): valuename = sys.argv[1] #print "Valuename: %s" % valuename datablock_name = sys.argv[2] #print "Datablock: %s" % datablock_name #dictionary = sys.argv[3] #cbf_handle = sys.argv[3] #print "CBF handle: %s" % cbf_handle #create our lexer and parser self.lexer = drel_lex.lexer self.parser = drel_yacc.parser #use a simple dictionary testdic = CifFile.CifDic("cif_expanded.dic", grammar='DDLm') self.testblock = CifFile.CifFile("cbf_data_debug") [datablock_name] self.testblock.assign_dictionary(testdic) realres = self.testblock[valuename] #create the global namespace print "Generated value: %s" % realres fout = open("method_output", 'w') print>>fout, realres
def save(self, path, verbose=0): cif = pycif.CifFile() block = pycif.CifBlock() cif['block'] = block cif['block']['_symmetry.space_group_name_h-m'] = f'{self.spg}' cif['block']['_cell.angle_alpha'] = np.degrees(self.alpha) cif['block']['_cell.angle_beta'] = np.degrees(self.beta) cif['block']['_cell.angle_gamma'] = np.degrees(self.gamma) cif['block']['_cell.length_a'] = self.a_mag cif['block']['_cell.length_b'] = self.b_mag cif['block']['_cell.length_c'] = self.c_mag cif['block']['_refln.index_h'] = self.scat_bragg[:,0] cif['block']['_refln.index_k'] = self.scat_bragg[:,1] cif['block']['_refln.index_l'] = self.scat_bragg[:,2] cif['block']['_refln.intensity_meas'] = self.scat_bragg[:,3] cif['block'].CreateLoop( ['_refln.index_h', '_refln.index_k', '_refln.index_l', '_refln.intensity_meas'] ) outfile = open(path, 'w') outfile.write(cif.WriteOut()) outfile.close()
def testReport(self): CifFile.validate_report(CifFile.validate("tests/C13H2203_with_errors.cif",dic=ddl1dic))
core_dic_filepath="/EclipseWork/mpod/media/dictionary/cif_core.dic" mpod_dic_filepath="/EclipseWork/mpod/media/dictionary/cif_material_properties_0_0_6.dic" cifs_dir_output ="/EclipseWork/mpod/media/datafiles/test/" sql_out_file_path="/EclipseWork/mpod/media/datafiles/test/" fds=os.listdir(cifs_dir) fds2=filter(lambda x: x[-5:]==".mpod", fds) filets=sorted(filter(lambda x: os.path.isfile(os.path.join(cifs_dir, x)), fds2)) core_dic = CifFile.CifDic(core_dic_filepath) mpod_dic = CifFile.CifDic(mpod_dic_filepath) for fil in filets[:]: filepath=os.path.join(cifs_dir, fil) print filepath df=CifFile.ReadCif(filepath) val_report = CifFile.validate(filepath, diclist=[core_dic_filepath, mpod_dic_filepath]) print CifFile.validate_report(val_report) print df.keys() for fil in filets[:]: filepath=os.path.join(cifs_dir, fil) print filepath in_file = open(filepath, 'r') lins = in_file.readlines() in_file.close() ind_beg = 0 for i_l, lin in enumerate(lins): if lin.startswith('data_1000'): ind_beg = i_l break new_lins = lins[:ind_beg]
def testAReverseO(self): # the reverse should be OK! newdic = CifFile.merge_dic([self.adic,self.offdic],mergemode='overlay') jj = CifFile.ValidCifFile(datasource="dictionaries/merge_test.cif", dic = newdic)
def testAOverlay(self): newdic = CifFile.merge_dic([self.offdic,self.adic],mergemode='overlay') # print newdic.__str__() self.assertRaises(CifFile.ValidCifError,CifFile.ValidCifFile, datasource="dictionaries/merge_test.cif", dic=newdic)
def testCVOverlay(self): jj = open("merge_debug","w") newdic = CifFile.merge_dic([self.cvdica,self.cvdicb,self.cvdicc,self.cvdicd],mergemode='overlay') jj.write(newdic.__str__())
tmpfile = tempfile.mkstemp()[1] jj = open(tmpfile,"w") jj.write(input_cif.file.read()) jj.close() try: cf = CifFile.ReadCif(tmpfile,scantype="flex") os.remove(tmpfile) except CifFile.StarFile.StarError: os.remove(tmpfile) import sys,re print "<h3>File reading error</h3>" print "<p>File %s appears to have one or more syntax errors</p>" % input_cif.filename print "<p>The detailed error message is as follows:</p><p><pre>" etype,eval,trace = sys.exc_info() unsafe_str = str(eval) unsafe_str = unsafe_str.replace("&","&") unsafe_str = unsafe_str.replace("<","<") safe_str = unsafe_str.replace(">",">") print safe_str print "</pre>" except: os.remove(tmpfile) print "Unspecified error reading file %s. This is most likely a CIF syntax error." # Now do the validation... else: diclist = map(lambda a:os.path.join(dic_directory,a),input_dics) merged_dics = CifFile.merge_dic(diclist) validate_cif.output_header(True,filename,input_dics) print CifFile.validate_report(CifFile.validate(cf,dic= merged_dics,isdic=is_dic),use_html=True) validate_cif.output_footer(True)
import time import CifFile import profile import pstats testfiles = ['1YGG.cif','C13H22O3.cif','../dictionaries/cif_core.dic', '../dictionaries/mmcif_pdbx.dic'] #testfiles = [] cached_mmdic = CifFile.CifDic('../dictionaries/mmcif_pdbx.dic') cached_core = CifFile.CifDic('../dictionaries/cif_core.dic') valfiles = [ ('1YGG.cif',cached_mmdic), ('C13H22O3.cif',cached_core) ] for file in testfiles: start_time = time.time() jj = CifFile.ReadCif(file,scantype='flex') # no profiling, approx usage finish_time = time.time() print "File %s: wallclock time %8.1f\n" % (file,finish_time - start_time) profile.run("jj = CifFile.ReadCif(file,scantype='flex') ","profout") p = pstats.Stats( "profout") p.strip_dirs().sort_stats("cumulative").print_stats() # try to validate for file,dic in valfiles: start_time = time.time() jj = CifFile.validate(file,dic=dic) finish_time = time.time() print "Validate file %s: %8.1f\n" % (file,finish_time - start_time)
def testdot(self): """Make sure a single dot is skipped""" res1,res2 = CifFile.get_number_with_esd(".") self.failUnless(res1==None)
def Reader(self,filename,filepointer, ParentFrame=None, **kwarg): '''Read powder data from a CIF. If multiple datasets are requested, use self.repeat and buffer caching. ''' # Define lists of data names used for holding powder diffraction data # entries of a type that are not implemented are commented out in case # we will want them later. xDataItems = ( # "x-axis" data names" ('_pd_meas_2theta_range_min', '_pd_meas_2theta_range_max', '_pd_meas_2theta_range_inc'), ('_pd_proc_2theta_range_min', '_pd_proc_2theta_range_max', '_pd_proc_2theta_range_inc'), '_pd_meas_2theta_scan', '_pd_meas_time_of_flight', '_pd_proc_2theta_corrected', #'_pd_proc_d_spacing', #'_pd_proc_energy_incident', #'_pd_proc_energy_detection', #'_pd_proc_recip_len_q', #'_pd_proc_wavelength', ) intDataItems = ( # intensity axis data names '_pd_meas_counts_total', #'_pd_meas_counts_background', #'_pd_meas_counts_container', '_pd_meas_intensity_total', #'_pd_meas_intensity_background', #'_pd_meas_intensity_container', '_pd_proc_intensity_net', '_pd_proc_intensity_total', #'_pd_proc_intensity_bkg_calc', #'_pd_proc_intensity_bkg_fix', '_pd_calc_intensity_net', # allow computed patterns as input data? '_pd_calc_intensity_total', ) ESDDataItems = ( # items that can be used to compute uncertainties for obs data '_pd_proc_ls_weight', '_pd_meas_counts_total' ) ModDataItems = ( # items that modify the use of the data '_pd_meas_step_count_time', '_pd_meas_counts_monitor', '_pd_meas_intensity_monitor', '_pd_proc_intensity_norm', '_pd_proc_intensity_incident', ) rdbuffer = kwarg.get('buffer') cf = None choicelist = None selections = None # reload previously saved values if self.repeat and rdbuffer is not None: cf = rdbuffer.get('lastcif') choicelist = rdbuffer.get('choicelist') print 'debug: Reuse previously parsed CIF' selections = rdbuffer.get('selections') if cf is None: self.ShowBusy() # this can take a while print "Starting parse of CIF file" try: cf = G2IO.ReadCIF(filename) except Exception as detail: self.errors = "Parse or reading of file failed in pyCifRW; check syntax of file in enCIFer or CheckCIF" return False finally: self.DoneBusy() print "CIF file parsed" # scan all blocks for sets of data if choicelist is None: try: choicelist = [] for blk in cf.keys(): blkkeys = [k.lower() for k in cf[blk].keys()] # save a list of the data items, since we will use it often # scan through block for x items xldict = {} for x in xDataItems: if type(x) is tuple: # check for the presence of all three items that define a range of data if not all([i in blkkeys for i in x]): continue try: items = [float(cf[blk][xi]) for xi in x] l = 1 + int(0.5 + (items[1]-items[0])/items[2]) except: continue else: if x not in blkkeys: continue l = len(cf[blk][x]) if xldict.get(l) is None: xldict[l] = [x] else: xldict[l].append(x) # now look for matching intensity items yldict = {} suldict = {} for y in intDataItems: if y in blkkeys: l = len(cf[blk][y]) if yldict.get(l) is None: yldict[l] = [y] else: yldict[l].append(y) # now check if the first item has an uncertainty if cif.get_number_with_esd(cf[blk][y][0])[1] is None: continue if suldict.get(l) is None: suldict[l] = [y] else: suldict[l].append(y) for y in ESDDataItems: if y in blkkeys: l = len(cf[blk][y]) if suldict.get(l) is None: suldict[l] = [y] else: suldict[l].append(y) modldict = {} for y in ModDataItems: if y in blkkeys: l = len(cf[blk][y]) if modldict.get(l) is None: modldict[l] = [y] else: modldict[l].append(y) for l in xldict: if yldict.get(l) is None: continue choicelist.append([blk,l,xldict[l],yldict[l],suldict.get(l,[]),modldict.get(l,[])]) #print blk,l,xldict[l],yldict[l],suldict.get(l,[]),modldict.get(l,[]) except Exception as detail: self.errors = "Error scanning blocks" self.errors += "\n Read exception: "+str(detail) print self.formatName+' read error:'+str(detail) # for testing import traceback traceback.print_exc(file=sys.stdout) #self.errors += "\n Traceback info:\n"+str(traceback.format_exc()) return False print "CIF file scanned for blocks with data" if not choicelist: selblk = None # no block to choose self.errors = "No powder diffraction blocks found" return False elif len(choicelist) == 1: # only one choice selblk = 0 elif self.repeat and selections is not None: # we were called to repeat the read print 'debug: repeat #',self.repeatcount,'selection',selections[self.repeatcount] selblk = selections[self.repeatcount] self.repeatcount += 1 if self.repeatcount >= len(selections): self.repeat = False else: # choose from options # compile a list of choices for the user choices = [] for blk,l,x,y,su,mod in choicelist: sx = x[0] if len(x) > 1: sx += '...' sy = y[0] if len(y) > 1: sy += '...' choices.append( 'Block '+str(blk)+', '+str(l)+' points. X='+sx+' & Y='+sy ) selections = self.MultipleBlockSelector( choices, ParentFrame=ParentFrame, title='Select dataset(s) to read from the list below', size=(600,100), header='Dataset Selector') if len(selections) == 0: self.errors = "Abort: block not selected" return False selblk = selections[0] # select first in list if len(selections) > 1: # prepare to loop through again self.repeat = True self.repeatcount = 1 if rdbuffer is not None: rdbuffer['selections'] = selections rdbuffer['lastcif'] = cf # save the parsed cif for the next loop rdbuffer['choicelist'] = choicelist # save the parsed choices for the future # got a selection, now read it # do we need to ask which fields to read? blk,l,xch,ych,such,modch = choicelist[selblk] xi,yi,sui,modi = 0,0,0,0 if len(xch) > 1 or len(ych) > 1 or len(such) > 1 or len(modch) > 0: choices = [] chlbls = [] chlbls.append('Select the scanned data item') xchlst = [] for x in xch: if type(x) is tuple: xchlst.append(x[0]) else: xchlst.append(x) choices.append(xchlst) chlbls.append('Select the intensity data item') choices.append(ych) chlbls.append('Select the data item to be used for weighting') choices.append(such) chlbls.append('Divide intensities by data item') choices.append(['none']+modch) res = self.MultipleChoicesDialog(choices,chlbls) if not res: self.errors = "Abort: data items not selected" return False xi,yi,sui,modi = res # now read in the values try: self.ShowBusy() # this can also take a while # x-values xcf = xch[xi] if type(xcf) is tuple: vals = [float(cf[blk][xi]) for xi in xcf] x = np.array( [(i*vals[2] + vals[0]) for i in range(1 + int(0.5 + (vals[1]-vals[0])/vals[2]))] ) else: vl = [] for val in cf[blk].get(xcf,'?'): v,e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(np.NaN) else: vl.append(v) x = np.array(vl) # y-values ycf = ych[yi] vl = [] for val in cf[blk].get(ycf,'?'): v,e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(np.NaN) else: vl.append(v) y = np.array(vl) # weights if sui == -1: # no weights vl = np.zeros(len(x)) + 1. else: sucf = such[sui] if sucf == '_pd_proc_ls_weight': for val in cf[blk].get(sucf,'?'): v,e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(0.) else: vl.append(v) elif sucf == '_pd_meas_counts_total': for val in cf[blk].get(sucf,'?'): v,e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(0.) elif v <= 0: vl.append(1.) else: vl.append(1./v) else: for val in cf[blk].get(sucf,'?'): v,e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(0.0) elif v <= 0: vl.append(0.0) else: vl.append(1./(v*v)) w = np.array(vl) # intensity modification factor if modi >= 1: ycf = modch[modi-1] vl = [] for val in cf[blk].get(ycf,'?'): v,e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(np.NaN) else: vl.append(v) y /= np.array(vl) w /= np.array(vl) N = len(x) except Exception as detail: self.errors = "Error reading from selected block" self.errors += "\n Read exception: "+str(detail) print self.formatName+' read error:'+str(detail) # for testing import traceback traceback.print_exc(file=sys.stdout) #self.errors += "\n Traceback info:\n"+str(traceback.format_exc()) return False finally: self.DoneBusy() print "CIF file, read from selected block" self.errors = "Error while storing read values" self.powderdata = [ np.array(x), # x-axis values np.array(y), # powder pattern intensities np.array(w), # 1/sig(intensity)^2 values (weights) np.zeros(N), # calc. intensities (zero) np.zeros(N), # calc. background (zero) np.zeros(N), # obs-calc profiles ] self.powderentry[0] = filename #self.powderentry[1] = pos # bank offset (N/A here) self.powderentry[2] = 1 # xye file only has one bank self.idstring = os.path.basename(filename) + ': ' + blk if cf[blk].get('_diffrn_radiation_probe'): if cf[blk]['_diffrn_radiation_probe'] == 'neutron': self.instdict['type'] = 'PNC' #if cf[blk].get('_pd_meas_time_of_flight'): self.instdict['type'] = 'PNT' # not supported yet else: self.instdict['type'] = 'PXC' if cf[blk].get('_diffrn_radiation_wavelength'): val = cf[blk]['_diffrn_radiation_wavelength'] wl = [] if type(val) is list: for v in val: w,e = cif.get_number_with_esd(v) if w: wl.append(w) else: w,e = cif.get_number_with_esd(val) if w: wl.append(w) if wl: self.instdict['wave'] = wl if cf[blk].get('_diffrn_ambient_temperature'): val = cf[blk]['_diffrn_ambient_temperature'] w,e = cif.get_number_with_esd(val) if w: self.Sample['Temperature'] = w return True