Example #1
0
def basic_imageset_from_dict(d, directory=None):
    """ Construct an ImageSet class from the dictionary."""
    # Get the filename list and create the imageset
    filenames = map(
        lambda p: load_path(p, directory=directory), map(str, d["filenames"])
    )
    imageset = ImageSetFactory.new(filenames)[0]

    # Set some external lookups
    if "mask" in d and d["mask"] is not None and d["mask"] is not "":
        path = load_path(d["mask"], directory=directory)
        with open(path) as infile:
            imageset.external_lookup.mask.filename = path
            imageset.external_lookup.mask.data = ImageBool(pickle.load(infile))
    if "gain" in d and d["gain"] is not None and d["gain"] is not "":
        path = load_path(d["gain"], directory=directory)
        with open(path) as infile:
            imageset.external_lookup.gain.filename = path
            imageset.external_lookup.gain.data = ImageDouble(pickle.load(infile))
    if "pedestal" in d and d["pedestal"] is not None and d["pedestal"] is not "":
        path = load_path(d["pedestal"], directory=directory)
        with open(path) as infile:
            imageset.external_lookup.pedestal.filename = path
            imageset.external_lookup.pedestal.data = ImageDouble(pickle.load(infile))

    # Get the existing models as dictionaries
    beam_dict = imageset.get_beam(0).to_dict()
    detector_dict = imageset.get_detector(0).to_dict()

    # Set models
    imageset.set_beam(BeamFactory.from_dict(d.get("beam"), beam_dict))
    imageset.set_detector(DetectorFactory.from_dict(d.get("detector"), detector_dict))

    # Return the imageset
    return imageset
Example #2
0
def basic_imageset_from_dict(d):
    ''' Construct an ImageSet class from the dictionary.'''
    from dxtbx.model import BeamFactory, DetectorFactory
    from dxtbx.imageset import ImageSetFactory
    from dxtbx.serialize.filename import load_path

    # Get the filename list and create the imageset
    filenames = map(lambda p: load_path(p), map(str, d['filenames']))
    imageset = ImageSetFactory.new(filenames)[0]

    # Set some external lookups
    if 'mask' in d and d['mask'] is not None:
        with open(d['mask']) as infile:
            imageset.external_lookup.mask.filename = d['mask']
            imageset.external_lookup.mask.data = pickle.load(infile)
    if 'gain' in d and d['gain'] is not None:
        with open(d['gain']) as infile:
            imageset.external_lookup.gain.filename = d['gain']
            imageset.external_lookup.gain.data = pickle.load(infile)
    if 'pedestal' in d and d['pedestal'] is not None:
        with open(d['pedestal']) as infile:
            imageset.external_lookup.pedestal.filename = d['pedestal']
            imageset.external_lookup.pedestal.data = pickle.load(infile)

    # Get the existing models as dictionaries
    beam_dict = imageset.get_beam().to_dict()
    detector_dict = imageset.get_detector().to_dict()

    # Set models
    imageset.set_beam(BeamFactory.from_dict(d.get('beam'), beam_dict))
    imageset.set_detector(
        DetectorFactory.from_dict(d.get('detector'), detector_dict))

    # Return the imageset
    return imageset
Example #3
0
def imagesweep_from_dict(d, check_format=True, directory=None):
    """Construct and image sweep from the dictionary."""
    # Get the template (required)
    template = load_path(str(d["template"]), directory=directory)

    # If the scan isn't set, find all available files
    scan_dict = d.get("scan")
    if scan_dict is None:
        image_range = None
    else:
        image_range = scan_dict.get("image_range")

    # Set the models with the exisiting models as templates
    beam = BeamFactory.from_dict(d.get("beam"))
    goniometer = GoniometerFactory.from_dict(d.get("goniometer"))
    detector = DetectorFactory.from_dict(d.get("detector"))
    scan = ScanFactory.from_dict(d.get("scan"))

    # Construct the sweep
    try:
        sweep = ImageSetFactory.from_template(
            template,
            image_range,
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
            check_format=check_format,
        )[0]
    except Exception:
        indices = range(image_range[0], image_range[1] + 1)
        sweep = ImageSetFactory.make_sweep(
            template,
            indices,
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
            check_format=check_format,
        )

    # Set some external lookups
    if "mask" in d and d["mask"] is not None and d["mask"] is not "":
        path = load_path(d["mask"], directory=directory)
        with open(path) as infile:
            sweep.external_lookup.mask.filename = path
            sweep.external_lookup.mask.data = ImageBool(pickle.load(infile))
    if "gain" in d and d["gain"] is not None and d["gain"] is not "":
        path = load_path(d["gain"], directory=directory)
        with open(path) as infile:
            sweep.external_lookup.gain.filename = path
            sweep.external_lookup.gain.data = ImageDouble(pickle.load(infile))
    if "pedestal" in d and d["pedestal"] is not None and d["pedestal"] is not "":
        path = load_path(d["pedestal"], directory=directory)
        with open(path) as infile:
            sweep.external_lookup.pedestal.filename = path
            sweep.external_lookup.pedestal.data = ImageDouble(pickle.load(infile))

    # Return the sweep
    return sweep
