fixed crash bug. rewrite of .stream open. couldn't open large stream files.

This commit is contained in:
Beale John Henry
2025-01-25 22:35:44 +01:00
parent 8704397750
commit 6f9605d801

View File

@@ -19,85 +19,78 @@ import pandas as pd
import regex as re import regex as re
import sys import sys
def count_chunks( stream ):
# get number of chunks def scrub_cells( line ):
# example - ----- Begin chunk -----
# count them
try:
pattern = r"-----\sBegin\schunk\s-----"
chunks = re.findall( pattern, stream )
if AttributeError:
return len( chunks )
except AttributeError:
return np.nan
def scrub_cells( stream ):
# get uc values from stream file # get uc values from stream file
# example - Cell parameters 7.71784 7.78870 3.75250 nm, 90.19135 90.77553 90.19243 deg # example - Cell parameters 7.71784 7.78870 3.75250 nm, 90.19135 90.77553 90.19243 deg
# scrub clen and return - else nan pattern = r"Cell\sparameters\s(\d+\.\d+)\s(\d+\.\d+)\s(\d+\.\d+)\snm,\s(\d+\.\d+)\s(\d+\.\d+)\s(\d+\.\d+)\sdeg"
try: a = re.search( pattern, line ).group(1)
pattern = r"Cell\sparameters\s(\d+\.\d+)\s(\d+\.\d+)\s(\d+\.\d+)\snm,\s(\d+\.\d+)\s(\d+\.\d+)\s(\d+\.\d+)\sdeg" b = re.search( pattern, line ).group(2)
cell_lst = re.findall( pattern, stream ) c = re.search( pattern, line ).group(3)
xtals = len( cell_lst ) alpha = re.search( pattern, line ).group(4)
if AttributeError: beta = re.search( pattern, line ).group(5)
return cell_lst, xtals gamma = re.search( pattern, line ).group(6)
except AttributeError:
return np.nan return [ a, b, c, alpha, beta, gamma ]
def scrub_res( stream ): def scrub_res( stream ):
# get diffraction limit # get diffraction limit
# example - diffraction_resolution_limit = 4.07 nm^-1 or 2.46 A # example - diffraction_resolution_limit = 4.07 nm^-1 or 2.46 A
# scrub res_lst or return np.nan pattern = r"diffraction_resolution_limit\s=\s\d\.\d+\snm\^-1\sor\s(\d+\.\d+)\sA"
try: res = re.search( pattern, stream ).group(1)
pattern = r"diffraction_resolution_limit\s=\s\d\.\d+\snm\^-1\sor\s(\d+\.\d+)\sA" return res
res_lst = re.findall( pattern, stream )
if AttributeError:
return res_lst
except AttributeError:
return np.nan
def scrub_obs( stream ): def scrub_obs( stream ):
# get number of reflections # get number of reflections
# example - num_reflections = 308 # example - num_reflections = 308
# scrub reflections or return np.nan pattern = r"num_reflections\s=\s(\d+)"
try: obs = re.search( pattern, stream ).group(1)
pattern = r"num_reflections\s=\s(\d+)" return obs
obs_lst = re.findall( pattern, stream )
if AttributeError:
return obs_lst
except AttributeError:
return np.nan
def main( stream_pwd ): def main( stream_pwd ):
print( "reading stream file" ) chunks = 0
# open stream file xtals = 0
stream = open( stream_pwd, "r" ).read() cells = []
print( "done" ) obs_list = []
res_list = []
print( "scrubing data" ) print( "scrubing data" )
# get total number chunks # open stream file
chunks = count_chunks( stream ) with open( stream_pwd ) as stream:
for line in stream:
# count chunks
if line.startswith( "----- Begin chunk -----" ):
chunks = chunks + 1
# get list of cells # get cell
cell_lst, xtals = scrub_cells( stream ) if line.startswith( "Cell parameters" ):
cell = scrub_cells( line )
cells.append( cell )
xtals = xtals + 1
# get list of cells # get res
res_lst = scrub_res( stream ) if line.startswith( "diffraction_resolution_limit" ):
res = scrub_res( line )
res_list.append( res )
# get list of cells # get obs
obs_lst = scrub_obs( stream ) if line.startswith( "num_reflections" ):
print( "done" ) obs = scrub_obs( line )
obs_list.append( obs )
if chunks % 1000 == 0:
print( "scrubbed {0} chunks".format( chunks ), end='\r' )
# res_df # res_df
cols = [ "a", "b", "c", "alpha", "beta", "gamma" ] cols = [ "a", "b", "c", "alpha", "beta", "gamma" ]
df = pd.DataFrame( cell_lst, columns=cols ) df = pd.DataFrame( cells, columns=cols )
df[ "resolution" ] = res_lst df[ "resolution" ] = res_list
df[ "obs" ] = obs_lst df[ "obs" ] = obs_list
# convert all to floats # convert all to floats
df = df.astype(float) df = df.astype(float)