Esempio n. 1
0
def main():
    '''Launch Frameviewer after parsing command line arguments'''
    parser = py_utils.MyArgparser(
        description='Utility for viewing frames of an emc file (list)')
    parser.add_argument(
        '-M',
        '--mask',
        help='Whether to zero out masked pixels (default False)',
        action='store_true',
        default=False)
    parser.add_argument('-P',
                        '--powder',
                        help='Show powder sum of all frames',
                        action='store_true',
                        default=False)
    parser.add_argument(
        '-C',
        '--compare',
        help='Compare with predicted intensities (needs data/quat.dat)',
        action='store_true',
        default=False)
    args = parser.special_parse_args()

    app = QtWidgets.QApplication(sys.argv)
    Frameviewer(args.config_file,
                mask=args.mask,
                do_powder=args.powder,
                do_compare=args.compare)
    sys.exit(app.exec_())
Esempio n. 2
0
def main():
    '''Generate virutal powder pattern by adding up all frames'''
    # Read detector and photons file from config
    parser = py_utils.MyArgparser(
        description="Generate virtual powder pattern")
    args = parser.special_parse_args()

    try:
        photons_list = [
            read_config.get_filename(args.config_file, 'emc',
                                     "in_photons_file")
        ]
    except read_config.configparser.NoOptionError:
        with open(
                read_config.get_param(args.config_file, 'emc',
                                      "in_photons_list"), 'r') as fptr:
            photons_list = [line.rstrip() for line in fptr.readlines()]

    try:
        det_fname = read_config.get_filename(args.config_file, 'emc',
                                             'in_detector_file')
    except:
        print(
            'emc:::in_detector_file not found. Note that single detector file needed for powder sum'
        )
        return
    det = detector.Detector(det_fname)

    powder = np.zeros(det.x.shape)
    assem_powder = np.zeros(det.frame_shape)

    # For each emc file, read data and add to powder
    for photons_file in photons_list:
        # Read photon data
        with open(photons_file, 'rb') as fptr:
            num_data = np.fromfile(fptr, dtype='i4', count=1)[0]
            num_pix = np.fromfile(fptr, dtype='i4', count=1)[0]
            print(photons_file + ': num_data = %d, num_pix = %d' %
                  (num_data, num_pix))
            if num_pix != len(powder):
                print('Detector and photons file dont agree on num_pix')
            fptr.seek(1024, 0)
            ones = np.fromfile(fptr, dtype='i4', count=num_data)
            multi = np.fromfile(fptr, dtype='i4', count=num_data)
            place_ones = np.fromfile(fptr, dtype='i4', count=ones.sum())
            place_multi = np.fromfile(fptr, dtype='i4', count=multi.sum())
            count_multi = np.fromfile(fptr, dtype='i4', count=multi.sum())

        # Place photons in powder array
        np.add.at(powder, place_ones, 1)
        np.add.at(powder, place_multi, count_multi)
        np.add.at(assem_powder, (det.x[place_ones], det.y[place_ones]), 1)
        np.add.at(assem_powder, (det.x[place_multi], det.y[place_multi]),
                  count_multi)

    # Write float64 array to file
    powder.tofile('data/powder.bin')
    assem_powder.tofile('data/powder_%d_%d.bin' % det.frame_shape)
Esempio n. 3
0
def main():
    '''Parses command line arguments and launches Classifier GUI'''
    parser = py_utils.MyArgparser(description='Data classifier')
    parser.add_argument(
        '-M',
        '--mask',
        help='Whether to zero out masked pixels (default False)',
        action='store_true',
        default=False)
    args = parser.special_parse_args()

    app = QtWidgets.QApplication(sys.argv)
    Classifier(args.config_file, mask=args.mask)
    sys.exit(app.exec_())