Example #4
0
def imagesweep_from_dict(d, check_format=True):
    '''Construct and image sweep from the dictionary.'''
    from dxtbx.imageset import ImageSetFactory
    from dxtbx.model import BeamFactory, DetectorFactory, GoniometerFactory, ScanFactory
    from dxtbx.serialize.filename import load_path

    # Get the template (required)
    template = load_path(str(d['template']))

    # If the scan isn't set, find all available files
    scan_dict = d.get('scan')
    if scan_dict is None:
        image_range = None
    else:
        image_range = scan_dict.get('image_range')

    # Set the models with the exisiting models as templates
    beam = BeamFactory.from_dict(d.get('beam'))
    goniometer = GoniometerFactory.from_dict(d.get('goniometer'))
    detector = DetectorFactory.from_dict(d.get('detector'))
    scan = ScanFactory.from_dict(d.get('scan'))

    # Construct the sweep
    try:
        sweep = ImageSetFactory.from_template(template,
                                              image_range,
                                              beam=beam,
                                              detector=detector,
                                              goniometer=goniometer,
                                              scan=scan,
                                              check_format=check_format)[0]
    except Exception:
        indices = range(image_range[0], image_range[1] + 1)
        sweep = ImageSetFactory.make_sweep(template,
                                           indices,
                                           beam=beam,
                                           detector=detector,
                                           goniometer=goniometer,
                                           scan=scan,
                                           check_format=check_format)

    # Set some external lookups
    if 'mask' in d and d['mask'] is not None and d['mask'] is not "":
        with open(d['mask']) as infile:
            sweep.external_lookup.mask.filename = d['mask']
            sweep.external_lookup.mask.data = ImageBool(pickle.load(infile))
    if 'gain' in d and d['gain'] is not None and d['gain'] is not "":
        with open(d['gain']) as infile:
            sweep.external_lookup.gain.filename = d['gain']
            sweep.external_lookup.gain.data = ImageDouble(pickle.load(infile))
    if 'pedestal' in d and d['pedestal'] is not None and d[
            'pedestal'] is not "":
        with open(d['pedestal']) as infile:
            sweep.external_lookup.pedestal.filename = d['pedestal']
            sweep.external_lookup.pedestal.data = ImageDouble(
                pickle.load(infile))

    # Return the sweep
    return sweep
Example #5
0
def test_beam():
    b1 = Beam((1, 0, 0), 2, 0.1, 0.1)
    d = b1.to_dict()
    b2 = BeamFactory.from_dict(d)
    assert d["direction"] == (1, 0, 0)
    assert d["wavelength"] == 2
    assert d["divergence"] == pytest.approx(0.1)
    assert d["sigma_divergence"] == pytest.approx(0.1)
    assert b1 == b2
    assert "s0_at_scan_points" not in d

    # Test with a template and partial dictionary
    d2 = {"direction": (0, 1, 0), "divergence": 0.2}
    b3 = BeamFactory.from_dict(d2, d)
    assert b3.get_sample_to_source_direction() == (0, 1, 0)
    assert b3.get_wavelength() == 2
    assert b3.get_divergence() == pytest.approx(0.2)
    assert b3.get_sigma_divergence() == pytest.approx(0.1)
    assert b2 != b3
