def __init__(self, file_name, graphics_bin=1): #, output_file=None): from rstbx.slip_viewer.slip_viewer_image_factory import SlipViewerImageFactory as ImageFactory from iotbx.detectors import ImageException try: from PIL import Image except ImportError: import Image try: img = ImageFactory(file_name) except ImageException as e: raise Sorry(str(e)) img.read() self.img = img if (img.vendortype in ["Pilatus-6M", "Pilatus-2M", "Pilatus-300K"]): graphics_bin = 1 flex_img = img.get_flex_image(binning=graphics_bin, brightness=1) flex_img.setWindow(0.0, 0.0, 1) flex_img.spot_convention(0) flex_img.adjust(color_scheme=0) flex_img.prep_string() data_string = flex_img.export_string try: # fromstring raises Exception in Pillow >= 3.0.0 self.imageout = Image.fromstring( "RGB", (flex_img.ex_size2(), flex_img.ex_size1()), data_string) except Exception: self.imageout = Image.frombytes( "RGB", (flex_img.ex_size2(), flex_img.ex_size1()), data_string)
def test_read_image(test_image, dials_regression): """Test that all the regression images can be read""" if not xfel and test_image.startswith("LCLS"): pytest.skip("could not import 'xfel'") if not h5py and test_image.endswith((".h5", ".nxs")): pytest.skip("could not import 'h5py'") if dials_regression: test_image = os.path.join( dials_regression, "image_examples", *test_image.split("/") ) format_instance = dxtbx.format.Registry.get_format_class_for_file(test_image) print("Reading", test_image) print("Format:", format_instance) assert format_instance, "no matching format class found" instance = format_instance(test_image) # Test metadata reading instance.get_goniometer() instance.get_beam() instance.get_scan() detector = instance.get_detector() # From old test_dxtbx; get the image size if detector is not None: detector[0].get_image_size() for panel in detector: d_mat = scitbx.matrix.sqr(panel.get_d_matrix()) if d_mat.determinant() <= 0: print(" d matrix with non-positive determinant") # Test reading of the raw data # XDS, HKL we expect to fail for this - so skip this part for those if not test_image.endswith(("XDS", "HKL")): try: R_raw_data = instance.get_raw_data() except TypeError: R_raw_data = instance.get_raw_data(0) if not isinstance(R_raw_data, tuple): R_raw_data = (R_raw_data,) print("%-40s" % format_instance.__name__, R_raw_data[0].focus()) # Set instance.detectorbase, if available. There used to be a blacklist # of files not to call this with, but relying on the attribute test # seems to be just as effective try: instance.get_detectorbase() except NotImplementedError: pass print(" Have detectorbase? ", hasattr(instance, "detectorbase")) # Specific test for cctbx/dxtbx#163. This test will fail if char is unsigned. if "APS_24IDC" in test_image and "pilatus_1_0001.cbf" in test_image: d = R_raw_data[0] assert ( flex.sum(d.as_1d().select(d.as_1d() >= 0)) == 20108255 ) # matches total counts from dxtbx.print_header # test the older detectorbase interface if available if hasattr(instance, "detectorbase"): imgfactory = SlipViewerImageFactory(test_image) imgfactory.read() print(" Detectorbase:", instance.detectorbase.__class__.__name__) try: print(imgfactory.rawdata.focus()) except AttributeError: # Not all instances have this attribute print(" multireadout") I_raw_data = imgfactory.get_raw_data() if not isinstance(I_raw_data, tuple): I_raw_data = (I_raw_data,) # NOTE dxtbx and image factory arrays are compared here for identical values. for Ip, Rp in zip(I_raw_data, R_raw_data): assert (Ip == Rp).all_eq(True)
def test_read_image(test_image_for_reading): """Test that all the regression images can be read""" if "LCLS" in test_image_for_reading and not libtbx.env.has_module('xfel'): pytest.skip("Ignoring LCLS because xfel missing") return format_instance = Registry.find(test_image_for_reading) instance = format_instance(test_image_for_reading) print("Reading", test_image_for_reading) print("Format:", format_instance) # Test metadata reading instance.get_goniometer() instance.get_beam() instance.get_scan() detector = instance.get_detector() # From old test_dxtbx; get the image size if detector is not None: detector[0].get_image_size() for panel in detector: d_mat = scitbx.matrix.sqr(panel.get_d_matrix()) if d_mat.determinant() == 0: print(" d matrix with zero determinant") if d_mat.determinant() < 0: print(" d matrix with negative determinant") # Test reading of the raw data # XDS, HKL we expect to fail for this - so skip this part for those if not test_image_for_reading[-3:].upper() in {"XDS", "HKL"}: try: R_raw_data = instance.get_raw_data() except TypeError: R_raw_data = instance.get_raw_data(0) if not isinstance(R_raw_data, tuple): R_raw_data = (R_raw_data, ) print("%-40s" % format_instance.__name__, R_raw_data[0].focus()) # Set instance.detectorbase, if available. There used to be a blacklist # of files not to call this with, but relying on the attribute test # seems to be just as effective instance.get_detectorbase() print(" Have detectorbase? ", hasattr(instance, "detectorbase")) # test the older detectorbase interface if available if hasattr(instance, "detectorbase"): imgfactory = SlipViewerImageFactory(test_image_for_reading) imgfactory.read() print(" Detectorbase:", instance.detectorbase.__class__.__name__) try: print(imgfactory.rawdata.focus()) except AttributeError: # Not all instances have this attribute print(" multireadout") I_raw_data = imgfactory.get_raw_data() if not isinstance(I_raw_data, tuple): I_raw_data = (I_raw_data, ) # NOTE dxtbx and image factory arrays are compared here for identical values. for Ip, Rp in zip(I_raw_data, R_raw_data): assert (Ip == Rp).all_eq(True) convert_image(test_image_for_reading, graphics_bin=2).output().getvalue() print
def test_read_image(test_image_for_reading): """Test that all the regression images can be read""" if "LCLS" in test_image_for_reading and not libtbx.env.has_module("xfel"): pytest.skip("Ignoring LCLS because xfel missing") return format_instance = dxtbx.format.Registry.get_format_class_for_file( test_image_for_reading ) print("Reading", test_image_for_reading) print("Format:", format_instance) assert format_instance, "no matching format class found" instance = format_instance(test_image_for_reading) # Test metadata reading instance.get_goniometer() instance.get_beam() instance.get_scan() detector = instance.get_detector() # From old test_dxtbx; get the image size if detector is not None: detector[0].get_image_size() for panel in detector: d_mat = scitbx.matrix.sqr(panel.get_d_matrix()) if d_mat.determinant() == 0: print(" d matrix with zero determinant") if d_mat.determinant() < 0: print(" d matrix with negative determinant") # Test reading of the raw data # XDS, HKL we expect to fail for this - so skip this part for those if not test_image_for_reading[-3:].upper() in {"XDS", "HKL"}: try: R_raw_data = instance.get_raw_data() except TypeError: R_raw_data = instance.get_raw_data(0) if not isinstance(R_raw_data, tuple): R_raw_data = (R_raw_data,) print("%-40s" % format_instance.__name__, R_raw_data[0].focus()) # Set instance.detectorbase, if available. There used to be a blacklist # of files not to call this with, but relying on the attribute test # seems to be just as effective instance.get_detectorbase() print(" Have detectorbase? ", hasattr(instance, "detectorbase")) # Specific test for cctbx/dxtbx#163. This test will fail if char is unsigned. if ( "APS_24IDC" in test_image_for_reading and "pilatus_1_0001.cbf" in test_image_for_reading ): d = R_raw_data[0] assert ( flex.sum(d.as_1d().select(d.as_1d() >= 0)) == 20108255 ) # matches total counts from dxtbx.print_header # test the older detectorbase interface if available if hasattr(instance, "detectorbase"): imgfactory = SlipViewerImageFactory(test_image_for_reading) imgfactory.read() print(" Detectorbase:", instance.detectorbase.__class__.__name__) try: print(imgfactory.rawdata.focus()) except AttributeError: # Not all instances have this attribute print(" multireadout") I_raw_data = imgfactory.get_raw_data() if not isinstance(I_raw_data, tuple): I_raw_data = (I_raw_data,) # NOTE dxtbx and image factory arrays are compared here for identical values. for Ip, Rp in zip(I_raw_data, R_raw_data): assert (Ip == Rp).all_eq(True)