from pyobjcryst import loadCrystal from pyobjcryst.molecule import Molecule from pdfstream.io import load_img, load_data NI_PONI = resource_filename('tests', 'test_data/Ni_poni_file.poni') NI_GR = resource_filename('tests', 'test_data/Ni_gr_file.gr') NI_CHI = resource_filename('tests', 'test_data/Ni_chi_file.chi') NI_FGR = resource_filename('tests', 'test_data/Ni_fgr_file.fgr') NI_IMG = resource_filename('tests', 'test_data/Ni_img_file.tiff') NI_CIF = resource_filename('tests', 'test_data/Ni_cif_file.cif') KAPTON_IMG = resource_filename('tests', 'test_data/Kapton_img_file.tiff') BLACK_IMG = resource_filename('tests', 'test_data/black_img.tiff') WHITE_IMG = resource_filename('tests', 'test_data/white_img.tiff') NI_CONFIG = PDFConfig() NI_CONFIG.readConfig(NI_GR) NI_PDFGETTER = PDFGetter(NI_CONFIG) ZRP_CIF = resource_filename('tests', 'test_data/ZrP.cif') NI_CRYSTAL = loadCrystal(NI_CIF) ZRP_CRYSTAL = loadCrystal(ZRP_CIF) NI_DIFFPY = loadStructure(NI_CIF) DB = { 'Ni_img_file': NI_IMG, 'Ni_img': load_img(NI_IMG), 'Kapton_img_file': KAPTON_IMG, 'Kapton_img': load_img(KAPTON_IMG), 'Ni_poni_file': NI_PONI, 'Ni_gr_file': NI_GR, 'Ni_chi_file': NI_CHI, 'Ni_fgr_file': NI_FGR,
def load_pdfconfig(cfg_file: str) -> PDFConfig: """Load the PDFConfig from the processed data file or configuration file.""" pdfconfig = PDFConfig() pdfconfig.readConfig(cfg_file) return pdfconfig
NI_GR_FILE = resource_filename('tests', 'test_data/Ni_gr_file.gr') NI_CHI_FILE = resource_filename('tests', 'test_data/Ni_chi_file.chi') NI_FGR_FILE = resource_filename('tests', 'test_data/Ni_fgr_file.fgr') NI_IMG_FILE = resource_filename('tests', 'test_data/Ni_img_file.tiff') MASK_FILE = resource_filename("tests", "test_data/mask_file.npy") KAPTON_IMG_FILE = resource_filename('tests', 'test_data/Kapton_img_file.tiff') BLACK_IMG_FILE = resource_filename('tests', 'test_data/black_img.tiff') WHITE_IMG_FILE = resource_filename('tests', 'test_data/white_img.tiff') NI_IMG = load_img(NI_IMG_FILE) NI_FRAMES = numpy.expand_dims(NI_IMG, 0) KAPTON_IMG = load_img(KAPTON_IMG_FILE) NI_GR = load_array(NI_GR_FILE) NI_CHI = load_array(NI_CHI_FILE) NI_FGR = load_array(NI_FGR_FILE) NI_CONFIG = PDFConfig() NI_CONFIG.readConfig(NI_GR_FILE) NI_PDFGETTER = PDFGetter(NI_CONFIG) AI = pyFAI.load(NI_PONI_FILE) MASK = numpy.load(MASK_FILE) BLACK_IMG = load_img(BLACK_IMG_FILE) WHITE_IMG = load_img(WHITE_IMG_FILE) START_DOC_FILE = resource_filename('tests', 'test_data/start.json') with Path(START_DOC_FILE).open("r") as f: START_DOC = json.load(f) DB = { 'Ni_img_file': NI_IMG_FILE, 'Ni_img': NI_IMG, 'Kapton_img_file': KAPTON_IMG_FILE, 'Kapton_img': KAPTON_IMG, 'Ni_poni_file': NI_PONI_FILE,
def run(args): FILE = args.INPUT FILE_NO_EXTENSION = FILE[:-6] SAVE_PATH = os.getcwd() print(SAVE_PATH) if args.OUTPUT: SAVE_PATH = args.OUTPUT else: print( '!!! Warning files will be saved in the current folder because no output was defined.' ) REFERENCE_SLICE_NUMBER = 100 ### Collecting data informations ### try: inputFile = h5py.File(FILE, 'r') print("File " + FILE + " loaded") except ValueError: raise ### Creating matrix from data ### rawData = np.array(inputFile['data/data']) rawTheta = np.array(inputFile['data/theta']) dataX = np.ndarray.flatten(np.array(inputFile['data/dataX'])) theta = np.sort(rawTheta) sinogramData = np.zeros(np.shape(rawData)) ### Corrections ### #Correcting unsorted 2theta acquisition argsortVal = np.argsort(rawTheta) if not np.array_equal(theta, rawTheta): sorting = 0 while sorting < np.max(argsortVal) - 1: sinogramData[sorting, :, :] = rawData[argsortVal[sorting], :, :] progression("Sorting data................ ", np.size(argsortVal) - 2, sorting) sorting = sorting + 1 print() ##Deleting lines if args.DELETE: deleted_line = np.fromstring(args.DELETE, dtype=int, sep=',') for i in range(0, len(deleted_line)): sinogramData = np.delete(sinogramData, deleted_line[i], axis=0) theta = np.delete(theta, deleted_line[i], axis=0) progression("Deleting lines.............. ", len(deleted_line), i) print() ### Removing outlier pixels from data ### if args.OUTLIERS: for i in range(0, np.size(sinogramData, 2)): sinogramData[:, :, i] = findOutlierPixels(sinogramData[:, :, i], tolerance=10, worry_about_edges=False) progression("Correcting wrong pixels..... ", np.size(sinogramData, 2), i) print() ### Subtract air from raw data ### if args.AIR: dataAir = np.genfromtxt(args.AIR, dtype=float) FILE_NO_EXTENSION = FILE_NO_EXTENSION + '_SUBAIR' for i in range(0, np.size(sinogramData, 0)): currentAir = dataAir[:, 1] * ( 0.85 * np.average(sinogramData[i, :, 50]) / dataAir[50, 1]) for j in range(0, np.size(sinogramData, 1)): sinogramData[i, j, :] = sinogramData[i, j, :] - currentAir progression("Substacting air............. ", np.size(sinogramData, 0), i) print() ### Correcting thermal/beam drifts ### if args.CORRECT: thresholdedSinogramData = np.copy(sinogramData[:, :, REFERENCE_SLICE_NUMBER]) thresholdedSinogramData[sinogramData[:, :, REFERENCE_SLICE_NUMBER] < 40] = 0 thresholdedSinogramData[sinogramData[:, :, REFERENCE_SLICE_NUMBER] >= 40] = 1 thresholdedSinogramData = binary_fill_holes(thresholdedSinogramData) thresholdedSinogramData = imageFilterBigPart(thresholdedSinogramData) CoMThresh = centerOfMass(thresholdedSinogramData * sinogramData[:, :, REFERENCE_SLICE_NUMBER], axis=1) for i in range(0, np.size(sinogramData, 2)): sinogramData[:, :, i] = fixDrift(sinogramData[:, :, i], CoMThresh) progression("Correcting drifts........... ", np.size(sinogramData, 2), i) print() ### Subtract extra pattern from raw data ### if args.EXTRA: dataExtra = np.genfromtxt(args.EXTRA, dtype=float) FILE_NO_EXTENSION = FILE_NO_EXTENSION + '_SUBPAP' for i in range(0, np.size(sinogramData, 0)): for j in range(0, np.size(sinogramData, 1)): currentExtra = dataExtra[:, 1] * (sinogramData[i, j, 100] / dataExtra[100, 1]) sinogramData[i, j, :] = sinogramData[i, j, :] - currentExtra progression("Substacting extra........... ", np.size(sinogramData, 0), i) print() ### Extract PDF signal ### sinogramDataPdf = np.copy(sinogramData) if args.PDF: cfg = PDFConfig() cfg.readConfig(args.PDF) pdfget = PDFGetter() pdfget.configure(cfg) sinogramDataPdf = np.zeros( (np.size(sinogramData, 0), np.size(sinogramData, 1), round((cfg.rmax - cfg.rmin) / cfg.rstep) + 1)) for i in range(0, np.size(sinogramDataPdf, 0)): for j in range(0, np.size(sinogramDataPdf, 1)): currentPdfDataY = sinogramData[i, j, :] pdfget.getTransformation('gr') pdfget(dataX, currentPdfDataY) pdfResults = pdfget.results pdfResults = pdfResults[8] sinogramDataPdf[i, j, :] = pdfResults[1] progression("Extracting PDF.............. ", np.size(sinogramDataPdf, 0), i) for i in range(0, np.size(sinogramDataPdf, 2)): sinogramDataPdf[:, :, i] = np.average(sinogramData[:, :, :], axis=2) * np.copy( sinogramDataPdf[:, :, i]) sinogramData = np.copy(sinogramDataPdf) FILE_NO_EXTENSION = FILE_NO_EXTENSION + '_PDF' print() ### Saving ### if (args.OVERWRITE == True or os.path.isfile(FILE_NO_EXTENSION + '_corrected.h5') == False): saveHdf5File(sinogramData, SAVE_PATH, FILE_NO_EXTENSION + '_corrected.h5', mode='stack') else: print( '!!! Warning sinogram file exists, use command -R to overwrite it') ### Reconstruction ### if args.RECONSTRUCT: reconstructedData = np.zeros( (np.size(sinogramData, 1), np.size(sinogramData, 1), np.size(sinogramData, 2))) for i in range(0, np.size(sinogramData, 2)): reconstructedData[:, :, i] = reconstruction(sinogramData[:, :, i], theta, output_size=np.size( sinogramData, 1)) progression("Reconstructing data......... ", np.size(sinogramData, 2), i) print() if args.OVERWRITE and args.RECONSTRUCT: saveHdf5File(reconstructedData, SAVE_PATH, FILE_NO_EXTENSION + '_reconstructed_stack.h5', mode='stack') else: print( '!!! Warning reconstruction file exists, use command -R to overwrite it' )