def setUp(self, *args): self.numtests = 1000 self.precision = 1e-8 self.f_lower = 10. # create some component masses to work with self.m1 = numpy.random.uniform(1., 100., size=self.numtests) self.m2 = numpy.random.uniform(1., 100., size=self.numtests) # create some spins to work with spin_angledist = distributions.UniformSolidAngle() rvals = spin_angledist.rvs(size=self.numtests) self.spin1_polar = rvals['theta'] self.spin1_az = rvals['phi'] self.spin1_amp = numpy.random.uniform(0., 1., size=self.numtests) rvals = spin_angledist.rvs(size=self.numtests) self.spin2_polar = rvals['theta'] self.spin2_az = rvals['phi'] self.spin2_amp = numpy.random.uniform(0., 1., size=self.numtests) # calculate derived parameters from each self.mp = conversions.primary_mass(self.m1, self.m2) self.ms = conversions.secondary_mass(self.m1, self.m2) self.mtotal = conversions.mtotal_from_mass1_mass2(self.m1, self.m2) self.q = conversions.q_from_mass1_mass2(self.m1, self.m2) self.invq = conversions.invq_from_mass1_mass2(self.m1, self.m2) self.mchirp = conversions.mchirp_from_mass1_mass2(self.m1, self.m2) self.eta = conversions.eta_from_mass1_mass2(self.m1, self.m2) self.tau0 = conversions.tau0_from_mtotal_eta(self.mtotal, self.eta, self.f_lower) self.tau3 = conversions.tau3_from_mtotal_eta(self.mtotal, self.eta, self.f_lower) self.spin1x, self.spin1y, self.spin1z = \ coordinates.spherical_to_cartesian(self.spin1_amp, self.spin1_az, self.spin1_polar) self.spin2x, self.spin2y, self.spin2z = \ coordinates.spherical_to_cartesian(self.spin2_amp, self.spin2_az, self.spin2_polar) self.effective_spin = conversions.chi_eff(self.m1, self.m2, self.spin1z, self.spin2z) self.chi_p = conversions.chi_p(self.m1, self.m2, self.spin1x, self.spin1y, self.spin2x, self.spin2y) self.primary_spinx = conversions.primary_spin(self.m1, self.m2, self.spin1x, self.spin2x) self.primary_spiny = conversions.primary_spin(self.m1, self.m2, self.spin1y, self.spin2y) self.primary_spinz = conversions.primary_spin(self.m1, self.m2, self.spin1z, self.spin2z) self.secondary_spinx = conversions.secondary_spin(self.m1, self.m2, self.spin1x, self.spin2x) self.secondary_spiny = conversions.secondary_spin(self.m1, self.m2, self.spin1y, self.spin2y) self.secondary_spinz = conversions.secondary_spin(self.m1, self.m2, self.spin1z, self.spin2z)
def setUp(self, *args): self.numtests = 1000 self.precision = 1e-8 self.f_lower = 10. # create some component masses to work with self.m1 = numpy.random.uniform(1., 100., size=self.numtests) self.m2 = numpy.random.uniform(1., 100., size=self.numtests) # create some spins to work with spin_angledist = distributions.UniformSolidAngle() rvals = spin_angledist.rvs(size=self.numtests) self.spin1_polar = rvals['theta'] self.spin1_az = rvals['phi'] self.spin1_amp = numpy.random.uniform(0., 1., size=self.numtests) rvals = spin_angledist.rvs(size=self.numtests) self.spin2_polar = rvals['theta'] self.spin2_az = rvals['phi'] self.spin2_amp = numpy.random.uniform(0., 1., size=self.numtests) # calculate derived parameters from each self.mp = conversions.primary_mass(self.m1, self.m2) self.ms = conversions.secondary_mass(self.m1, self.m2) self.mtotal = conversions.mtotal_from_mass1_mass2(self.m1, self.m2) self.q = conversions.q_from_mass1_mass2(self.m1, self.m2) self.invq = conversions.invq_from_mass1_mass2(self.m1, self.m2) self.mchirp = conversions.mchirp_from_mass1_mass2(self.m1, self.m2) self.eta = conversions.eta_from_mass1_mass2(self.m1, self.m2) self.tau0 = conversions.tau0_from_mtotal_eta(self.mtotal, self.eta, self.f_lower) self.tau3 = conversions.tau3_from_mtotal_eta(self.mtotal, self.eta, self.f_lower) self.spin1x, self.spin1y, self.spin1z = \ coordinates.spherical_to_cartesian(self.spin1_amp, self.spin1_az, self.spin1_polar) self.spin2x, self.spin2y, self.spin2z = \ coordinates.spherical_to_cartesian(self.spin2_amp, self.spin2_az, self.spin2_polar) self.effective_spin = conversions.chi_eff(self.m1, self.m2, self.spin1z, self.spin2z) self.primary_spinx = conversions.primary_spin(self.m1, self.m2, self.spin1x, self.spin2x) self.primary_spiny = conversions.primary_spin(self.m1, self.m2, self.spin1y, self.spin2y) self.primary_spinz = conversions.primary_spin(self.m1, self.m2, self.spin1z, self.spin2z) self.secondary_spinx = conversions.secondary_spin(self.m1, self.m2, self.spin1x, self.spin2x) self.secondary_spiny = conversions.secondary_spin(self.m1, self.m2, self.spin1y, self.spin2y) self.secondary_spinz = conversions.secondary_spin(self.m1, self.m2, self.spin1z, self.spin2z)
def transform(self, maps): """ This function transforms from spherical to cartesian spins. Parameters ---------- maps : a mapping object Examples -------- Convert a dict of numpy.array: >>> import numpy >>> from pycbc import transforms >>> t = transforms.SphericalSpin1ToCartesianSpin1() >>> t.transform({'spin1_a': numpy.array([0.1]), 'spin1_azimuthal': numpy.array([0.1]), 'spin1_polar': numpy.array([0.1])}) {'spin1_a': array([ 0.1]), 'spin1_azimuthal': array([ 0.1]), 'spin1_polar': array([ 0.1]), 'spin2x': array([ 0.00993347]), 'spin2y': array([ 0.00099667]), 'spin2z': array([ 0.09950042])} Returns ------- out : dict A dict with key as parameter name and value as numpy.array or float of transformed values. """ a, az, po = self._inputs data = coordinates.spherical_to_cartesian(maps[a], maps[az], maps[po]) out = {param: val for param, val in zip(self._outputs, data)} return self.format_output(maps, out)
def transform(self, maps): """ This function transforms from spherical to cartesian spins. Parameters ---------- maps : a mapping object Examples -------- Convert a dict of numpy.array: >>> import numpy >>> from pycbc import transforms >>> t = transforms.SphericalSpin1ToCartesianSpin1() >>> t.transform({'spin1_a': numpy.array([0.1]), 'spin1_azimuthal': numpy.array([0.1]), 'spin1_polar': numpy.array([0.1])}) {'spin1_a': array([ 0.1]), 'spin1_azimuthal': array([ 0.1]), 'spin1_polar': array([ 0.1]), 'spin2x': array([ 0.00993347]), 'spin2y': array([ 0.00099667]), 'spin2z': array([ 0.09950042])} Returns ------- out : dict A dict with key as parameter name and value as numpy.array or float of transformed values. """ a, az, po = self._inputs data = coordinates.spherical_to_cartesian(maps[a], maps[az], maps[po]) out = {param : val for param, val in zip(self._outputs, data)} return self.format_output(maps, out)
def generator_spin_spherical_to_spin_cartesian(generator): """Converts spherical spin magnitude and angles in `current_params`, to cartesian component spins. """ x, y, z = coordinates.spherical_to_cartesian( generator.current_params["spin1_a"], generator.current_params["spin1_azimuthal"], generator.current_params["spin1_polar"]) generator.current_params["spin1x"] = x generator.current_params["spin1y"] = y generator.current_params["spin1z"] = z x, y, z = coordinates.spherical_to_cartesian( generator.current_params["spin2_a"], generator.current_params["spin2_azimuthal"], generator.current_params["spin2_polar"]) generator.current_params["spin2x"] = x generator.current_params["spin2y"] = y generator.current_params["spin2z"] = z
azimuthal_bounds=(phi_low,phi_high)) # Now we can take a random variable sample from that distribution. # In this case we want 50000 samples. solid_angle_samples = uniform_solid_angle_distribution.rvs(size=10000) # Make a spin 1 magnitude since solid angle is only 2 dimensions and we need a # 3rd dimension for a 3D plot that we make later on. spin_mag = numpy.ndarray(shape=(10000), dtype=float) for i in range(0,10000): spin_mag[i] = 1. # Use pycbc.coordinates as co. Use spherical_to_cartesian function to # convert from spherical polar coordinates to cartesian coordinates. spinx, spiny, spinz = co.spherical_to_cartesian(spin_mag, solid_angle_samples['phi'], solid_angle_samples['theta']) # Plot the spherical distribution of spins to make sure that we # distributed across the surface of a sphere. fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(111, projection='3d') ax.scatter(spinx, spiny, spinz, s=1) ax.set_xlabel('Spin X Axis') ax.set_ylabel('Spin Y Axis') ax.set_zlabel('Spin Z Axis') plt.show()
polar_bounds=(theta_low, theta_high), azimuthal_bounds=(phi_low, phi_high)) # Now we can take a random variable sample from that distribution. # In this case we want 50000 samples. solid_angle_samples = uniform_solid_angle_distribution.rvs(size=10000) # Make a spin 1 magnitude since solid angle is only 2 dimensions and we need a # 3rd dimension for a 3D plot that we make later on. spin_mag = numpy.ndarray(shape=(10000), dtype=float) for i in range(0, 10000): spin_mag[i] = 1. # Use pycbc.coordinates as co. Use spherical_to_cartesian function to # convert from spherical polar coordinates to cartesian coordinates. spinx, spiny, spinz = co.spherical_to_cartesian(spin_mag, solid_angle_samples['phi'], solid_angle_samples['theta']) # Plot the spherical distribution of spins to make sure that we # distributed across the surface of a sphere. fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111, projection='3d') ax.scatter(spinx, spiny, spinz, s=1) ax.set_xlabel('Spin X Axis') ax.set_ylabel('Spin Y Axis') ax.set_zlabel('Spin Z Axis') plt.show()