Example #6
0
    def tst_beam(self):
        from dxtbx.model import Beam, BeamFactory
        b1 = Beam((1, 0, 0), 2, 0.1, 0.1)
        d = b1.to_dict()
        b2 = BeamFactory.from_dict(d)
        assert (d['direction'] == (1, 0, 0))
        assert (d['wavelength'] == 2)
        assert (abs(d['divergence'] - 0.1) <= 1e-7)
        assert (abs(d['sigma_divergence'] - 0.1) <= 1e-7)
        assert (b1 == b2)

        # Test with a template and partial dictionary
        d2 = {'direction': (0, 1, 0), 'divergence': 0.2}
        b3 = BeamFactory.from_dict(d2, d)
        assert (b3.get_direction() == (0, 1, 0))
        assert (b3.get_wavelength() == 2)
        assert (abs(b3.get_divergence() - 0.2) <= 1e-7)
        assert (abs(b3.get_sigma_divergence() - 0.1) <= 1e-7)
        assert (b2 != b3)
        print 'OK'
Example #7
0
def test_beam():
    from dxtbx.model import Beam, BeamFactory
    b1 = Beam((1, 0, 0), 2, 0.1, 0.1)
    d = b1.to_dict()
    b2 = BeamFactory.from_dict(d)
    assert d['direction'] == (1, 0, 0)
    assert d['wavelength'] == 2
    assert d['divergence'] == pytest.approx(0.1)
    assert d['sigma_divergence'] == pytest.approx(0.1)
    assert b1 == b2
    assert 's0_at_scan_points' not in d

    # Test with a template and partial dictionary
    d2 = {'direction': (0, 1, 0), 'divergence': 0.2}
    b3 = BeamFactory.from_dict(d2, d)
    assert b3.get_direction() == (0, 1, 0)
    assert b3.get_wavelength() == 2
    assert b3.get_divergence() == pytest.approx(0.2)
    assert b3.get_sigma_divergence() == pytest.approx(0.1)
    assert b2 != b3
Example #8
0
 def beam(self):
   # Does this handle the conventions ? Im always confused about where the beam is pointing, whats s0 and whats beam_vector
   beam_dict = {'direction': self.beam_vector,
                 'divergence': 0.0,  # TODO
                 'flux': self.flux,
                 'polarization_fraction': self.polarization,  #TODO
                 'polarization_normal': col(self.polar_vector).cross(col(self.beam_vector)),
                 'sigma_divergence': 0.0,  # TODO
                 'transmission': 1.0,  #TODO ?
                 'wavelength': self.wavelength_A}
   beam = BeamFactory.from_dict(beam_dict)
   return beam
Example #9
0
def import_geometry(xds_inp=None, dials_json=None):
    assert (xds_inp, dials_json).count(None) == 1

    geom_kwds = set([
        "DIRECTION_OF_DETECTOR_X-AXIS",
        "DIRECTION_OF_DETECTOR_Y-AXIS",
        "DETECTOR_DISTANCE",
        "ORGX",
        "ORGY",
        "ROTATION_AXIS",  # "X-RAY_WAVELENGTH",
        "INCIDENT_BEAM_DIRECTION",
        "SEGMENT",
        "DIRECTION_OF_SEGMENT_X-AXIS",
        "DIRECTION_OF_SEGMENT_Y-AXIS",
        "SEGMENT_DISTANCE",
        "SEGMENT_ORGX",
        "SEGMENT_ORGY"
    ])

    # FIXME in case of multi-segment detector..

    if xds_inp:
        inp = get_xdsinp_keyword(xds_inp)
        inp = filter(lambda x: x[0] in geom_kwds, inp)
        return map(lambda x: "%s= %s" % x, inp)
    elif dials_json:
        import dxtbx.imageset
        from dxtbx.serialize.load import _decode_dict
        from dxtbx.model import BeamFactory
        from dxtbx.model import DetectorFactory
        from dxtbx.model import GoniometerFactory
        from dxtbx.model import ScanFactory
        from dxtbx.serialize.xds import to_xds
        j = json.loads(open(dials_json).read(), object_hook=_decode_dict)
        # dummy
        sweep = dxtbx.imageset.ImageSetFactory.from_template(
            "####", image_range=[1, 1], check_format=False)[0]
        sweep.set_detector(DetectorFactory.from_dict(j["detector"][0]))
        sweep.set_beam(BeamFactory.from_dict(j["beam"][0]))
        sweep.set_goniometer(GoniometerFactory.from_dict(j["goniometer"][0]))
        sweep.set_scan(
            ScanFactory.make_scan(image_range=[1, 1],
                                  exposure_times=[1],
                                  oscillation=[1, 2],
                                  epochs=[0]))  # dummy
        sio = cStringIO.StringIO()
        to_xds(sweep).XDS_INP(sio)
        inp = get_xdsinp_keyword(inp_str=sio.getvalue())
        inp = filter(lambda x: x[0] in geom_kwds, inp)
        return map(lambda x: "%s= %s" % x, inp)

    return []
