/
interface.py
344 lines (301 loc) · 13.6 KB
/
interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# Copyright 2011-2016, Vinothan N. Manoharan, Thomas G. Dimiduk,
# Rebecca W. Perry, Jerome Fung, Ryan McGorty, Anna Wang, Solomon Barkley
#
# This file is part of HoloPy.
#
# HoloPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# HoloPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with HoloPy. If not, see <http://www.gnu.org/licenses/>.
"""
Base class for scattering theories. Implements python-based
calc_intensity and calc_holo, based on subclass's calc_field
.. moduleauthor:: Thomas G. Dimiduk <tdimiduk@physics.harvard.edu>
"""
from warnings import warn
import xarray as xr
import numpy as np
from holopy.core.holopy_object import SerializableMetaclass
from holopy.core.metadata import (
vector, illumination, update_metadata, to_vector, copy_metadata, from_flat,
dict_to_array)
from holopy.core.utils import dict_without, ensure_array
from holopy.scattering.scatterer import (
Sphere, Spheres, Spheroid, Cylinder, _expand_parameters,
_interpret_parameters)
from holopy.scattering.errors import AutoTheoryFailed, MissingParameter
from holopy.scattering.theory import Mie, Multisphere
from holopy.scattering.theory import Tmatrix
from holopy.scattering.theory.dda import DDA
def prep_schema(detector, medium_index, illum_wavelen, illum_polarization):
detector = update_metadata(
detector, medium_index, illum_wavelen, illum_polarization)
if detector.illum_wavelen is None:
raise MissingParameter("wavelength")
if detector.medium_index is None:
raise MissingParameter("medium refractive index")
if illum_polarization is not False and detector.illum_polarization is None:
raise MissingParameter("polarization")
illum_wavelen = ensure_array(detector.illum_wavelen)
illum_polarization = detector.illum_polarization
if len(illum_wavelen) > 1 or ensure_array(illum_polarization).ndim == 2:
# multiple illuminations to calculate
if illumination in illum_polarization.dims:
if isinstance(illum_wavelen, xr.DataArray):
pass
else:
if len(illum_wavelen) == 1:
illum_wavelen = illum_wavelen.repeat(
len(illum_polarization.illumination))
illum_wavelen = xr.DataArray(
illum_wavelen, dims=illumination,
coords={illumination: illum_polarization.illumination})
else:
# need to interpret illumination from detector.illum_wavelen
if not isinstance(illum_wavelen, xr.DataArray):
illum_wavelen = xr.DataArray(
illum_wavelen, dims=illumination,
coords={illumination: illum_wavelen})
illum_polarization = xr.broadcast(
illum_polarization, illum_wavelen, exclude=[vector])[0]
if illumination in detector.dims:
detector = detector.sel(
illumination=detector.illumination[0], drop=True)
detector = update_metadata(
detector, illum_wavelen=illum_wavelen,
illum_polarization=illum_polarization)
return detector
def interpret_theory(scatterer, theory='auto'):
if isinstance(theory, str) and theory == 'auto':
theory = determine_default_theory_for(scatterer.guess)
if isinstance(theory, SerializableMetaclass):
theory = theory()
return theory
def finalize(detector, result):
if not hasattr(detector, 'flat'):
result = from_flat(result)
return copy_metadata(detector, result, do_coords=False)
# Some comments on why `determine_default_theory_for` exists, rather than each
# Scatterer class knowing what a good default theory is.
# The problem is that the theories (Mie etc) import Sphere to see if
# the theory can handle the scatterer, in the _can_handle method and
# others. Worse, since the DDA theory calls an external DDA library
# with specially-defined DDA objects, the DDA theory has a switch statement
# for basically every holopy scatterer. So right now the scatterers can't
# have a default theory and/or valid theory attr, as this causes a dependency
# loop.
def determine_default_theory_for(scatterer):
if isinstance(scatterer, Sphere):
theory = Mie()
elif isinstance(scatterer, Spheres):
if all([np.isscalar(scat.r) for scat in scatterer.scatterers]):
theory = Multisphere()
else:
warn("HoloPy's multisphere theory can't handle coated spheres." +
"Using Mie theory.")
theory = Mie()
elif isinstance(scatterer, Spheroid) or isinstance(scatterer, Cylinder):
theory = Tmatrix()
elif DDA()._can_handle(scatterer):
theory = DDA()
else:
raise AutoTheoryFailed(scatterer)
return theory
def calc_intensity(detector, scatterer, medium_index=None, illum_wavelen=None,
illum_polarization=None, theory='auto'):
"""
Calculate intensity from the scattered field at a set of locations
Parameters
----------
detector : xarray object
The detector points and calculation metadata used to calculate
the intensity.
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_intensity will error out and
ask you to specify a theory
Returns
-------
inten : xarray.DataArray
scattered intensity
"""
field = calc_field(detector, scatterer, medium_index=medium_index,
illum_wavelen=illum_wavelen,
illum_polarization=illum_polarization, theory=theory)
intensity = (np.abs(field.sel(vector=['x', 'y']))**2).sum(dim=vector)
return finalize(detector, intensity)
def calc_holo(detector, scatterer, medium_index=None, illum_wavelen=None,
illum_polarization=None, theory='auto', scaling=1.0):
"""
Calculate hologram formed by interference between scattered
fields and a reference wave
Parameters
----------
detector : xarray object
The detector points and calculation metadata used to calculate
the hologram.
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_holo` will error out and
ask you to specify a theory
scaling : scaling value (alpha) for amplitude of reference wave
Returns
-------
holo : xarray.DataArray
Calculated hologram from the given distribution of spheres
"""
theory = interpret_theory(scatterer, theory)
uschema = prep_schema(
detector, medium_index, illum_wavelen, illum_polarization)
# Massage scaling into an xarray with color channels if needed
scaling = dict(_expand_parameters({'alpha': scaling}.items()))
for key in scaling.keys():
if hasattr(scaling[key], 'guess'):
scaling[key] = scaling[key].guess
scaling = _interpret_parameters(scaling)['alpha']
scaling = dict_to_array(detector, scaling)
scattered_field = theory.calculate_scattered_field(
scatterer.guess, uschema)
reference_field = uschema.illum_polarization
holo = scattered_field_to_hologram(
scattered_field * scaling, reference_field)
return finalize(uschema, holo)
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 calc_scat_matrix(detector, scatterer, medium_index=None, illum_wavelen=None,
theory='auto'):
"""
Compute farfield scattering matrices for scatterer
Parameters
----------
detector : xarray object
The detector points and calculation metadata used to calculate
the scattering matrices.
scatterer : :class:`holopy.scattering.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_scat_matrix` will error out
and ask you to specify a theory
Returns
-------
scat_matr : :class:`.Marray`
Scattering matrices at specified positions
"""
theory = interpret_theory(scatterer, theory)
uschema = prep_schema(
detector, medium_index=medium_index, illum_wavelen=illum_wavelen,
illum_polarization=False)
result = theory.calculate_scattering_matrix(scatterer.guess, uschema)
return finalize(uschema, result)
def calc_field(detector, scatterer, medium_index=None, illum_wavelen=None,
illum_polarization=None, theory='auto'):
"""
Calculate the scattered fields from a scatterer illuminated by
a reference wave.
Parameters
----------
detector : xarray object
The detector points and calculation metadata used to calculate
the scattered fields.
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_field` will error out and
ask you to specify a theory
Returns
-------
e_field : :class:`.Vector` object
Calculated hologram from the given distribution of spheres
"""
theory = interpret_theory(scatterer, theory)
uschema = prep_schema(
detector, medium_index=medium_index, illum_wavelen=illum_wavelen,
illum_polarization=illum_polarization)
result = theory.calculate_scattered_field(scatterer.guess, uschema)
return finalize(uschema, result)
# this is pulled out separate from the calc_holo method because
# occasionally you want to turn prepared e_fields into holograms directly
def scattered_field_to_hologram(scat, ref):
"""
Calculate a hologram from an E-field
Parameters
----------
scat : :class:`.VectorGrid`
The scattered (object) field
ref : xarray[vector]]
The reference field
detector_normal : (float, float, float)
Vector normal to the detector the hologram should be measured at
(defaults to z hat, a detector in the x, y plane)
"""
total_field = scat + ref
holo = (np.abs(total_field.sel(vector=['x', 'y']))**2).sum(dim=vector)
return holo