Esempio n. 4
0
def main():
    '''Parses command line arguments to create 3D intensity file using config file parameters'''
    logging.basicConfig(filename="recon.log",
                        level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    parser = py_utils.MyArgparser(description="make intensities")
    args = parser.special_parse_args()

    den_file = os.path.join(
        args.main_dir,
        read_config.get_filename(args.config_file, 'make_intensities',
                                 'in_density_file'))
    intens_file = os.path.join(
        args.main_dir,
        read_config.get_filename(args.config_file, 'make_intensities',
                                 'out_intensity_file'))
    try:
        num_threads = int(
            read_config.get_param(args.config_file, 'make_intensities',
                                  'num_threads'))
    except read_config.configparser.NoOptionError:
        num_threads = 4
    if args.yes:
        to_write = True
    else:
        to_write = py_utils.check_to_overwrite(intens_file)
    logging.info("\n\nStarting.... make_intensities")
    logging.info(' '.join(sys.argv))

    if to_write:
        timer = py_utils.MyTimer()
        pm = read_config.get_detector_config(args.config_file, show=args.vb)  #pylint: disable=C0103
        q_pm = read_config.compute_q_params(pm['detd'],
                                            pm['dets_x'],
                                            pm['dets_y'],
                                            pm['pixsize'],
                                            pm['wavelength'],
                                            pm['ewald_rad'],
                                            show=args.vb)
        timer.reset_and_report(
            "Reading experiment parameters") if args.vb else timer.reset()

        fov_len = 2 * int(np.ceil(
            q_pm['fov_in_A'] / q_pm['half_p_res'] / 2.)) + 3
        logging.info('Volume size: %d', fov_len)
        den = py_utils.read_density(den_file)
        min_over = float(fov_len) / den.shape[0]
        if min_over > 12:
            if py_utils.confirm_oversampling(min_over) is False:
                sys.exit(0)
        timer.reset_and_report(
            "Reading densities") if args.vb else timer.reset()

        pad_den = np.zeros(3 * (fov_len, ))
        den_sh = den.shape
        pad_den[:den_sh[0], :den_sh[1], :den_sh[2]] = den.copy()
        if WITH_PYFFTW:
            intens = np.abs(
                np.fft.fftshift(
                    pyfftw.interfaces.numpy_fft.fftn(
                        pad_den,
                        threads=num_threads,
                        planner_effort='FFTW_ESTIMATE')))**2
        else:
            intens = np.abs(np.fft.fftshift(np.fft.fftn(pad_den)))**2
        timer.reset_and_report(
            "Computing intensities") if args.vb else timer.reset()

        py_utils.write_density(intens_file, intens, binary=True)
        timer.reset_and_report(
            "Writing intensities") if args.vb else timer.reset()

        timer.report_time_since_beginning() if args.vb else timer.reset()
    else:
        pass
Esempio n. 5
0
def main():
    """Parse command line arguments and convert file(s)"""
    logging.basicConfig(filename='recon.log',
                        level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    parser = py_utils.MyArgparser(description='condortoemc')
    parser.add_argument('condor_fname',
                        help='Condor CXI file to convert to emc format')
    parser.add_argument('-o',
                        '--output',
                        help='Output folder, read from config file by default',
                        type=str,
                        default=None)
    parser.add_argument(
        '-l',
        '--list',
        help=
        'condor_fname is list of condor CXI files rather than a single one',
        action='store_true',
        default=False)
    args = parser.special_parse_args()

    logging.info('Starting condortoemc...')
    logging.info(' '.join(sys.argv))
    if args.output is None:
        output_folder = read_config.get_filename(args.config_file, 'emc',
                                                 'output_folder')
    else:
        output_folder = args.output

    if not os.path.isfile(args.condor_fname):
        print('Data file %s not found. Exiting.' % args.condor_fname)
        logging.error('Data file %s not found. Exiting.', args.condor_fname)
        sys.exit(1)

    if args.list:
        logging.info('Reading file names in list %s', args.condor_fname)
        with open(args.condor_fname, 'r') as fptr:
            flist = [fname.rstrip() for fname in fptr.readlines()]
    else:
        flist = [args.condor_fname]

    # Read meta information from condor CXI file
    with h5py.File(flist[0], 'r') as fptr:
        dets_x = fptr['detector/nx'][0]
        dets_y = fptr['detector/ny'][0]

    fname = '%s/%s.emc' % (output_folder,
                           os.path.splitext(os.path.basename(
                               args.condor_fname))[0])
    emcwriter = writeemc.EMCWriter(fname, dets_x * dets_y)

    for fname in flist:
        fptr = h5py.File(fname, 'r')
        dset = fptr['entry_1/data_1/data']
        mask = fptr['entry_1/data_1/mask']
        num_frames = dset.shape[0]
        if not args.list:
            logging.info('Converting %d frames in %s', num_frames,
                         args.condor_fname)

        for i in range(num_frames):
            photons = dset[i] * (~(mask[i] == 512))
            photons[photons < 0] = 0
            emcwriter.write_frame(photons.flatten())
            if not args.list:
                sys.stderr.write('\rFinished %d/%d' % (i + 1, num_frames))
        fptr.close()

    if not args.list:
        sys.stderr.write('\n')
    emcwriter.finish_write()
Esempio n. 6
0
def main():
    '''Parse command line arguments and generate electron density volume with config file'''
    logging.basicConfig(filename="recon.log",
                        level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    parser = py_utils.MyArgparser(description="make electron density")
    args = parser.special_parse_args()
    logging.info("\n\nStarting.... make_densities")
    logging.info(' '.join(sys.argv))

    try:
        pdb_file = os.path.join(
            args.main_dir,
            read_config.get_filename(args.config_file, 'make_densities',
                                     'in_pdb_file'))
        pdb_code = None
    except read_config.configparser.NoOptionError:
        pdb_code = read_config.get_filename(args.config_file, 'make_densities',
                                            'pdb_code')
        pdb_file = 'aux/%s.pdb' % pdb_code.upper()
    try:
        num_threads = int(
            read_config.get_param(args.config_file, 'make_densities',
                                  'num_threads'))
    except read_config.configparser.NoOptionError:
        num_threads = 4
    aux_dir = os.path.join(
        args.main_dir,
        read_config.get_filename(args.config_file, 'make_densities',
                                 'scatt_dir'))
    den_file = os.path.join(
        args.main_dir,
        read_config.get_filename(args.config_file, 'make_densities',
                                 'out_density_file'))
    if args.yes:
        to_write = True
    else:
        to_write = py_utils.check_to_overwrite(den_file)

    if to_write:
        timer = py_utils.MyTimer()
        pm = read_config.get_detector_config(args.config_file, show=args.vb)  # pylint: disable=C0103
        q_pm = read_config.compute_q_params(pm['detd'],
                                            pm['dets_x'],
                                            pm['dets_y'],
                                            pm['pixsize'],
                                            pm['wavelength'],
                                            pm['ewald_rad'],
                                            show=args.vb)
        timer.reset_and_report(
            "Reading experiment parameters") if args.vb else timer.reset()

        if pdb_code is not None:
            process_pdb.fetch_pdb(pdb_code)
        all_atoms = process_pdb.process(pdb_file, aux_dir, pm['wavelength'])
        timer.reset_and_report("Reading PDB") if args.vb else timer.reset()

        den = process_pdb.atoms_to_density_map(all_atoms, q_pm['half_p_res'])
        lp_den = process_pdb.low_pass_filter_density_map(den,
                                                         threads=num_threads)
        timer.reset_and_report(
            "Creating density map") if args.vb else timer.reset()

        py_utils.write_density(den_file, lp_den, binary=True)
        timer.reset_and_report(
            "Writing densities to file") if args.vb else timer.reset()

        timer.report_time_since_beginning() if args.vb else timer.reset()
Esempio n. 7
0
def main():
    """Parse command line arguments and convert file"""
    logging.basicConfig(filename='recon.log',
                        level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    parser = py_utils.MyArgparser(description='CONDOR to det')
    parser.add_argument('filename', help='CONDOR file', type=str)
    parser.add_argument(
        '-C',
        '--corners',
        help='Set corner pixels to be irrelevant. Default = False',
        action='store_true',
        default=False)
    args = parser.special_parse_args()

    logging.info('Starting condortodet...')
    logging.info(' '.join(sys.argv))
    pm = read_config.get_detector_config(args.config_file, show=args.vb)  # pylint: disable=invalid-name
    output_folder = read_config.get_filename(args.config_file, 'emc',
                                             'output_folder')
    det = detector.Detector()

    with h5py.File(args.filename, 'r') as fptr:
        condor_dets_x = fptr['detector']['nx'][0]
        condor_dets_y = fptr['detector']['ny'][0]
        condor_detc_x = fptr['detector']['cx'][0]
        condor_detc_y = fptr['detector']['cy'][0]
        condor_detd = fptr['detector']['distance'][0]  # in units of m
        condor_pixsize = fptr['detector']['pixel_size'][0]  # in units of m
        condor_wavelength = fptr['source/wavelength'][0]  # in units of m
        condor_mask = fptr['entry_1/data_1/mask'][0]
    _test_match(condor_dets_x, pm['dets_x'])
    _test_match(condor_dets_y, pm['dets_y'])
    _test_match(condor_detc_x, pm['detc_x'])
    _test_match(condor_detc_y, pm['detc_y'])
    _test_match(condor_detd * 1e3, pm['detd'])
    _test_match(condor_pixsize * 1e3, pm['pixsize'])
    _test_match(condor_wavelength * 1e10, pm['wavelength'])

    # CONDOR CALCULATION OF QMAP AND SOLID ANGLE
    # ---------------------------------------------
    cdet = condor.Detector(condor_detd,
                           condor_pixsize,
                           cx=condor_detc_x,
                           cy=condor_detc_y,
                           nx=condor_dets_x,
                           ny=condor_dets_y)
    condor_X, condor_Y = cdet.generate_xypix(
        cx=cdet.get_cx_mean_value(),  # pylint: disable=invalid-name
        cy=cdet.get_cy_mean_value())
    condor_R = np.sqrt(condor_X**2 + condor_Y**2)  # pylint: disable=invalid-name
    condor_qmap = cdet.generate_qmap(condor_wavelength,
                                     cx=cdet.get_cx_mean_value(),
                                     cy=cdet.get_cy_mean_value(),
                                     order='xyz')
    condor_qx = condor_qmap[:, :, 0].flatten()
    condor_qy = condor_qmap[:, :, 1].flatten()
    condor_qz = condor_qmap[:, :, 2].flatten()
    condor_polar = cdet.calculate_polarization_factors(
        cx=cdet.get_cx_mean_value(),
        cy=cdet.get_cy_mean_value(),
        polarization='unpolarized').flatten()
    det.corr = condor_polar * cdet.get_all_pixel_solid_angles(
        cx=cdet.get_cx_mean_value(), cy=cdet.get_cy_mean_value()).flatten()

    # DRAGONFLY CALCULATION OF QMAP AND SOLID ANGLE
    # ---------------------------------------------
    q_pm = read_config.compute_q_params(pm['detd'],
                                        pm['dets_x'],
                                        pm['dets_y'],
                                        pm['pixsize'],
                                        pm['wavelength'],
                                        pm['ewald_rad'],
                                        show=args.vb)
    qscaling = 1. / pm['wavelength'] / q_pm['q_sep']

    # RESCALE CONDOR Qs to match dragonfly convention
    # -----------------------------------------------
    det.qx = condor_qx / (2 * np.pi) * condor_wavelength * qscaling
    det.qy = condor_qy / (2 * np.pi) * condor_wavelength * qscaling
    det.qz = condor_qz / (2 * np.pi) * condor_wavelength * qscaling

    if pm['mask_fname'] is None:
        det.raw_mask = np.zeros(condor_mask.shape)
        det.raw_mask[~(condor_mask == 0)] = 2
        if args.corners:
            rmax = min(condor_X.max(), np.abs(condor_X.min()), condor_Y.max(),
                       np.abs(condor_Y.min()))
            det.raw_mask[condor_R > rmax] = 1
        det.raw_mask = det.raw_mask.flatten()
    else:
        det.raw_mask = np.fromfile(pm['mask_fname'], '=u1')

    det.detd = pm['detd'] / pm['pixsize']
    det.ewald_rad = pm['ewald_rad']
    det_file = output_folder + '/' + 'condor.dat'
    sys.stderr.write('Writing detector file to %s\n' % det_file)
    det.write(det_file)
Esempio n. 8
0
def main():
    """Parse command line arguments and generate file"""
    logging.basicConfig(filename='recon.log',
                        level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    parser = py_utils.MyArgparser(description='PSANA to det')
    parser.add_argument('exp_string', help='PSANA experiment string')
    parser.add_argument(
        'det_name', help='PSANA Detector name (either source string or alias)')
    parser.add_argument(
        '-C',
        '--corners',
        help='Set corner pixels to be irrelevant. Default = False',
        action='store_true',
        default=False)
    args = parser.special_parse_args()

    logging.info('Starting psanatodet...')
    logging.info(' '.join(sys.argv))
    pm = read_config.get_detector_config(args.config_file, show=args.vb)  # pylint: disable=invalid-name
    q_pm = read_config.compute_q_params(pm['detd'],
                                        pm['dets_x'],
                                        pm['dets_y'],
                                        pm['pixsize'],
                                        pm['wavelength'],
                                        pm['ewald_rad'],
                                        show=args.vb)
    output_folder = read_config.get_filename(args.config_file, 'emc',
                                             'output_folder')

    dsource = psana.DataSource(args.exp_string + ':idx')
    run = dsource.runs().next()
    times = run.times()
    evt = run.event(times[0])
    psana_det = psana.Detector(args.det_name)
    cx = psana_det.coords_x(evt).flatten() * 1.e-3  # pylint: disable=invalid-name
    cy = psana_det.coords_y(evt).flatten() * 1.e-3  # pylint: disable=invalid-name

    detd = pm['detd']  # in mm
    qscaling = 1. / pm['wavelength'] / q_pm['q_sep']
    rad = np.sqrt(cx * cx + cy * cy)
    norm = np.sqrt(cx * cx + cy * cy + detd * detd)
    polar = read_config.compute_polarization(pm['polarization'], cx, cy, norm)

    det = detector.Detector()
    det.detd = detd / pm['pixsize']
    det.ewald_rad = pm['ewald_rad']
    det.qx = cx * qscaling / norm
    det.qy = cy * qscaling / norm
    det.qz = qscaling * (detd / norm - 1.)
    det.corr = pm['detd'] * pm['pixsize'] * pm['pixsize'] / np.power(norm, 3.0)
    det.corr *= polar

    if pm['mask_fname'] is None:
        det.raw_mask = psana_det.mask(evt,
                                      status=True,
                                      calib=True,
                                      edges=True,
                                      central=True,
                                      unbondnbrs8=True).flatten()
        det.raw_mask = 2 * (1 - det.raw_mask)
        if args.corners:
            rmax = min(cx.max(), np.abs(cx.min()), cy.max(), np.abs(cy.min()))
            det.raw_mask[(rad > rmax) & (det.raw_mask == 0)] = 1
    else:
        det.raw_mask = np.fromfile(pm['mask_fname'], '=u1')

    det_file = output_folder + '/det_' + args.exp_string.split(':')[0][4:]
    try:
        import h5py
        det_file += '.h5'
    except ImportError:
        det_file += '.dat'
    sys.stderr.write('Writing detector file to %s\n' % det_file)
    det.write(det_file)
Esempio n. 9
0
def main():
    """Parse command line arguments and convert file"""
    logging.basicConfig(filename='recon.log',
                        level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    parser = py_utils.MyArgparser(description='cheetahtodet')
    parser.add_argument('h5_name',
                        help='HDF5 file to convert to detector format')
    parser.add_argument(
        '-M',
        '--mask',
        help=
        'Path to detector style mask (0:good, 1:no_orient, 2:bad) in h5 file')
    parser.add_argument('--mask_dset',
                        help='Data set in mask file. Default: /data/data',
                        default='data/data')
    parser.add_argument(
        '--dragonfly_mask',
        help='Whether mask has Dragonfly style values or not. (Default: false)',
        default=False,
        action='store_true')
    args = parser.special_parse_args()

    logging.info('Starting cheetahtodet...')
    logging.info(' '.join(sys.argv))
    pm = read_config.get_detector_config(args.config_file, show=args.vb)  # pylint: disable=invalid-name
    q_pm = read_config.compute_q_params(pm['detd'],
                                        pm['dets_x'],
                                        pm['dets_y'],
                                        pm['pixsize'],
                                        pm['wavelength'],
                                        pm['ewald_rad'],
                                        show=args.vb)
    output_folder = read_config.get_filename(args.config_file, 'emc',
                                             'output_folder')

    # Cheetah geometry files have coordinates in m
    with h5py.File(args.h5_name, 'r') as fptr:
        x = fptr['x'][:].flatten() * 1.e3
        y = fptr['y'][:].flatten() * 1.e3
        z = fptr['z'][:].flatten() * 1.e3 + pm['detd']

    det = detector.Detector()
    norm = np.sqrt(x * x + y * y + z * z)
    qscaling = 1. / pm['wavelength'] / q_pm['q_sep']
    det.qx = x * qscaling / norm
    det.qy = y * qscaling / norm
    det.qz = qscaling * (z / norm - 1.)
    det.corr = pm['detd'] * (pm['pixsize'] * pm['pixsize']) / np.power(
        norm, 3.0)
    det.corr *= read_config.compute_polarization(pm['polarization'], x, y,
                                                 norm)
    if args.mask is None:
        radius = np.sqrt(x * x + y * y)
        rmax = min(np.abs(x.max()), np.abs(x.min()), np.abs(y.max()),
                   np.abs(y.min()))
        det.raw_mask = np.zeros(det.corr.shape, dtype='u1')
        det.raw_mask[radius > rmax] = 1
    else:
        with h5py.File(args.mask, 'r') as fptr:
            det.raw_mask = fptr[args.mask_dset][:].astype('u1').flatten()
        if not args.dragonfly_mask:
            det.raw_mask = 2 - 2 * det.raw_mask

    det.detd = pm['detd'] / pm['pixsize']
    det.ewald_rad = pm['ewald_rad']
    det_file = output_folder + '/' + os.path.splitext(
        os.path.basename(args.h5_name))[0]
    try:
        import h5py
        det_file += '.h5'
    except ImportError:
        det_file += '.dat'
    logging.info('Writing detector file to %s', det_file)
    sys.stderr.write('Writing detector file to %s\n' % det_file)
    det.write(det_file)
Esempio n. 10
0
def main():
    """Parse command line arguments and convert file(s)"""
    logging.basicConfig(filename='recon.log', level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    parser = py_utils.MyArgparser(description='h5toemc')
    parser.add_argument('h5_name', help='HDF5 file to convert to emc format')
    parser.add_argument('-d', '--dset_name',
                        help='Name of HDF5 dataset containing photon data', default=None)
    parser.add_argument('-b', '--binning',
                        help='Downsampling binning factor (must divide array size)',
                        default=None, type=int)
    parser.add_argument('-f', '--fraction',
                        help='Random fraction of photons to be converted',
                        default=1., type=float)
    parser.add_argument('-p', '--partition',
                        help='Number of frames to partition each original frame',
                        default=1, type=int)
    parser.add_argument('-s', '--sel_file',
                        help='Path to text file containing indices of frames. Default: Do all',
                        default=None)
    parser.add_argument('-S', '--sel_dset',
                        help='Same as --sel_file, but pointing to the name of an HDF5 dataset',
                        default=None)
    parser.add_argument('-l', '--list',
                        help='h5_name is list of h5 files rather than a single one',
                        action='store_true', default=False)
    parser.add_argument('-o', '--out_fname',
                        help='Output filename if different from calculated name', default=None)
    args = parser.special_parse_args()

    logging.info('Starting h5toemc....')
    logging.info(' '.join(sys.argv))
    pm = read_config.get_detector_config(args.config_file, show=args.vb) # pylint: disable=invalid-name
    output_folder = read_config.get_filename(args.config_file, 'emc', 'output_folder')
    curr_num_data = 0

    if not os.path.isfile(args.h5_name):
        print('Data file %s not found. Exiting.' % args.h5_name)
        logging.error('Data file %s not found. Exiting.', args.h5_name)
        sys.exit()

    if args.fraction < 1. and args.partition > 1:
        print('Cannot set both fraction < 1 and partition > 1')
        return

    if args.list:
        logging.info('Reading file names in list %s', args.h5_name)
        with open(args.h5_name, 'r') as fptr:
            flist = [os.path.realpath(fname.rstrip()) for fname in fptr.readlines()]
    else:
        flist = [args.h5_name]

    if args.out_fname is None:
        fname = '%s/%s.emc' % (output_folder, os.path.splitext(os.path.basename(args.h5_name))[0])
        emcwriter = writeemc.EMCWriter(fname, pm['dets_x']*pm['dets_y'])
    else:
        emcwriter = writeemc.EMCWriter(args.out_fname, pm['dets_x']*pm['dets_y'])

    for fnum, fname in enumerate(flist):
        fptr = h5py.File(fname, 'r')
        dset = get_dset(fptr, args)
        if dset is None:
            continue

        ind, num_frames = get_indices(fptr, dset, args, curr_num_data)

        curr_num_data += num_frames
        if not args.list:
            logging.info('Converting %d/%d frames in %s', num_frames, dset.shape[0], args.h5_name)

        for i in range(num_frames):
            if len(dset.shape) >= 3:
                photons = dset[ind[i]]
            else:
                photons = dset[:]
            photons[photons < 0] = 0
            if args.binning is not None:
                photons = bin_image(photons, args.binning)
            emcwriter.write_frame(photons.ravel(), fraction=args.fraction, partition=args.partition)
            if not args.list:
                sys.stderr.write('\rFinished %d/%d' % (i+1, num_frames))

        fptr.close()
        if not args.list:
            sys.stderr.write('\n')
        sys.stderr.write('\rProcessed %s %d/%d' % (fname, fnum, len(flist)))

    sys.stderr.write('\n')
    emcwriter.finish_write()
Esempio n. 11
0
def main():
    '''Parses command line arguments and config file to generate detector file'''
    logging.basicConfig(filename="recon.log",
                        level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    parser = py_utils.MyArgparser(description="make detector")
    args = parser.special_parse_args()

    det_file = os.path.join(
        args.main_dir,
        read_config.get_filename(args.config_file, 'make_detector',
                                 'out_detector_file'))
    if args.yes:
        to_write = True
    else:
        to_write = py_utils.check_to_overwrite(det_file)
    logging.info("\n\nStarting make_detector....")
    logging.info(' '.join(sys.argv))

    if to_write:
        timer = py_utils.MyTimer()
        pm = read_config.get_detector_config(args.config_file, show=args.vb)  # pylint: disable=C0103
        q_pm = read_config.compute_q_params(pm['detd'],
                                            pm['dets_x'],
                                            pm['dets_y'],
                                            pm['pixsize'],
                                            pm['wavelength'],
                                            pm['ewald_rad'],
                                            show=args.vb)
        timer.reset_and_report(
            "Reading experiment parameters") if args.vb else timer.reset()

        qscaling = 1. / pm['wavelength'] / q_pm['q_sep']
        (x, y) = np.mgrid[0:pm['dets_x'], 0:pm['dets_y']]
        (x, y) = (x.flatten() - pm['detc_x'], y.flatten() - pm['detc_y'])
        norm = np.sqrt(x**2 + y**2 + (pm['detd'] / pm['pixsize'])**2)
        polar = read_config.compute_polarization(pm['polarization'], x, y,
                                                 norm)
        (qx, qy, qz) = (
            x * qscaling / norm,  # pylint: disable=C0103
            y * qscaling / norm,
            qscaling * (pm['detd'] / pm['pixsize'] / norm - 1.))
        logging.info('%15s:%10.4f', 'qmax',
                     np.sqrt(qx * qx + qy * qy + qz * qz).max())
        solid_angle = pm['detd'] / pm['pixsize'] / norm**3
        solid_angle = polar * solid_angle
        rad = np.sqrt(x * x + y * y)
        if pm['mask_fname'] is None:
            mask = np.zeros(solid_angle.shape, dtype='u1')
            mask[rad > min(pm['detc_x'], pm['detc_y'])] = 1
            mask[rad < pm['stoprad']] = 2
        else:
            mask = np.fromfile(pm['mask_fname'], '=u1')
            mask[(rad > min(pm['detc_x'], pm['detc_y'])) & (mask == 0)] = 1
        timer.reset_and_report(
            "Creating detector") if args.vb else timer.reset()

        with open(det_file, "w") as fptr:
            #fptr.write(str(pm['dets_x']*pm['dets_y']) + "\n")
            fptr.write("%d %.6f %.6f\n" %
                       (pm['dets_x'] * pm['dets_y'],
                        pm['detd'] / pm['pixsize'], pm['ewald_rad']))
            for par0, par1, par2, par3, par4 in zip(qx, qy, qz, solid_angle,
                                                    mask):
                txt = "%21.15e %21.15e %21.15e %21.15e %d\n" % (
                    par0, par1, par2, par3, par4)
                fptr.write(txt)
        timer.reset_and_report(
            "Writing detector") if args.vb else timer.reset()

        timer.report_time_since_beginning() if args.vb else timer.reset()
    else:
        pass
Esempio n. 12
0
def main():
    '''Parses command line arguments and config file to generate detector file'''
    logging.basicConfig(filename="recon.log",
                        level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    parser = py_utils.MyArgparser(description="make detector")
    args = parser.special_parse_args()

    det_file = os.path.join(
        args.main_dir,
        read_config.get_filename(args.config_file, 'make_detector',
                                 'out_detector_file'))
    if args.yes:
        to_write = True
    else:
        to_write = py_utils.check_to_overwrite(det_file)
    logging.info("\n\nStarting make_detector....")
    logging.info(' '.join(sys.argv))

    if to_write:
        timer = py_utils.MyTimer()
        pm = read_config.get_detector_config(args.config_file, show=args.vb)  # pylint: disable=C0103
        q_pm = read_config.compute_q_params(pm['detd'],
                                            pm['dets_x'],
                                            pm['dets_y'],
                                            pm['pixsize'],
                                            pm['wavelength'],
                                            pm['ewald_rad'],
                                            show=args.vb)
        timer.reset_and_report(
            "Reading experiment parameters") if args.vb else timer.reset()

        det = detector.Detector()
        qscaling = 1. / pm['wavelength'] / q_pm['q_sep']
        (x, y) = np.mgrid[0:pm['dets_x'], 0:pm['dets_y']]
        (x, y) = (x.flatten() - pm['detc_x'], y.flatten() - pm['detc_y'])
        norm = np.sqrt(x**2 + y**2 + (pm['detd'] / pm['pixsize'])**2)
        (det.qx, det.qy, det.qz) = (
            x * qscaling / norm,  # pylint: disable=C0103
            y * qscaling / norm,
            qscaling * (pm['detd'] / pm['pixsize'] / norm - 1.))
        logging.info('%15s:%10.4f', 'qmax',
                     np.sqrt(det.qx**2 + det.qy**2 + det.qz**2).max())

        polar = read_config.compute_polarization(pm['polarization'], x, y,
                                                 norm)
        det.corr = pm['detd'] / pm['pixsize'] / norm**3
        det.corr *= polar
        rad = np.sqrt(x * x + y * y)
        if pm['mask_fname'] is None:
            det.raw_mask = np.zeros(det.corr.shape, dtype='u1')
            det.raw_mask[rad > min(pm['detc_x'], pm['detc_y'])] = 1
            det.raw_mask[rad < pm['stoprad']] = 2
        else:
            det.raw_mask = np.fromfile(pm['mask_fname'], '=u1')
            det.raw_mask[(rad > min(pm['detc_x'], pm['detc_y']))
                         & (mask == 0)] = 1
        timer.reset_and_report(
            "Creating detector") if args.vb else timer.reset()

        det.detd = pm['detd'] / pm['pixsize']
        det.ewald_rad = pm['ewald_rad']
        det.write(det_file)
        timer.reset_and_report("Writing detector to %s" %
                               det_file) if args.vb else timer.reset()

        timer.report_time_since_beginning() if args.vb else timer.reset()
    else:
        pass