def read_smv_image(image_file): from boost.python import streambuf from dxtbx import read_uint16, read_uint16_bs, is_big_endian from scitbx.array_family import flex header_size, header_dictionary = get_smv_header(image_file) f = open(image_file, 'rb') f.read(header_size) if header_dictionary['BYTE_ORDER'] == 'big_endian': big_endian = True else: big_endian = False image_size = (int(header_dictionary['SIZE1']), int(header_dictionary['SIZE2'])) if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(f), int(image_size[0] * image_size[1])) else: raw_data = read_uint16_bs(streambuf(f), int(image_size[0] * image_size[1])) raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def get_raw_data(self): """Get the pixel intensities (i.e. read the image and return as a flex array of integers.)""" from boost.python import streambuf from dxtbx import read_uint16, read_uint16_bs, is_big_endian from scitbx.array_family import flex assert len(self.get_detector()) == 1 image_pedestal = 40 panel = self.get_detector()[0] size = panel.get_image_size() f = FormatSMVADSCSN.open_file(self._image_file, "rb") f.read(self._header_size) if self._header_dictionary["BYTE_ORDER"] == "big_endian": big_endian = True else: big_endian = False if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(f), int(size[0] * size[1])) else: raw_data = read_uint16_bs(streambuf(f), int(size[0] * size[1])) # apply image pedestal, will result in *negative pixel values* raw_data -= image_pedestal image_size = panel.get_image_size() raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def get_raw_data(self): '''Get the pixel intensities (i.e. read the image and return as a flex array of integers.)''' from boost.python import streambuf from dxtbx import read_uint16, read_uint16_bs, is_big_endian from scitbx.array_family import flex f = self.open_file(self._image_file, 'rb') f.read(self._header_size) if self._header_dictionary['BYTE_ORDER'] == 'big_endian': big_endian = True else: big_endian = False if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(f), int(512 * 512)) else: raw_data = read_uint16_bs(streambuf(f), int(512 * 512)) image_size = (512, 512) raw_data.reshape(flex.grid(image_size[1], image_size[0])) # split into separate panels self._raw_data = [] d = self.get_detector() for panel in d: xmin, ymin, xmax, ymax = self.coords[panel.get_name()] self._raw_data.append(raw_data[ymin:ymax, xmin:xmax]) return tuple(self._raw_data)
def get_raw_data(self): """Get the pixel intensities (i.e. read the image and return as a flex array of integers.)""" from boost.python import streambuf from dxtbx import read_uint16, read_uint16_bs, is_big_endian from scitbx.array_family import flex assert len(self.get_detector()) == 1 image_pedestal = int(self._header_dictionary["IMAGE_PEDESTAL"]) panel = self.get_detector()[0] size = panel.get_image_size() f = FormatSMVADSCSN.open_file(self._image_file, "rb") f.read(self._header_size) if self._header_dictionary["BYTE_ORDER"] == "big_endian": big_endian = True else: big_endian = False if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(f), int(size[0] * size[1])) else: raw_data = read_uint16_bs(streambuf(f), int(size[0] * size[1])) # apply image pedestal, will result in *negative pixel values* raw_data -= image_pedestal image_size = panel.get_image_size() raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def get_raw_data(self): '''Get the pixel intensities (i.e. read the image and return as a flex array of integers.)''' from boost.python import streambuf from dxtbx import read_uint16, read_uint16_bs, is_big_endian from scitbx.array_family import flex assert (len(self.get_detector()) == 1) panel = self.get_detector()[0] size = panel.get_image_size() f = FormatSMVNOIR.open_file(self._image_file, 'rb') f.read(self._header_size) if self._header_dictionary['BYTE_ORDER'] == 'big_endian': big_endian = True else: big_endian = False if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(f), int(size[0] * size[1])) else: raw_data = read_uint16_bs(streambuf(f), int(size[0] * size[1])) image_size = panel.get_image_size() raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def get_raw_data(self): '''Get the pixel intensities (i.e. read the image and return as a flex array of integers.)''' from boost.python import streambuf from dxtbx import read_uint16, read_uint16_bs, is_big_endian from scitbx.array_family import flex assert(len(self.get_detector()) == 1) panel = self.get_detector()[0] size = panel.get_image_size() f = FormatSMVADSC.open_file(self._image_file, 'rb') f.read(self._header_size) if self._header_dictionary['BYTE_ORDER'] == 'big_endian': big_endian = True else: big_endian = False if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(f), int(size[0] * size[1])) else: raw_data = read_uint16_bs(streambuf(f), int(size[0] * size[1])) image_size = panel.get_image_size() raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def get_raw_data(self): '''Get the pixel intensities (i.e. read the image and return as a flex array of integers.)''' from boost.python import streambuf from dxtbx import read_uint16, read_uint16_bs, is_big_endian from scitbx.array_family import flex f = self.open_file(self._image_file, 'rb') f.read(self._header_size) nx = int(self._header_dictionary['SIZE1']) # number of pixels ny = int(self._header_dictionary['SIZE2']) # number of pixels if self._header_dictionary['BYTE_ORDER'] == 'big_endian': big_endian = True else: big_endian = False if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(f), int(nx * ny)) else: raw_data = read_uint16_bs(streambuf(f), int(nx * ny)) # note that x and y are reversed here raw_data.reshape(flex.grid(ny, nx)) self._raw_data = raw_data return self._raw_data
def read_smv_image(image_file): from boost.python import streambuf from dxtbx import read_uint16, read_uint16_bs, is_big_endian from scitbx.array_family import flex header_size, header_dictionary = get_smv_header(image_file) with open(image_file, "rb") as f: f.seek(header_size) if header_dictionary["BYTE_ORDER"] == "big_endian": big_endian = True else: big_endian = False image_size = (int(header_dictionary["SIZE1"]), int(header_dictionary["SIZE2"])) if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(f), int(image_size[0] * image_size[1])) else: raw_data = read_uint16_bs(streambuf(f), int(image_size[0] * image_size[1])) raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def get_raw_data(self): '''Get the pixel intensities (i.e. read the image and return as a flex array of integers.)''' from boost.python import streambuf from dxtbx import read_uint16, read_uint16_bs, is_big_endian from scitbx.array_family import flex f = self.open_file(self._image_file, 'rb') header_size = int(self.header_dict['HDRBLKS']) * 512 f.read(header_size) # 16 bits per pixel assert int(self.header_dict['NPIXELB'].split()[0]) == 2 nrows = int(self.header_dict['NROWS'].split()[0]) ncols = int(self.header_dict['NCOLS'].split()[0]) if is_big_endian(): raw_data = read_uint16_bs(streambuf(f), nrows * ncols) else: raw_data = read_uint16(streambuf(f), nrows * ncols) image_size = (nrows, ncols) raw_data.reshape(flex.grid(image_size[0], image_size[1])) return raw_data
def time_it(path, buffer_size): if (buffer_size is None): buffer_size = streambuf.default_buffer_size print "Buffer is %i bytes" % buffer_size path = os.path.expanduser(path) input = open(path, 'r') inp_buf = streambuf(python_file_obj=input, buffer_size=buffer_size) ext.time_read(input.name, inp_buf) output = open("tmp_tst_python_streambuf", "w") out_buf = streambuf(python_file_obj=output, buffer_size=buffer_size) ext.time_write(output.name, out_buf)
def time_it(path, buffer_size): if (buffer_size is None): buffer_size = streambuf.default_buffer_size print("Buffer is %i bytes" % buffer_size) path = os.path.expanduser(path) input = open(path, 'r') inp_buf = streambuf(python_file_obj=input, buffer_size=buffer_size) ext.time_read(input.name, inp_buf) output = open("tmp_tst_python_streambuf", "w") out_buf = streambuf(python_file_obj=output, buffer_size=buffer_size) ext.time_write(output.name, out_buf)
def _get_endianic_raw_data(self, size): big_endian = self._header_dictionary["BYTE_ORDER"] == "big_endian" with self.open_file(self._image_file, "rb") as fh: fh.seek(self._header_size) if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(fh), int(size[0] * size[1])) else: raw_data = read_uint16_bs(streambuf(fh), int(size[0] * size[1])) # note that x and y are reversed here raw_data.reshape(flex.grid(size[1], size[0])) return raw_data
def get_raw_data(self): """Get the pixel intensities""" from boost.python import streambuf try: from dxtbx.ext import read_uint16_bs except ImportError: from dxtbx import read_uint16_bs from scitbx.array_family import flex f = FormatTimepixRaw512x512.open_file(self._image_file, "rb") f.read(self._header_size) raw_data = read_uint16_bs(streambuf(f), 512 * 512) image_size = (512, 512) raw_data.reshape(flex.grid(image_size[1], image_size[0])) self._raw_data = [] d = self.get_detector() for panel in d: xmin, ymin, xmax, ymax = self.coords[panel.get_name()] self._raw_data.append(raw_data[ymin:ymax, xmin:xmax]) return tuple(self._raw_data)
def exercise_partial_read(self): self.create_file_object(mode='r') words = ext.test_read(streambuf(self.file_object), "partial read") assert words == "Coding, should, " trailing = self.file_object.read() assert trailing == " be fun" self.file_object.close()
def exercise_seek_and_read(self): self.create_instrumented_file_object(mode='r') words = ext.test_read(streambuf(self.file_object), "read and seek") assert words == "should, should, uld, ding, fun, [ eof ]" n = streambuf.default_buffer_size soughts = self.file_object.seek_call_log # stringent tests carefully crafted to make sure the seek-in-buffer # optimisation works as expected # C.f. the comment in the C++ function actual_write_test assert soughts[-1] == (-4,2) soughts = soughts[:-1] if n >= 14: assert soughts == [] else: assert soughts[0] == (6,0) soughts = soughts[1:] if 8 <= n <= 13: assert len(soughts) == 1 and self.only_seek_cur(soughts) elif n == 7: assert len(soughts) == 2 and self.only_seek_cur(soughts) else: assert soughts[0] == (6,0) soughts = soughts[1:] assert self.only_seek_cur(soughts) if n == 4: assert len(soughts) == 1 else: assert len(soughts) == 2 self.file_object.close()
def _read_cbf_image(self): from cbflib_adaptbx import uncompress start_tag = binascii.unhexlify("0c1a04d5") with self.open_file(self._image_file, "rb") as fh: data = fh.read() data_offset = data.find(start_tag) + 4 cbf_header = self._parse_cbf_header( data[: data_offset - 4].decode("ascii", "ignore") ) if cbf_header["byte_offset"]: pixel_values = uncompress( packed=data[data_offset : data_offset + cbf_header["size"]], fast=cbf_header["fast"], slow=cbf_header["slow"], ) elif cbf_header["no_compression"]: from boost.python import streambuf from dxtbx import read_int32 from scitbx.array_family import flex assert len(self.get_detector()) == 1 with self.open_file(self._image_file) as f: f.read(data_offset) pixel_values = read_int32(streambuf(f), cbf_header["length"]) pixel_values.reshape(flex.grid(cbf_header["slow"], cbf_header["fast"])) else: raise ValueError( "Compression of type other than byte_offset or none is not supported (contact authors)" ) return pixel_values
def exercise_seek_and_read(self): self.create_instrumented_file_object(mode='r') words = ext.test_read(streambuf(self.file_object), "read and seek") assert words == "should, should, uld, ding, fun, [ eof ]" n = streambuf.default_buffer_size soughts = self.file_object.seek_call_log # stringent tests carefully crafted to make sure the seek-in-buffer # optimisation works as expected # C.f. the comment in the C++ function actual_write_test assert soughts[-1] == (-4, 2) soughts = soughts[:-1] if n >= 14: assert soughts == [] else: assert soughts[0] == (6, 0) soughts = soughts[1:] if 8 <= n <= 13: assert len(soughts) == 1 and self.only_seek_cur(soughts) elif n == 7: assert len(soughts) == 2 and self.only_seek_cur(soughts) else: assert soughts[0] == (6, 0) soughts = soughts[1:] assert self.only_seek_cur(soughts) if n == 4: assert len(soughts) == 1 else: assert len(soughts) == 2 self.file_object.close()
def channel_pixels(wavelength_A, flux, N, UMAT_nm, Amatrix_rot, fmodel_generator, local_data): fmodel_generator.reset_wavelength(wavelength_A) fmodel_generator.reset_specific_at_wavelength( label_has="FE1", tables=local_data.get("Fe_oxidized_model"), newvalue=wavelength_A) fmodel_generator.reset_specific_at_wavelength( label_has="FE2", tables=local_data.get("Fe_reduced_model"), newvalue=wavelength_A) print("USING scatterer-specific energy-dependent scattering factors") sfall_channel = fmodel_generator.get_amplitudes() SIM = nanoBragg(detpixels_slowfast=(3000, 3000), pixel_size_mm=0.11, Ncells_abc=(N, N, N), wavelength_A=wavelength_A, verbose=0) SIM.adc_offset_adu = 10 # Do not offset by 40 SIM.mosaic_spread_deg = 0.05 # interpreted by UMAT_nm as a half-width stddev SIM.mosaic_domains = 25 # 77 seconds. With 100 energy points, 7700 seconds (2 hours) per image SIM.distance_mm = 141.7 SIM.set_mosaic_blocks(UMAT_nm) #SIM.detector_thick_mm = 0.5 # = 0 for Rayonix #SIM.detector_thicksteps = 1 # should default to 1 for Rayonix, but set to 5 for CSPAD #SIM.detector_attenuation_length_mm = default is silicon # get same noise each time this test is run SIM.seed = 1 SIM.oversample = 1 SIM.wavelength_A = wavelength_A SIM.polarization = 1 SIM.default_F = 0 SIM.Fhkl = sfall_channel SIM.Amatrix_RUB = Amatrix_rot SIM.xtal_shape = shapetype.Gauss # both crystal & RLP are Gaussian SIM.progress_meter = False # flux is always in photons/s SIM.flux = flux SIM.exposure_s = 1.0 # so total fluence is e12 # assumes round beam SIM.beamsize_mm = 0.003 #cannot make this 3 microns; spots are too intense temp = SIM.Ncells_abc print("Ncells_abc=", SIM.Ncells_abc) SIM.Ncells_abc = temp from libtbx.development.timers import Profiler P = Profiler("nanoBragg") if add_spots_algorithm is "NKS": from boost.python import streambuf # will deposit printout into dummy StringIO as side effect SIM.add_nanoBragg_spots_nks(streambuf(StringIO())) elif add_spots_algorithm is "JH": SIM.add_nanoBragg_spots() elif add_spots_algorithm is "cuda": SIM.add_nanoBragg_spots_cuda() else: raise Exception("unknown spots algorithm") del P return SIM
def read_cbf_image(self, cbf_image): from cbflib_adaptbx import uncompress import binascii start_tag = binascii.unhexlify("0c1a04d5") data = self.open_file(cbf_image, "rb").read() data_offset = data.find(start_tag) + 4 cbf_header = data[:data_offset - 4] fast = 0 slow = 0 length = 0 byte_offset = False no_compression = False for record in cbf_header.split("\n"): if "X-Binary-Size-Fastest-Dimension" in record: fast = int(record.split()[-1]) elif "X-Binary-Size-Second-Dimension" in record: slow = int(record.split()[-1]) elif "X-Binary-Number-of-Elements" in record: length = int(record.split()[-1]) elif "X-Binary-Size:" in record: size = int(record.split()[-1]) elif "conversions" in record: if "x-CBF_BYTE_OFFSET" in record: byte_offset = True elif "x-CBF_NONE" in record: no_compression = True assert length == fast * slow if byte_offset: pixel_values = uncompress(packed=data[data_offset:data_offset + size], fast=fast, slow=slow) elif no_compression: from boost.python import streambuf try: from dxtbx.ext import read_int32 except ImportError: from dxtbx import read_int32 from scitbx.array_family import flex assert len(self.get_detector()) == 1 f = self.open_file(self._image_file) f.read(data_offset) pixel_values = read_int32(streambuf(f), int(slow * fast)) pixel_values.reshape(flex.grid(slow, fast)) else: raise ValueError( "Uncompression of type other than byte_offset or none " " is not supported (contact authors)") return pixel_values
def exercise_write_failure(self): import platform if (platform.platform().find("redhat-8.0") >= 0): return # avoid Abort self.create_file_object(mode='r') try: ext.test_write(streambuf(self.file_object), "write") except IOError, err: pass
def __call__(self, N, UMAT_nm, Amatrix_rot, sfall_channel): SIM = nanoBragg(detpixels_slowfast=(3000, 3000), pixel_size_mm=0.11, Ncells_abc=(N, N, N), wavelength_A=self.wavlen[0], verbose=0) SIM.adc_offset_adu = 10 # Do not offset by 40 SIM.mosaic_domains = n_mosaic_domains # 77 seconds. With 100 energy points, 7700 seconds (2 hours) per image SIM.mosaic_spread_deg = mosaic_spread_deg # interpreted by UMAT_nm as a half-width stddev SIM.distance_mm = distance_mm SIM.set_mosaic_blocks(UMAT_nm) #SIM.detector_thick_mm = 0.5 # = 0 for Rayonix #SIM.detector_thicksteps = 1 # should default to 1 for Rayonix, but set to 5 for CSPAD #SIM.detector_attenuation_length_mm = default is silicon # get same noise each time this test is run SIM.seed = 1 SIM.oversample = 1 SIM.polarization = 1 SIM.default_F = 0 SIM.Fhkl = sfall_channel SIM.Amatrix_RUB = Amatrix_rot SIM.xtal_shape = shapetype.Gauss # both crystal & RLP are Gaussian SIM.progress_meter = False # flux is always in photons/s #SIM.flux=0.8E10 # number of photons per 100 ps integrated pulse, 24-bunch APS SIM.flux = 0.8E12 # number of photons per 100 ps integrated pulse, 24-bunch APS SIM.xray_source_wavelengths_A = self.wavlen SIM.xray_source_intensity_fraction = self.norm_intensity SIM.xray_source_XYZ = self.source_XYZ SIM.exposure_s = 1.0 # so total fluence is e12 # assumes round beam SIM.beamsize_mm = 0.003 #cannot make this 3 microns; spots are too intense temp = SIM.Ncells_abc print("Ncells_abc=", SIM.Ncells_abc) SIM.Ncells_abc = temp from libtbx.development.timers import Profiler P = Profiler("nanoBragg") if add_spots_algorithm is "NKS": print("USING NKS") from boost.python import streambuf # will deposit printout into dummy StringIO as side effect SIM.add_nanoBragg_spots_nks(streambuf(StringIO())) elif add_spots_algorithm is "JH": print("USING JH") SIM.add_nanoBragg_spots() elif add_spots_algorithm is "cuda": print("USING cuda") SIM.add_nanoBragg_spots_cuda() else: raise Exception("unknown spots algorithm") del P return SIM
def channel_pixels(simparams=None,single_wavelength_A=None,single_flux=None,N=None,UMAT_nm=None, \ Amatrix_rot=None,sfall_channel=None,rank=None): # print("## inside channel wavlength/flux = ", single_wavelength_A, single_flux/simparams.flux) SIM = nanoBragg(detpixels_slowfast=(simparams.detector_size_ny,simparams.detector_size_nx),pixel_size_mm=simparams.pixel_size_mm,\ Ncells_abc=(N,N,N),wavelength_A=single_wavelength_A,verbose=0) SIM.adc_offset_adu = 10 # Do not offset by 40 SIM.seed = 0 SIM.mosaic_spread_deg = simparams.mosaic_spread_deg # interpreted by UMAT_nm as a half-width stddev SIM.mosaic_domains = simparams.mosaic_domains # 77 seconds. With 100 energy points, 7700 seconds (2 hours) per image SIM.distance_mm = simparams.distance_mm SIM.set_mosaic_blocks(UMAT_nm) ###################### SIM.beamcenter_convention = convention.ADXV SIM.beam_center_mm = (simparams.beam_center_x_mm, simparams.beam_center_y_mm) # 95.975 96.855 ###################### # get same noise each time this test is run SIM.seed = 0 SIM.oversample = simparams.oversample SIM.wavelength_A = single_wavelength_A SIM.polarization = simparams.polarization SIM.default_F = simparams.default_F SIM.Fhkl = sfall_channel SIM.Amatrix_RUB = Amatrix_rot SIM.xtal_shape = shapetype.Gauss # both crystal & RLP are Gaussian SIM.progress_meter = False # flux is always in photons/s SIM.flux = single_flux SIM.exposure_s = simparams.exposure_s # so total fluence is e12 # assumes round beam SIM.beamsize_mm = simparams.beamsize_mm #cannot make this 3 microns; spots are too intense temp = SIM.Ncells_abc SIM.Ncells_abc = temp # SIM.show_params() add_spots_algorithm = simparams.add_spots_algorithm if add_spots_algorithm == "NKS": from boost.python import streambuf # will deposit printout into dummy StringIO as side effect SIM.add_nanoBragg_spots_nks(streambuf(StringIO())) elif add_spots_algorithm == "JH": SIM.add_nanoBragg_spots() elif add_spots_algorithm == "cuda": devices_per_node = int(os.environ["DEVICES_PER_NODE"]) SIM.device_Id = rank % devices_per_node #if rank==7: # os.system("nvidia-smi") SIM.add_nanoBragg_spots_cuda() else: raise Exception("!!! unknown spots algorithm") return SIM
def exercise_read_failure(self): self.create_file_object(mode='r') self.file_object.close() try: ext.test_read(streambuf(self.file_object), "read") except ValueError: pass else: raise Exception_expected self.file_object.close()
def sim_rois(self, rois=None, reset=True, cuda=False, omp=False, add_water=False, boost=1, add_spots=True, show_params=False): if show_params: self.SIM2.show_params() #if self.crystal_size_mm is not None: print(" Mosaic domain size mm = %.3g" % np.power(self.mosaic_domain_volume, 1 / 3.)) print(" Spot scale = %.3g" % self.SIM2.spot_scale) if rois is None: rois = [self.FULL_ROI] if add_spots: for roi in rois: self.SIM2.region_of_interest = roi if cuda: self.SIM2.add_nanoBragg_spots_cuda() elif omp: from boost.python import streambuf # will deposit printout into dummy StringIO as side effect from six.moves import StringIO self.SIM2.add_nanoBragg_spots_nks(streambuf(StringIO())) else: self.SIM2.add_nanoBragg_spots() self.SIM2.raw_pixels = self.SIM2.raw_pixels * boost if add_water: # add water for full panel, ignoring ROI water_scatter = flex.vec2_double([(0, 2.57), (0.0365, 2.58), (0.07, 2.8), (0.12, 5), (0.162, 8), (0.2, 6.75), (0.18, 7.32), (0.216, 6.75), (0.236, 6.5), (0.28, 4.5), (0.3, 4.3), (0.345, 4.36), (0.436, 3.77), (0.5, 3.17)]) self.SIM2.Fbg_vs_stol = water_scatter self.SIM2.amorphous_density_gcm3 = 1 self.SIM2.amorphous_molecular_weight_Da = 18 self.SIM2.amorphous_sample_thick_mm = self.amorphous_sample_thick_mm self.SIM2.region_of_interest = self.FULL_ROI self.SIM2.add_background() img = self.SIM2.raw_pixels.as_numpy_array() if reset: self.SIM2.raw_pixels *= 0 self.SIM2.region_of_interest = self.FULL_ROI return img
def get_raw_data(self): """Read the data - assuming it is streaming 4-byte unsigned ints following the header...""" assert len(self.get_detector()) == 1 size = self.get_detector()[0].get_image_size() f = self.open_file(self._image_file) f.read(int(self._header_dictionary["HEADER_BYTES"])) raw_data = read_int32(streambuf(f), int(size[0] * size[1])) raw_data.reshape(flex.grid(size[1], size[0])) return raw_data
def read_smv_image(image_file): header_size, header_dictionary = FormatSMV.get_smv_header(image_file) with open(image_file, "rb") as f: f.seek(header_size) big_endian = header_dictionary["BYTE_ORDER"] == "big_endian" image_size = (int(header_dictionary["SIZE1"]), int(header_dictionary["SIZE2"])) if big_endian == dxtbx.ext.is_big_endian(): raw_data = dxtbx.ext.read_uint16( streambuf(f), int(image_size[0] * image_size[1])) else: raw_data = dxtbx.ext.read_uint16_bs( streambuf(f), int(image_size[0] * image_size[1])) raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def get_raw_data(self): """Get the pixel intensities (i.e. read the image and return as a flex array of integers.)""" from boost.python import streambuf from dxtbx import read_uint16, read_uint16_bs, is_big_endian from scitbx.array_family import flex assert len(self.get_detector()) == 1 image_pedestal = 1 try: image_pedestal = float(self._header_dictionary["ADC_OFFSET"]) except KeyError: pass panel = self.get_detector()[0] size = panel.get_image_size() f = FormatSMVJHSim.open_file(self._image_file, "rb") f.read(self._header_size) if self._header_dictionary["BYTE_ORDER"] == "big_endian": big_endian = True else: big_endian = False if big_endian == is_big_endian(): raw_data = read_uint16(streambuf(f), int(size[0] * size[1])) else: raw_data = read_uint16_bs(streambuf(f), int(size[0] * size[1])) # apply image pedestal, will result in *negative pixel values* # this is a horrible idea since raw_data is unsigned # see all instances of image_pedestal in dxtbx raw_data -= int(image_pedestal) image_size = panel.get_image_size() raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def read_tiff_image(image_file): # currently have no non-little-endian machines... width, height, depth, order, header_bytes = get_tiff_header(image_file) image_size = (width, height) header_size = 4096 with open(image_file, "rb") as fh: fh.seek(header_size) raw_data = dxtbx.ext.read_uint16(streambuf(fh), int(image_size[0] * image_size[1])) raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def sim_rois(self, rois, reset=True, cuda=False, omp=False, add_water=False, add_noise=False, water_par=None, noise_par=None, boost=1, add_spots=True): if add_spots: for roi in rois: self.SIM2.region_of_interest = roi if cuda: self.SIM2.add_nanoBragg_spots_cuda() elif omp: from boost.python import streambuf # will deposit printout into dummy StringIO as side effect from six.moves import StringIO self.SIM2.add_nanoBragg_spots_nks(streambuf(StringIO())) else: self.SIM2.add_nanoBragg_spots() self.SIM2.raw_pixels = self.SIM2.raw_pixels * boost if add_water: water_scatter = flex.vec2_double([(0, 2.57), (0.0365, 2.58), (0.07, 2.8), (0.12, 5), (0.162, 8), (0.2, 6.75), (0.18, 7.32), (0.216, 6.75), (0.236, 6.5), (0.28, 4.5), (0.3, 4.3), (0.345, 4.36), (0.436, 3.77), (0.5, 3.17)]) self.SIM2.Fbg_vs_stol = water_scatter self.SIM2.amorphous_sample_thick_mm = 0.004 # typical GDVN self.SIM2.amorphous_density_gcm3 = 1 self.SIM2.amorphous_molecular_weight_Da = 18 self.SIM2.exposure_s = 1.0 # multiplies flux x exposure self.SIM2.region_of_interest = self.FULL_ROI self.SIM2.add_background() if add_noise: self.SIM2.add_noise() img = self.SIM2.raw_pixels.as_numpy_array() if reset: self.SIM2.raw_pixels *= 0 self.SIM2.region_of_interest = self.FULL_ROI return img
def get_raw_data(self, index): from boost.python import streambuf from scitbx.array_family import flex data_offset = self._header_dictionary['DataOffsetArray'][index] with FormatSER.open_file(self._image_file, 'rb') as f: f.seek(data_offset) d = {} d['CalibrationOffsetX'] = struct.unpack('<d', f.read(8))[0] d['CalibrationDeltaX'] = struct.unpack('<d', f.read(8))[0] d['CalibrationElementX'] = struct.unpack('<I', f.read(4))[0] d['CalibrationOffsetY'] = struct.unpack('<d', f.read(8))[0] d['CalibrationDeltaY'] = struct.unpack('<d', f.read(8))[0] d['CalibrationElementY'] = struct.unpack('<I', f.read(4))[0] d['DataType'] = struct.unpack('<H', f.read(2))[0] if d['DataType'] == 6: from dxtbx import read_int32 as read_pixel elif d['DataType'] == 5: from dxtbx import read_int16 as read_pixel elif d['DataType'] == 3: from dxtbx import read_uint32 as read_pixel elif d['DataType'] == 2: from dxtbx import read_uint16 as read_pixel elif d['DataType'] == 1: from dxtbx import read_uint8 as read_pixel else: raise RuntimeError( 'Image {0} data is of an unsupported type'.format(index + 1)) d['ArraySizeX'] = struct.unpack('<I', f.read(4))[0] d['ArraySizeY'] = struct.unpack('<I', f.read(4))[0] nelts = d['ArraySizeX'] * d['ArraySizeY'] raw_data = read_pixel(streambuf(f), nelts) # Check image size is as expected (same as the first image) if d['ArraySizeX'] != self._header_dictionary['ArraySizeX']: raise RuntimeError( 'Image {0} has an unexpected array size in X'.format(index + 1)) if d['ArraySizeY'] != self._header_dictionary['ArraySizeY']: raise RuntimeError( 'Image {0} has an unexpected array size in Y'.format(index + 1)) image_size = (d['ArraySizeX'], d['ArraySizeY']) raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def get_raw_data(self): """Get the pixel intensities (i.e. read the image and return as a flex array of integers.)""" # currently have no non-little-endian machines... assert len(self.get_detector()) == 1 image_size = self.get_detector()[0].get_image_size() with self.open_file(self._image_file) as fh: fh.seek(self._header_size) raw_data = read_uint16(streambuf(fh), int(image_size[0] * image_size[1])) raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def get_raw_data(self): '''Read the data - assuming it is streaming 4-byte unsigned ints following the header...''' from boost.python import streambuf from dxtbx import read_int32 from scitbx.array_family import flex assert (len(self.get_detector()) == 1) size = self.get_detector()[0].get_image_size() f = self.open_file(self._image_file) f.read(int(self._header_dictionary['HEADER_BYTES'])) raw_data = read_int32(streambuf(f), int(size[0] * size[1])) raw_data.reshape(flex.grid(size[1], size[0])) return raw_data
def get_raw_data(self): '''Read the data - assuming it is streaming 4-byte unsigned ints following the header...''' from boost.python import streambuf from dxtbx import read_int32 from scitbx.array_family import flex assert(len(self.get_detector()) == 1) size = self.get_detector()[0].get_image_size() f = self.open_file(self._image_file) f.read(int(self._header_dictionary['HEADER_BYTES'])) raw_data = read_int32(streambuf(f), int(size[0] * size[1])) raw_data.reshape(flex.grid(size[1], size[0])) return raw_data
def get_raw_data(self, index): from boost.python import streambuf from scitbx.array_family import flex data_offset = self._header_dictionary["DataOffsetArray"][index] with FormatSER.open_file(self._image_file, "rb") as f: f.seek(data_offset) d = {} d["CalibrationOffsetX"] = struct.unpack("<d", f.read(8))[0] d["CalibrationDeltaX"] = struct.unpack("<d", f.read(8))[0] d["CalibrationElementX"] = struct.unpack("<I", f.read(4))[0] d["CalibrationOffsetY"] = struct.unpack("<d", f.read(8))[0] d["CalibrationDeltaY"] = struct.unpack("<d", f.read(8))[0] d["CalibrationElementY"] = struct.unpack("<I", f.read(4))[0] d["DataType"] = struct.unpack("<H", f.read(2))[0] if d["DataType"] == 6: read_pixel = dxtbx.ext.read_int32 elif d["DataType"] == 5: read_pixel = dxtbx.ext.read_int16 elif d["DataType"] == 3: read_pixel = dxtbx.ext.read_uint32 elif d["DataType"] == 2: read_pixel = dxtbx.ext.read_uint16 elif d["DataType"] == 1: read_pixel = dxtbx.ext.read_uint8 else: raise RuntimeError( "Image {} data is of an unsupported type".format(index + 1)) d["ArraySizeX"] = struct.unpack("<I", f.read(4))[0] d["ArraySizeY"] = struct.unpack("<I", f.read(4))[0] nelts = d["ArraySizeX"] * d["ArraySizeY"] raw_data = read_pixel(streambuf(f), nelts) # Check image size is as expected (same as the first image) if d["ArraySizeX"] != self._header_dictionary["ArraySizeX"]: raise RuntimeError( "Image {} has an unexpected array size in X".format(index + 1)) if d["ArraySizeY"] != self._header_dictionary["ArraySizeY"]: raise RuntimeError( "Image {} has an unexpected array size in Y".format(index + 1)) image_size = (d["ArraySizeX"], d["ArraySizeY"]) raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def read_tiff_image(image_file): # currently have no non-little-endian machines... from boost.python import streambuf from dxtbx import read_uint16 from scitbx.array_family import flex width, height, depth, order, header_bytes = get_tiff_header(image_file) image_size = (width, height) header_size = 4096 f = open(image_file) f.read(header_size) raw_data = read_uint16(streambuf(f), int(image_size[0] * image_size[1])) raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def get_raw_data(self): '''Get the pixel intensities (i.e. read the image and return as a flex array of integers.)''' # currently have no non-little-endian machines... from boost.python import streambuf from dxtbx import read_uint16 from scitbx.array_family import flex assert(len(self.get_detector()) == 1) size = self.get_detector()[0].get_image_size() f = FormatSMV.open_file(self._image_file) f.read(self._header_size) raw_data = read_uint16(streambuf(f), int(size[0] * size[1])) image_size = self.get_detector()[0].get_image_size() raw_data.reshape(flex.grid(image_size[1], image_size[0])) return raw_data
def read_from_stdin(): written = ext.test_read(streambuf(sys.stdin), "read") assert written == "Veni, Vidi, Vici, [ fail, eof ]", written
def exercise_read(self): self.create_file_object(mode='r') words = ext.test_read(streambuf(self.file_object), "read") assert words == "Coding, should, be, fun, [ fail, eof ]" self.file_object.close()