def GetSpiceLeaAnisotropyTransforms( anisotropyDirAzimuth=216. * I3Units.deg, # direction of ice tilt (perp. to flow) magnitudeAlongDir=0.04, # magnitude of ice anisotropy along tilt magnitudePerpToDir=-0.08): # magnitude of ice anisotropy along flow """ Returns the direction-dependent absorption length scaling function and a pre- and post-scattering direction transformation used in the ice anisotropy description of Spice-Lea. """ absLenScaling = I3CLSimScalarFieldAnisotropyAbsLenScaling( anisotropyDirAzimuth=anisotropyDirAzimuth, magnitudeAlongDir=magnitudeAlongDir, magnitudePerpToDir=magnitudePerpToDir) # The matrix A in Dima's description is the distortion # in the frame of (n1,n2,n3), where n1 points in the # direction of the anisotropy and n2 points into the # direction perpendicular to it. k1 = numpy.exp(magnitudeAlongDir ) # coefficient of anisotropy parallel to "tilt" direction k2 = numpy.exp( magnitudePerpToDir ) # coefficient of anisotropy perpendicular to "tilt" direction kz = 1. / (k1 * k2) # a normalizing factor for the z direction A = numpy.array([[k1, 0., 0.], [0., k2, 0.], [0., 0., kz]]) # The orthogonal matrix T transforms a vector into # the (n1,n2,n3) coordinate system. (It is a simple # rotation around the z-axis.) sa = numpy.sin(anisotropyDirAzimuth) ca = numpy.cos(anisotropyDirAzimuth) T = numpy.array([[ca, sa, 0.], [-sa, ca, 0.], [0., 0., 1.]]) # Now combine the transformations: rotate into the # new coordinate system first, apply A and rotate back. # This is applied before the photon is rotated into # the new direction. # (read this from right to left) Cpre = numpy.dot(numpy.dot(T.T, A), T) preScatterTransform = I3CLSimVectorTransformMatrix( I3Matrix(Cpre), renormalize=True) # after applying Cpost, make it a unit vector again # Now combine the transformations: rotate into the # new coordinate system first, apply A^-1 and rotate back. # This is applied after the photon has been rotated into # its new direction. # (read this from right to left) Cpost = numpy.dot(numpy.dot(T.T, numpy.linalg.inv(A)), T) postScatterTransform = I3CLSimVectorTransformMatrix( I3Matrix(Cpost), renormalize=True) # after applying Cpost, make it a unit vector again return (absLenScaling, preScatterTransform, postScatterTransform)
def testFromNumpy(self): self.assertRaises(TypeError, I3Matrix, 1) # 1-D arrays fail self.assertRaises(ValueError, I3Matrix, numpy.array([0., 1.])) # > 2-D arrays fail self.assertRaises(ValueError, I3Matrix, numpy.ones((3, 3, 3))) # integer arrays fail self.assertRaises(TypeError, I3Matrix, numpy.eye(3, dtype=numpy.int32)) # as do single-precision arrays self.assertRaises(TypeError, I3Matrix, numpy.eye(3, dtype=numpy.float32)) # and compound types self.assertRaises( TypeError, I3Matrix, numpy.zeros((1, 1), dtype=numpy.dtype([('foo', numpy.float64), ('bar', numpy.float64)]))) # strided arrays are not handled strider = numpy.eye(3)[::2] self.assertNotEqual(strider.__array_interface__['strides'], None) self.assertRaises(ValueError, I3Matrix, strider) # I3Matrix is a copy, not a view orig = numpy.zeros((3, 3)) mat = I3Matrix(orig) view = numpy.asarray(mat) self.assert_((view == orig).all()) view += 1 self.assert_((orig == 0).all()) self.assert_((view == 1).all())
def testArrayInitialization(self): """ I3Matrix can be initialized with an arbitrary value """ m = I3Matrix(3, 2, numpy.pi) a = numpy.asarray(m) self.assert_((a == numpy.pi).all())
def testAsArray(self): """ I3Matrix can be viewed through numpy.asarray() """ m = I3Matrix(3, 2) a = numpy.asarray(m) self.assertEquals(a.shape, (3, 2)) self.assertEquals(a.dtype, numpy.float64)
def get_default_perturbation(): """ Get the default ice model variations imported from https://github.com/UTA-REST/multisim :returns: a tuple (parametrization, distribution) for use with icecube.snowstorm.Perturber """ import numpy as np from icecube.dataclasses import I3Matrix from icecube.snowstorm import MultivariateNormal from .PlusModeParametrization import PlusModeParametrization amp_sigmas = np.asarray([0.00500100, 0.03900780, 0.04500900, 0.17903581, 0.07101420, 0.30306061, 0.14502901, 0.09501900, 0.16103221, 0.13302661, 0.15703141, 0.13302661]) phase_sigmas = np.asarray([0.00000001, 0.01664937, 0.02708014, 0.43171273, 0.02351273, 2.33565571, 0.16767628, 0.05414841, 0.31355088, 0.04227052, 0.27955606, 4.02237848]) modes_to_shift = np.arange(12) variance = np.concatenate((amp_sigmas,phase_sigmas))**2 return PlusModeParametrization(modes_to_shift), MultivariateNormal(I3Matrix(np.diag(variance)), [0.]*variance.size)
def testRoundTrip(self): """ I3Matrix serializes and deserializes properly. """ fname = 'i3matrix_test.i3' orig = numpy.pi * numpy.ones((3, 3)) frame = icetray.I3Frame() frame['foo'] = I3Matrix(orig) f = dataio.I3File(fname, 'w') f.push(frame) f.close() frame = dataio.I3File(fname).pop_frame() view = numpy.asarray(frame['foo']) self.assertEquals(view.shape, orig.shape) self.assert_((view == orig).all()) os.unlink(fname)
def GetIceTiltZShift( tiltDirAzimuth=225. * I3Units.deg, tiltDirectory=expandvars("$I3_BUILD/clsim/resources/ice/TILT_data/"), detectorCenterDepth=1948.07 * I3Units.m): distance_from_origin_in_tilt_dir = numpy.loadtxt( tiltDirectory + "/tilt.par", unpack=True)[1] * I3Units.m tilt_dat = numpy.loadtxt(tiltDirectory + "/tilt.dat", unpack=True) zcoords = (detectorCenterDepth - tilt_dat[0])[::-1] zshift = [] for i in range(len(distance_from_origin_in_tilt_dir)): zshift.append(tilt_dat[i + 1][::-1]) zshift = numpy.array(zshift) return I3CLSimScalarFieldIceTiltZShift( distancesFromOriginAlongTilt=distance_from_origin_in_tilt_dir, zCoordinates=zcoords, zCorrections=I3Matrix(zshift), directionOfTiltAzimuth=tiltDirAzimuth)
def custom_uncorrelated_variation(modes_to_shift, amp_sigmas, phase_sigmas, **kwargs): """Create a custom uncorrelated fourier component perturbation Get custom ice model variations based on: https://github.com/UTA-REST/multisim Parameters ---------- modes_to_shift : list of int A list of (zero-based) mode numbers to shift. amp_sigmas : list of float The sigmas for the amplitude shifts. phase_sigmas : list of float The sigmas for the phase shifts. **kwargs Arbitrary keyword arguments. Returns ------- tuple (parametrization, distribution) Returns a tuple of a parameterization and a distribution which can be used with the icecube.snowstorm.Perturber. """ modes_to_shift = np.asarray(modes_to_shift) amp_sigmas = np.asarray(amp_sigmas) phase_sigmas = np.asarray(phase_sigmas) assert phase_sigmas.size == modes_to_shift.size assert phase_sigmas.size == amp_sigmas.size variance = np.concatenate((amp_sigmas, phase_sigmas))**2 parametrization = PlusModeParametrization(modes_to_shift) distribution = MultivariateNormal(I3Matrix(np.diag(variance)), [0.] * variance.size) return parametrization, distribution
def factory(): # NB: m leaves scope when the function returns m = I3Matrix(3, 2) a = numpy.asarray(m) return (a, id(m))