def test_sin(): var = sc.Variable([Dim.X], values=np.array([0.0, math.pi]), unit=sc.units.rad) expected = sc.Variable([Dim.X], values=np.array([math.sin(0.0), math.sin(math.pi)]), unit=sc.units.dimensionless) assert sc.sin(var) == expected
def reflectometry_q(wavelength: sc.Variable, theta: sc.Variable) -> sc.Variable: """ Compute the Q vector from the theta angle computed as the difference between gamma and omega. Note that this is identical the 'normal' Q defined in scippneutron, except that the `theta` angle is given as an input instead of `two_theta`. """ dtype = _elem_dtype(wavelength) c = (4 * pi).astype(dtype) return c * sc.sin(theta.astype(dtype, copy=False)) / wavelength
def test_sin_out(): var = sc.Variable([Dim.X], values=np.array([0.0, math.pi]), unit=sc.units.rad) expected = sc.Variable([Dim.X], values=np.array([math.sin(0.0), math.sin(math.pi)]), unit=sc.units.dimensionless) out = sc.sin(x=var, out=var) assert var == expected assert out == expected
def illumination_correction(beam_size: sc.Variable, sample_size: sc.Variable, theta: sc.Variable) -> sc.Variable: """ Compute the factor by which the intensity should be multiplied to account for the scattering geometry, where the beam is Gaussian in shape. :param beam_size: Width of incident beam. :param sample_size: Width of sample in the dimension of the beam. :param theta: Incident angle. """ beam_on_sample = beam_size / sc.sin(theta) fwhm_to_std = 2 * np.sqrt(2 * np.log(2)) return sc.erf(sample_size / beam_on_sample * fwhm_to_std)
def illumination_of_sample(beam_size: sc.Variable, sample_size: sc.Variable, theta: sc.Variable) -> sc.Variable: """ Determine the illumination of the sample by the beam and therefore the size of its illuminated length. :param beam_size: Width of incident beam. :param sample_size: Width of sample in the dimension of the beam. :param theta: Incident angle. """ beam_on_sample = beam_size / sc.sin(theta) if ((sc.mean(beam_on_sample)) > sample_size).value: beam_on_sample = sc.broadcast(sample_size, shape=theta.shape, dims=theta.dims) return beam_on_sample
def get_detector_properties(ws, source_pos, sample_pos, spectrum_dim, advanced_geometry=False): if not advanced_geometry: return (get_detector_pos(ws, spectrum_dim), None, None) spec_info = ws.spectrumInfo() det_info = ws.detectorInfo() comp_info = ws.componentInfo() nspec = len(spec_info) det_rot = np.zeros([nspec, 3, 3]) det_bbox = np.zeros([nspec, 3]) if sample_pos is not None and source_pos is not None: total_detectors = spec_info.detectorCount() act_beam = (sample_pos - source_pos) rot = _rot_from_vectors(act_beam, sc.vector(value=[0, 0, 1])) inv_rot = _rot_from_vectors(sc.vector(value=[0, 0, 1]), act_beam) pos_d = sc.Dataset() # Create empty to hold position info for all spectra detectors pos_d["x"] = sc.zeros(dims=["detector"], shape=[total_detectors], unit=sc.units.m) pos_d["y"] = sc.zeros_like(pos_d["x"]) pos_d["z"] = sc.zeros_like(pos_d["x"]) pos_d.coords[spectrum_dim] = sc.array(dims=["detector"], values=np.empty(total_detectors)) spectrum_values = pos_d.coords[spectrum_dim].values x_values = pos_d["x"].values y_values = pos_d["y"].values z_values = pos_d["z"].values idx = 0 for i, spec in enumerate(spec_info): if spec.hasDetectors: definition = spec_info.getSpectrumDefinition(i) n_dets = len(definition) quats = [] bboxes = [] for j in range(n_dets): det_idx = definition[j][0] p = det_info.position(det_idx) r = det_info.rotation(det_idx) spectrum_values[idx] = i x_values[idx] = p.X() y_values[idx] = p.Y() z_values[idx] = p.Z() idx += 1 quats.append( np.array([r.imagI(), r.imagJ(), r.imagK(), r.real()])) if comp_info.hasValidShape(det_idx): s = comp_info.shape(det_idx) bboxes.append(s.getBoundingBox().width()) det_rot[ i, :] = sc.geometry.rotation_matrix_from_quaternion_coeffs( np.mean(quats, axis=0)) det_bbox[i, :] = np.sum(bboxes, axis=0) rot_pos = rot * sc.geometry.position(pos_d["x"].data, pos_d["y"].data, pos_d["z"].data) _to_spherical(rot_pos, pos_d) averaged = sc.groupby(pos_d, spectrum_dim, bins=sc.Variable(dims=[spectrum_dim], values=np.arange( -0.5, len(spec_info) + 0.5, 1.0))).mean("detector") sign = averaged["p-sign"].data / sc.abs(averaged["p-sign"].data) averaged["p"] = sign * ( (np.pi * sc.units.rad) - averaged["p-delta"].data) averaged["x"] = averaged["r"].data * sc.sin( averaged["t"].data) * sc.cos(averaged["p"].data) averaged["y"] = averaged["r"].data * sc.sin( averaged["t"].data) * sc.sin(averaged["p"].data) averaged["z"] = averaged["r"].data * sc.cos(averaged["t"].data) pos = sc.geometry.position(averaged["x"].data, averaged["y"].data, averaged["z"].data) return (inv_rot * pos, sc.spatial.linear_transforms(dims=[spectrum_dim], values=det_rot), sc.vectors(dims=[spectrum_dim], values=det_bbox, unit=sc.units.m)) else: pos = np.zeros([nspec, 3]) for i, spec in enumerate(spec_info): if spec.hasDetectors: definition = spec_info.getSpectrumDefinition(i) n_dets = len(definition) vec3s = [] quats = [] bboxes = [] for j in range(n_dets): det_idx = definition[j][0] p = det_info.position(det_idx) r = det_info.rotation(det_idx) vec3s.append([p.X(), p.Y(), p.Z()]) quats.append( np.array([r.imagI(), r.imagJ(), r.imagK(), r.real()])) if comp_info.hasValidShape(det_idx): s = comp_info.shape(det_idx) bboxes.append(s.getBoundingBox().width()) pos[i, :] = np.mean(vec3s, axis=0) det_rot[ i, :] = sc.geometry.rotation_matrix_from_quaternion_coeffs( np.mean(quats, axis=0)) det_bbox[i, :] = np.sum(bboxes, axis=0) else: pos[i, :] = [np.nan, np.nan, np.nan] det_rot[i, :] = [np.nan, np.nan, np.nan, np.nan] det_bbox[i, :] = [np.nan, np.nan, np.nan] return (sc.vectors(dims=[spectrum_dim], values=pos, unit=sc.units.m), sc.spatial.linear_transforms(dims=[spectrum_dim], values=det_rot), sc.vectors( dims=[spectrum_dim], values=det_bbox, unit=sc.units.m, ))