def test_prep_schema(): sch_f = detector_grid(shape=5, spacing=1) sch_x = detector_grid( shape=5, spacing=1, extra_dims={'illumination': ['red', 'green', 'blue']}) wl_f = 0.5 wl_l = [0.5, 0.6, 0.7] wl_d = OrderedDict([('red', 0.5), ('green', 0.6), ('blue', 0.7)]) wl_x = xr.DataArray([0.5, 0.6, 0.7], dims='illumination', coords={'illumination': ['red', 'green', 'blue']}) pol_f = (0, 1) pol_d = OrderedDict([('red', (0, 1)), ('green', (1, 0)), ('blue', (0.5, 0.5))]) pol_x = xr.concat( [to_vector((0, 1)), to_vector((1, 0)), to_vector((0.5, 0.5))], wl_x.illumination) all_in = prep_schema(sch_x, 1, wl_x, pol_x) assert_obj_close(prep_schema(sch_x, 1, wl_d, pol_d), all_in) assert_obj_close(prep_schema(sch_x, 1, wl_l, pol_d), all_in) assert_obj_close(prep_schema(sch_f, 1, wl_x, pol_x), all_in)
def calc_cross_sections(scatterer, medium_index=None, illum_wavelen=None, illum_polarization=None, theory='auto'): """ Calculate scattering, absorption, and extinction cross sections, and asymmetry parameter <cos \theta>. Parameters ---------- scatterer : :class:`.scatterer` object (possibly composite) scatterer for which to compute scattering medium_index : float or complex Refractive index of the medium in which the scatter is imbedded illum_wavelen : float or ndarray(float) Wavelength of illumination light. If illum_wavelen is an array result will add a dimension and have all wavelengths theory : :class:`.theory` object (optional) Scattering theory object to use for the calculation. This is optional if there is a clear choice of theory for your scatterer. If there is not a clear choice, `calc_cross_sections` will error out and ask you to specify a theory Returns ------- cross_sections : array (4) Dimensional scattering, absorption, and extinction cross sections, and <cos theta> """ theory = interpret_theory(scatterer, theory) cross_section = theory.calculate_cross_sections( scatterer=scatterer.guess, medium_wavevec=2*np.pi/(illum_wavelen/medium_index), medium_index=medium_index, illum_polarization=to_vector(illum_polarization)) return cross_section
def test_raw_fields(): sp = Sphere(r=.5, n=1.6, center=(10, 10, 5)) wavelen = .66 index = 1.33 pol = to_vector((0, 1)) sch = detector_grid(3, .1) wavevec=2*np.pi/(wavelen/index) pos = Mie._transform_to_desired_coordinates( sch, (10, 10, 5), wavevec=wavevec) rf = Mie()._raw_fields( pos, sp, medium_wavevec=wavevec, medium_index=index, illum_polarization=pol) assert_allclose(rf, [[(0.0015606995428858754-0.0019143174710834162j), (-0.0003949071974815011-0.0024154494284017187j), (-0.002044525390662322-0.001302770747742109j), (-0.0003949071974815009-0.002415449428401719j), (-0.002055824337886397-0.0012853546864338861j), (-0.00230285180386436+0.000678693819245102j), (-0.0020445253906623225-0.0013027707477421095j), (-0.0023028518038643603+0.0006786938192451026j), (-0.0010011090105680883+0.0021552249454706712j)], [(-0.0010507058414478587+0.0036584360153097306j), (0.0020621595919700776+0.003210547679920805j), (0.0037794246074692407+0.000585690417403587j), (0.0020542215584045407+0.0031619947065620246j), (0.0037426710578253295+0.000527040269055415j), (0.002871631795307833-0.002470099566862354j), (0.0036968090916832948+0.0005330478443315597j), (0.002824872178181336-0.0024563186266035124j), (2.261564613123139e-06-0.003751168280253104j)], [(0.0010724312167657794+0.0039152445632936j), (0.003651474601303447+0.0017688083711547462j), (0.003740131549224567-0.001566271371618957j), (0.0036883581831347947+0.0017866751223785315j), (0.0037648739662344477-0.001614943488355339j), (0.0012643679510138835-0.003894481935619062j), (0.003816460764514863-0.0015982360934887314j), (0.0012772696647997395-0.0039342215472070105j), (-0.0021320123934202356-0.0035427449839031066j)]])
def load_image(inf, spacing=None, medium_index=None, illum_wavelen=None, illum_polarization=None, normals=None, noise_sd=None, channel=None, name=None): """ Load data or results Parameters ---------- inf : string File to load. spacing : float or (float, float) (optional) pixel size of images in each dimension - assumes square pixels if single value. set equal to 1 if not passed in and issues warning. medium_index : float (optional) refractive index of the medium illum_wavelen : float (optional) wavelength (in vacuum) of illuminating light illum_polarization : (float, float) (optional) (x, y) polarization vector of the illuminating light noise_sd : float (optional) noise level in the image, normalized to image intensity channel : int or tuple of ints (optional) number(s) of channel to load for a color image (in general 0=red, 1=green, 2=blue) name : str (optional) name to assign the xr.DataArray object resulting from load_image Returns ------- obj : xarray.DataArray representation of the image with associated metadata """ if normals is not None: raise ValueError(NORMALS_DEPRECATION_MESSAGE) if name is None: name = os.path.splitext(os.path.split(inf)[-1])[0] with open(inf, 'rb') as pi_raw: pi = pilimage.open(pi_raw) arr = np.asarray(pi).astype('d') try: if isinstance(yaml.safe_load(pi.tag[270][0]), dict): warnings.warn( "Metadata detected but ignored. Use hp.load to read it.") except (AttributeError, KeyError): pass extra_dims = None if channel is None: if arr.ndim > 2: raise BadImage( 'Not a greyscale image. You must specify which channel(s) to use' ) elif arr.ndim == 2: if not channel == 'all': warnings.warn("Not a color image (channel number ignored)") pass else: # color image with specified channel(s) if channel == 'all': channel = range(arr.shape[2]) channel = ensure_array(channel) if channel.max() >= arr.shape[2]: raise LoadError( filename, "The image doesn't have a channel number {0}".format( channel.max())) else: arr = arr[:, :, channel].squeeze() if len(channel) > 1: # multiple channels. increase output dimensionality if channel.max() <= 2: channel = [['red', 'green', 'blue'][c] for c in channel] extra_dims = {illumination: channel} if illum_wavelen is not None and not isinstance( illum_wavelen, dict) and len( ensure_array(illum_wavelen)) == len(channel): illum_wavelen = xr.DataArray(ensure_array(illum_wavelen), dims=illumination, coords=extra_dims) if not isinstance(illum_polarization, dict) and np.array( illum_polarization).ndim == 2: pol_index = xr.DataArray(channel, dims=illumination, name=illumination) illum_polarization = xr.concat( [to_vector(pol) for pol in illum_polarization], pol_index) image = data_grid(arr, spacing=spacing, medium_index=medium_index, illum_wavelen=illum_wavelen, illum_polarization=illum_polarization, noise_sd=noise_sd, name=name, extra_dims=extra_dims) return image