Example #10
0
def test_beam_with_scan_points():
    b1 = Beam((1, 0, 0), 2, 0.1, 0.1)

    s0_static = matrix.col(b1.get_s0())
    b1.set_s0_at_scan_points([s0_static] * 5)
    d = b1.to_dict()
    b2 = BeamFactory.from_dict(d)

    for s0comp in d["s0_at_scan_points"]:
        assert matrix.col(s0comp) == s0_static

    for s0comp in b2.get_s0_at_scan_points():
        assert matrix.col(s0comp) == s0_static

    assert b1 == b2
Example #11
0
def test_beam_with_scan_points():
    from dxtbx.model import Beam, BeamFactory
    b1 = Beam((1, 0, 0), 2, 0.1, 0.1)
    from scitbx import matrix
    s0_static = matrix.col(b1.get_s0())
    b1.set_s0_at_scan_points([s0_static] * 5)
    d = b1.to_dict()
    b2 = BeamFactory.from_dict(d)

    for s0comp in d['s0_at_scan_points']:
        assert matrix.col(s0comp) == s0_static

    for s0comp in b2.get_s0_at_scan_points():
        assert matrix.col(s0comp) == s0_static

    assert b1 == b2
Example #12
0
 def load_models(obj):
   try:
     beam = BeamFactory.from_dict(blist[obj['beam']])
   except Exception:
     beam = None
   try:
     dobj = dlist[obj['detector']]
     detector = DetectorFactory.from_dict(dobj)
   except Exception:
     detector = None
   try:
     gonio = GoniometerFactory.from_dict(glist[obj['goniometer']])
   except Exception:
     gonio = None
   try:
     scan = ScanFactory.from_dict(slist[obj['scan']])
   except Exception:
     scan = None
   return beam, detector, gonio, scan
 def _beam_from_dict(obj):
     ''' Get a beam from a dictionary. '''
     from dxtbx.model import BeamFactory
     return BeamFactory.from_dict(obj)
Example #14
0
def test_experiment():
    d = {
        "__id__":
        "crystal",
        "real_space_a":
        [14.963210089244596, -22.599814679318, 51.02946725220764],
        "real_space_b":
        [-19.963976860932235, -51.503385430151205, -16.955728379753463],
        "real_space_c":
        [135.29560393219694, -34.371677531924206, -54.89475471853507],
        "space_group_hall_symbol":
        " P 4",
        "A_at_scan_points": [[
            0.004481726844090139,
            -0.005980612987053365,
            0.006013325470974739,
            -0.006768741824936281,
            -0.015428970379357122,
            -0.0015280122438480544,
            0.01528745348419002,
            -0.005078101688718203,
            -0.0024394384982453095,
        ]],
    }

    crystal = CrystalFactory.from_dict(d)

    beam_d = {
        "direction": [-2.4882593300783137e-06, -0.0, 0.9999999999969044],
        "transmission": 1.0,
        "polarization_normal": [0.0, 1.0, 0.0],
        "divergence": 0.0,
        "polarization_fraction": 0.999,
        "flux": 0.0,
        "sigma_divergence": 0.0,
        "wavelength": 0.9762499999999994,
    }

    beam = BeamFactory.from_dict(beam_d)
    scan = Scan(image_range=[0, 1], oscillation=[0.0, 0.01])

    detector_dict = {
        "hierarchy": {
            "origin": [0.0, 0.0, 0.0],
            "fast_axis": [1.0, 0.0, 0.0],
            "name": "",
            "raw_image_offset": [0, 0],
            "slow_axis": [0.0, 1.0, 0.0],
            "material": "",
            "mask": [],
            "thickness": 0.0,
            "mu": 0.0,
            "gain": 1.0,
            "trusted_range": [0.0, 0.0],
            "image_size": [0, 0],
            "px_mm_strategy": {
                "type": "SimplePxMmStrategy"
            },
            "identifier": "",
            "type": "",
            "children": [{
                "panel": 0
            }],
            "pixel_size": [0.0, 0.0],
        },
        "panels": [{
            "origin":
            [-210.66631009735772, 205.7063614421482, -263.8386975038205],
            "fast_axis": [
                0.9999973940105483,
                -0.0016357501034268717,
                -0.0015925745544149894,
            ],
            "name":
            "Panel",
            "raw_image_offset": [0, 0],
            "slow_axis": [
                -0.0016426481736367285,
                -0.999989234013669,
                -0.004339765400707805,
            ],
            "material":
            "Si",
            "mask": [
                [488, 1, 494, 2527],
                [982, 1, 988, 2527],
                [1476, 1, 1482, 2527],
                [1970, 1, 1976, 2527],
                [1, 196, 2463, 212],
                [1, 408, 2463, 424],
                [1, 620, 2463, 636],
                [1, 832, 2463, 848],
                [1, 1044, 2463, 1060],
                [1, 1256, 2463, 1272],
                [1, 1468, 2463, 1484],
                [1, 1680, 2463, 1696],
                [1, 1892, 2463, 1908],
                [1, 2104, 2463, 2120],
                [1, 2316, 2463, 2332],
            ],
            "thickness":
            0.32,
            "mu":
            3.9220322752480934,
            "gain":
            1.0,
            "trusted_range": [-1.0, 161977.0],
            "image_size": [2463, 2527],
            "px_mm_strategy": {
                "type": "ParallaxCorrectedPxMmStrategy"
            },
            "identifier":
            "",
            "type":
            "SENSOR_PAD",
            "pixel_size": [0.17200000000000001, 0.17200000000000001],
        }],
    }

    detector = DetectorFactory.from_dict(detector_dict)

    expt = Experiment(beam=beam, crystal=crystal, scan=scan, detector=detector)
    return expt
Example #15
0
def imagesweep_from_dict(d, check_format=True):
    '''Construct and image sweep from the dictionary.'''
    from dxtbx.imageset import ImageSetFactory
    from dxtbx.model import BeamFactory, DetectorFactory, GoniometerFactory, ScanFactory
    from dxtbx.serialize.filename import load_path

    # Get the template (required)
    template = load_path(str(d['template']))

    # If the scan isn't set, find all available files
    scan_dict = d.get('scan')
    if scan_dict is None:
        image_range = None
    else:
        image_range = scan_dict.get('image_range')

    # Construct the sweep
    try:
        sweep = ImageSetFactory.from_template(template,
                                              image_range,
                                              check_format=check_format)[0]

        # Get the existing models as dictionaries
        beam_dict = sweep.get_beam().to_dict()
        gonio_dict = sweep.get_goniometer().to_dict()
        detector_dict = sweep.get_detector().to_dict()
        scan_dict = sweep.get_scan().to_dict()
    except Exception:
        indices = range(image_range[0], image_range[1] + 1)
        sweep = ImageSetFactory.make_sweep(template,
                                           indices,
                                           check_format=False)
        beam_dict = None
        gonio_dict = None
        detector_dict = None
        scan_dict = None

    # Set some external lookups
    if 'mask' in d and d['mask'] is not None:
        with open(d['mask']) as infile:
            sweep.external_lookup.mask.filename = d['mask']
            sweep.external_lookup.mask.data = pickle.load(infile)
    if 'gain' in d and d['gain'] is not None:
        with open(d['gain']) as infile:
            sweep.external_lookup.gain.filename = d['gain']
            sweep.external_lookup.gain.data = pickle.load(infile)
    if 'pedestal' in d and d['pedestal'] is not None:
        with open(d['pedestal']) as infile:
            sweep.external_lookup.pedestal.filename = d['pedestal']
            sweep.external_lookup.pedestal.data = pickle.load(infile)

    # Set the models with the exisiting models as templates
    sweep.set_beam(BeamFactory.from_dict(d.get('beam'), beam_dict))
    sweep.set_goniometer(
        GoniometerFactory.from_dict(d.get('goniometer'), gonio_dict))
    sweep.set_detector(
        DetectorFactory.from_dict(d.get('detector'), detector_dict))
    sweep.set_scan(ScanFactory.from_dict(d.get('scan'), scan_dict))

    # Return the sweep
    return sweep
Example #16
0
 def _beam_from_dict(obj):
     """ Get a beam from a dictionary. """
     return BeamFactory.from_dict(obj)