def __init__(self, modelpath, light_model, aperture_light, profile_model, schwsetname, schwmodelname, storage_2d_m0, storage_2d_m2, storage_2d_m4, storage_3d, storage_2d_losvd, fitdensity2d, fitdensity3d, observation, binned_data_m2, binned_data_m4, dfgrid, max_iterations=1000, regularization=None, postfix=""): self.modelpath = modelpath self.light_model = light_model self.profile_model = profile_model self.schwsetname = schwsetname self.schwmodelname = schwmodelname self.storage_2d_m0 = storage_2d_m0 self.storage_2d_m2 = storage_2d_m2 self.storage_2d_m4 = storage_2d_m4 self.storage_3d = storage_3d self.storage_2d_losvd = storage_2d_losvd self.aperture_light = aperture_light self.binned_data_m2 = binned_data_m2 self.binned_data_m4 = binned_data_m4 #self.storage_2d_binned = storage_2d_binned self.fitdensity2d = fitdensity2d self.fitdensity3d = fitdensity3d self.observation = observation self.dfgrid = dfgrid self.max_iterations = max_iterations self.regularization = regularization #self.regularization_delta = regularization_delta #self.use_jeans = use_jeans #self.jeans_fraction = jeans_fraction self.dirname = os.path.join(self.modelpath, "schw", self.schwsetname, self.schwmodelname) self.logger = logging.getLogger("gd.schw.solution.likelihood") self.postfix = postfix
from mab.constants import * from mab.astrounits import * from numpy import * from kaplot import * import mab.parallelize import mab.gd.logging as logging import scipy.ndimage import mab.cvsfile import scipy.optimize.nnls kpc_to_km = (1 * KPC).asNumber(KM) s_to_gyr = (S / GYR).asNumber() logger = logging.getLogger("gd.schw.uv") class UVGridder(object): def __init__(self, xmin, xmax, ymin, ymax, nx, ny): self.xmin = xmin self.xmax = xmax self.ymin = ymin self.ymax = ymax self.nx = nx self.ny = ny self.resize = (xmin, ymin), (xmax, ymax) def __call__(self, x, y): return self.histogram(x, y) def histogram(self, x, y): data, _, _ = histogram2d(x,
# -*- coding: utf-8 -*- from numpy import * import os import mab.gd.logging as logging import scipy.interpolate import mab.utils.numpy logger = logging.getLogger("gd.schw.derived") class CommandDerivedProfiles(object): def __init__(self, profile_calculators, profile_values): self.profile_calculators = profile_calculators self.profile_values = profile_values def run(self, args, opts, scope): for profile_calculator, profile_value in zip(self.profile_calculators, self.profile_values): values = profile_calculator.calculate() profile_value.set_values(values) profile_value.save() class ProfileAperture(object): def __init__(self, aperture, name="aperture", label="nolabel"): aperture.load() self.xmin = aperture.aperture_rcenters_kpc[0] self.xmax = aperture.aperture_rcenters_kpc[-1] self.aperture = aperture self.label = label self.name = name
from numpy import * import mab.parallelize import mab.utils.numpy import os from mab.constants import * from mab.astrounits import * import mab.gd.logging as logging kpc_to_km = (1*KPC).asNumber(KM) sec_in_gyr = (1*GYR).asNumber(S) logger = logging.getLogger("gd.schw.stackel") class OrbitLibrary(object): def __init__(self, dirname, orbit_integrator, initial_conditions, xy_gridder, Norbits=200, postfix=""): self.dirname = dirname self.orbit_integrator = orbit_integrator self.initial_conditions = initial_conditions self.xy_gridder = xy_gridder self.Norbits = Norbits grid = self.initial_conditions.grid #self.moments = [0, 1, 2] # 3 moments self.momentgrid = mab.utils.numpy.mmapzeros((grid.gridx.N, grid.gridy.N, grid.gridz.N, 5, xy_gridder.nx, xy_gridder.ny)) self.filename = os.path.join(dirname, "momentgrid" + postfix +".npy") def save(self): logger.info("saving momentgrid to: %s" % self.filename)
# -*- coding: utf-8 -*- import mab.gd.logging as logging from numpy import * import sys import numpy import mab.gd.gdfast from kaplot import * logger = logging.getLogger("gd.schw.tri") class PlotOutput(object): def __init__(self, output): self.output = output def run(self, args, opts, scope): self.output.load() class Output(object): def __init__(self, filename): self.filename = filename def load(self): self.orbitreader = mab.gd.gdfast.Orblib(self.filename) filename = self.filename + "-densities.npy" if 0: logger.info("reading: %s" % self.filename) self.orbitreader.read() grid = zeros( (self.orbitreader.noI1 * self.orbitreader.noI2 *
import itertools from numpy import * import os import mab.gd.logging as logging try: import h5py except: pass from kaplot import * logger = logging.getLogger("gd.gadget") class GadgetObservation(object): def __init__(self, galaxy, galaxy_host, orbit, working_directory, cores): self.galaxy = galaxy self.galaxy_host = galaxy_host self.orbit = orbit self.working_directory = working_directory self.cores = cores def run(self, args, kwargs, scope): oldcwd = os.getcwd() try: def do(i): logger.info("changing working directory to: %s" % self.working_directory) os.chdir(self.working_directory) filename = os.path.join("output", "snapshot_%03d.hdf5" % i)
import mab.gd.logging as logging logger = logging.getLogger("gd.utils.templatefile") class TemplateFile(object): def __init__(self, input, output, **kwargs): self.input = input self.output = output self.kwargs = kwargs for name, value in kwargs.items(): setattr(self, name, value) def run(self, args, opts, scope): self.save() def save(self): template_data = file(self.input).read() data = template_data.format(**self.kwargs) logger.info("writing to file: %s" % self.output) f = open(self.output, "w") f.write(data) f.close()
# -*- coding: utf-8 -*- import os import mab.gd.gdfast import numpy from numpy import * import mab.gd.schw.aperture import bz2 import mab.gd.logging as logging logger = logging.getLogger("gd.schw.output") scaling_factor = 1#0.999999918453/0.840580815266 #1.1345025156398363/0.961596185691/1.01356943378 #/3.79757705745/0.250000999817/0.88366506055 class MomentsIntrinsicSpherical(object): def __init__(self, modelpath, schwsetname, schwmodelname, xmin, xmax, dfgrid, nr, f=lambda x: log10(x)): self.modelpath = modelpath self.schwsetname = schwsetname self.schwmodelname = schwmodelname self.xmin = xmin self.xmax = xmax self.dfgrid = dfgrid self.nr = nr self.dirname = os.path.join(self.modelpath, "schw", self.schwsetname, self.schwmodelname) self.binner = mab.gd.gdfast.Binner(self.xmin, self.xmax) self.n = 0 self.filename = os.path.join(self.dirname, "intermediate", "moments3d.npy") self.x = (arange(self.nr) + 0.5 )/ (self.nr) * (xmax - xmin) + xmin self.xborders = (arange(self.nr+1.))/ (self.nr) * (xmax - xmin) + xmin self.firststore = True self.f = f def init(self):
# -*- coding: utf-8 -*- from mab.functions import gaussian import emcee import mab.gd.logging as logging logger = logging.getLogger("mab.models") from numpy import * import sys from kaplot import * import scipy class TwoGaussian(object): def __init__(self, mu1, sigma1, mu2, sigma2, ratio, values): self.mu1 = mu1 self.sigma1 = sigma1 self.mu2 = mu2 self.sigma2 = sigma2 self.ratio = ratio self.values = numpy.array(values) self.x0 = [mu1, sigma1, mu2, sigma2, ratio] logger.info("values length: %d" % len(values)) def __len__(self): return 5 def __call__(self, x): #print ">>", p mu1, sigma1, mu2, sigma2, ratio = x ratio = 10**(ratio) #ratio is f1/f2 f1 = ratio / (1. + ratio)
# -*- coding: utf-8 -*- import mab.gd.gdfast import os import numpy from numpy import * import os import pyublas import mab.gd.logging as logging logger = logging.getLogger("gd.schw.aperture") class AperturePrepare(object): def __init__(self, aperture): self.aperture = aperture def run(self, args, opts, scope): self.aperture.init() self.aperture.process() self.aperture.save() class AperturePolarBase(object): def __init__(self, modelpath, nphi, postfix, light_model, ellipticity, position_angle): self.modelpath = modelpath self.nphi = nphi self.postfix = postfix self.light_model = light_model self.ellipticity = ellipticity self.position_angle = position_angle
# -*- coding: utf-8 -*- from mab.gd.schw.cmdutils import * import sys import mab.gd.potential import mab.gd.logging as logging logger = logging.getLogger("gd.utils.mass_concentration") def c_maccio(Mvir, h=0.72): return 10**(1.020 - 0.109 * (log10(Mvir*h) - 12)) class Maccio(object): def __init__(self, mass_range, scale_range, dirname): self.mass_range = mass_range self.scale_range = scale_range self.dirname = dirname self.filename_concentration = os.path.join(self.dirname, "concentration.npy") self.filename_M200 = os.path.join(self.dirname, "M200.npy") self.filename_pdf = os.path.join(self.dirname, "mass-concentration-pdf.npy") self.h = 0.72# hubble param def run(self, args, opts, scope): self.calculate() self.save() def save(self): logger.info("saving concentration to: %s" % self.filename_concentration) save(self.filename_concentration, self.c_grid) logger.info("saving M200 to: %s" % self.filename_M200) save(self.filename_M200, self.M200_grid) logger.info("saving pdf to: %s" % self.filename_pdf)
# -*- coding: utf-8 -*- import itertools from numpy import * import os import mab.gd.logging as logging import mab.gd.gdfast import string import mab.parallelize #from numpy import nan nan = numpy.nan inf = numpy.inf logger = logging.getLogger("gd.utils") from numpy import * def multisum(a, axes): correction = 0 for axis in axes: a = numpy.sum(a, axis=axis-correction) correction += 1 return a class ParameterEstimates(object): def __init__(self, parameterset): self.parameterset = parameterset def run(self, args, opts, scope): self.parameterset.load() p = numpy.exp(self.parameterset.probability_grid) for i, param in enumerate(self.parameterset.parameter_range_list): print param.name axes = range(self.parameterset.dimension)
# -*- coding: utf-8 -*- import os import mab.gd.logging as logging logger = logging.getLogger("gd.schw.solution_discrete2") from numpy import * import numpy import scipy from mab.gd import gdfast_schw from kaplot import * import pyublas class LosvdPerBin(object): def __init__(self, solution, storage_losvd, aperture, binned_data_m2): self.solution = solution self.storage_losvd = storage_losvd self.aperture = aperture self.binned_data_m2 = binned_data_m2 def load(self): self.solution.load() self.storage_losvd.load() self.binned_data_m2.load() self.binned_data_m2.observation.load() self.binned_data_m2.aperture.load() dirname = os.path.join(self.binned_data_m2.modelpath, "schw", "aperture") cached_filename = os.path.join( dirname, "observation_group" + self.binned_data_m2.postfix + "_cache" + ".npy")
from numpy import * import numpy import mab.gd.logging as logging import mab.parallelize from scipy import special import pickle import mab.utils.numpy import os import sys from kaplot import * store_cache = True logger = logging.getLogger("gd.jeans.jeansmodel") def h3(x): return x**3-3*x def h4(x): return x**4-6*x**2+3 def h6(x): return x**6-15*x**4+45*x**2-15 fac3 = 1*2*3 fac4 = fac3*4 fac6 = fac3 * 4 * 5 * 6 def makeresize(parameterset, i, j): return (parameterset.ranges_org[i][0], parameterset.ranges_org[j][0]), (parameterset.ranges_org[i][1], parameterset.ranges_org[j][1]) class JeansModel(object):
# -*- coding: utf-8 -*- from numpy import * from mab.gd.gdfast import * import mab.gd.gdfast import pyublas import mab.gd.logging as logging logger = logging.getLogger("gd.schw.grid") class Grid2I_Block_Inside(object): def __init__(self, light_model, profile_model, logrmin, logrmax, n_I1, n_I2, dither=1, _issubgrid=False): self.light_model = light_model self.profile_model = profile_model self.logrmin = log10(light_model.arcsec_to_kpc(10**logrmin)) self.logrmax = log10(light_model.arcsec_to_kpc(10**logrmax)) self.n_I1 = n_I1 self.n_I2 = n_I2 self.logrs = (arange(0, n_I1, dtype=float) + 0.5) / (n_I1) * ( self.logrmax - self.logrmin) + self.logrmin self.logr_borders = (arange(0, n_I1 + 1, dtype=float)) / (n_I1) * ( self.logrmax - self.logrmin) + self.logrmin self.Es = profile_model.potentialr(10**self.logrs)
# -*- coding: utf-8 -*- from kaplot import * from mab.binningtools import bingrid, binrange import mab.gd.logging as logging import scipy.stats logger = logging.getLogger("gd.schw.plot") class OrbitLibraryLosvdPlot(object): def __init__(self, storage_2d): self.storage_2d = storage_2d def run(self, args, opts, scope): self.storage_2d.load() #OrbitLibraryLosvdPlot nE, nL = self.storage_2d.dfgrid.n_I1, self.storage_2d.dfgrid.n_I2 print self.storage_2d.losvds.shape #mozaic( class ProjectionMatrix(object): def __init__(self, projection_matrix): self.projection_matrix = projection_matrix def run(self, args, opts, scope): self.projection_matrix.load() I = self.projection_matrix.matrices[0] I = log10(I) I[I<-5] = -5 bins = I.shape[0] print bins resize = (0, scope["schw_logr_min"]), (bins-1, scope["schw_logr_max"])
# -*- coding: utf-8 -*- from numpy import * from kaplot import * import os import mab.cvsfile import mab.gd.logging as logging import scipy logger = logging.getLogger("gd.membership") def gaussian_(x, mu, sigma): return 1 / (sigma * sqrt(2 * pi)) * exp(-(mu - x)**2 / (2 * sigma**2)) class MemberRatiosZero(object): def load(self): pass def ratios(self, R1, R2): return 1.0, 0.0 def ratios1(self, R): return 1. class MemberRatios(object): def __init__(self, model, foreground_model, galaxy_model, light_profile, aperture): self.model = model self.foreground_model = foreground_model
import ConfigParser as configparser import sys import os import numpy from math import * import mab.gd.logging as logging import math import collections import mab.ordereddict import glob logger = logging.getLogger("gd.configure") enable_logging = True enable_imports = True module_exts = [".py"] imported = [] def recursive_import(basedirname, dirname): if not enable_imports: return if dirname in imported: return imported.append(dirname) filenames = glob.glob(os.path.join(dirname, "*")) for filename in filenames: if os.path.isdir(filename): #print "DIR", filename recursive_import(basedirname, filename)
import mab.gd.logging as logging logger = logging.getLogger("gd.commands") class SubMean(object): def __init__(self, input_observation, output_observation, vmean=None): self.input_observation = input_observation self.output_observation = output_observation self.vmean = vmean def run(self, args, opts, scope): stars = self.stars = self.input_observation.load() if self.vmean is None: vmean = stars.vlos_helio.mean() else: vmean = self.vmean logger.info("subtracting mean velocity: %f" % vmean) stars.vlos = stars.vlos_helio - vmean stars[0].attributes.append("vlos") self.output_observation.save(self.stars) class Aperture(object): def __init__(self, aperture): self.aperture = aperture def run(self, args, opts, scope): self.aperture.init() self.aperture.process() self.aperture.save()
# -*- coding: utf-8 -*- from numpy import * import mab.gd.gdfast import mab.utils.numpy import mab.random import os import bz2 from mab.constants import * from mab.astrounits import * import numpy import sys import mab.gd.logging as logging kpc_to_km = (1*KPC).asNumber(KM) logger = logging.getLogger("gd.schw.ic") class OrbitLibraryCommand(object): def __init__(self, schwmodel, modelpath, schwsetname, schwmodelname, load_compressed): self.schwmodel = schwmodel self.modelpath = modelpath self.schwsetname = schwsetname self.schwmodelname = schwmodelname self.load_compressed = load_compressed self.dirname = os.path.join(modelpath, "schw", schwsetname, schwmodelname) def run(self, args, opts, scope): filename_check = os.path.join(self.dirname, "finished.orblib") if os.path.exists(filename_check): logger.info("skipping creating orbit library (already finished)") return nE = len(self.schwmodel.dfgrid.Es)
# -*- coding: utf-8 -*- import numpy import mab.gd.logging as logging logger = logging.getLogger("gd.optimize.fitting") from kaplot import * class Fitting(object): def __init__(self, fitter, data, xattr, sigma_xattr, filename): self.fitter = fitter self.data = data self.xattr = xattr self.sigma_xattr = sigma_xattr self.filename = filename def run(self, args, opts, scope): obj = self.data.load() xtest = x = getattr(obj, self.xattr) sigma_x = getattr(obj, self.sigma_xattr) print x print sigma_x #dsa model = self.fitter.get_model() if 1: self.fitter.fit(x, sigma_x) self.x = [param.get() for param in model.parameters] for param in model.parameters: print param.name, param.get() if 0: for x, e in zip(x, sigma_x): y = model.logL(x, e)
# -*- coding: utf-8 -*- from numpy import * import mab.gd.logging as logging import cvxopt logger = logging.getLogger("gd.schw.regularization") class RegularizationQP_toiso(object): def __init__(self, modelpath, regularization_delta, dfgrid, light_model, orbitweight_calculator, scale=8): self.modelpath = modelpath self.regularization_delta = regularization_delta self.dfgrid = dfgrid self.light_model = light_model self.orbitweight_calculator = orbitweight_calculator self.scale = scale def assemble_system(self): weights = self.orbitweight_calculator.load().T #print weights.shape #Erborders = self.dfgrid.r_borders #density_Etarget = array([self.light_model.light_profile.cumdensityr((Erborders[i]), (Erborders[i+1]), M=1.) for i in range(self.dfgrid.n_I1)]) n_nodes = self.dfgrid.get_dof() nI1nodes = self.dfgrid.n_I1 nI2nodes = self.dfgrid.n_I2
import mab.cvsfile import mab.gd.logging as logging import numpy import os import math from numpy import * import random as pyrandom import mab.random import mab.parallelize logger = logging.getLogger("gd.galaxysampling") class GalaxySampling_SphericalNonRotating(object): def __init__(self, **kwargs): pass def export_csv(filename, records, seperator=","): #import pdb #pdb.set_trace() #N = len(records.dtype) f = open(filename, "w") #for name in records.dtype.names: print >> f, seperator.join(records.dtype.names) for record in records: values = [repr(value) for value in record] print >> f, seperator.join(values)
# -*- coding: utf-8 -*- import os import mab.gd.logging as logging import emcee from numpy import * import numpy import scipy from mab.gd import gdfast_schw from kaplot import * logger = logging.getLogger("gd.schw.solution2") class Dummy(object): pass dummy = Dummy() def lnprob(u): x = exp(u) x /= sum(x) logp = sum([k.logp(x) for k in dummy.opts]) #print logp return logp def domcmc(x, opts): dummy.opts = opts N = len(x)
# -*- coding: utf-8 -*- import os import subprocess import mab.gd.logging as logging logger = logging.getLogger("gd.utils.run") class Run(object): def __init__(self, command, working_directory=None): self.command = command self.working_directory = working_directory def run(self, args, opts, scope): oldcwd = os.getcwd() try: if self.working_directory: logger.info("changing working directory to: %s" % self.working_directory) os.chdir(self.working_directory) logger.info("running %s" % self.command) os.system(self.command) finally: os.chdir(oldcwd) class Batch(object): def __init__(self, commands): self.commands = commands
# -*- coding: utf-8 -*- import itertools from numpy import * import os import mab.gd.logging as logging import snapshot import h5py from kaplot import * logger = logging.getLogger("gd.nbody.gadget") class GadgetObservation(object): def __init__(self, galaxy, galaxy_host, orbit, working_directory, cores): self.galaxy = galaxy self.galaxy_host = galaxy_host self.orbit = orbit self.working_directory = working_directory self.cores = cores def run(self, args, kwargs, scope): oldcwd = os.getcwd() try: def do(i): logger.info("changing working directory to: %s" % self.working_directory) os.chdir(self.working_directory) filename = os.path.join("output", "snapshot_%03d.hdf5" % i) #if not os.path.exists(filename):
# -*- coding: utf-8 -*- from scipy.optimize import fsolve, fmin, brentq from scipy.integrate import quad from numpy import * from mab.gd import gdfast from mab.gd.jeans.jeans import Jeans, JeansAnisotropicConstant import mab.gd.logging as logging import scipy logger = logging.getLogger("gd.schw.galaxy") class ProfileModel1C(object): def __init__(self, light_profile): self.light_profile = light_profile self.light_profile_fast = self.light_profile.fast() self.fast_ = gdfast.ProfileModel1C(self.light_profile_fast) def vmax(self): return sqrt(-2 * self.potentialr(1e-5)) def fast(self): return self.fast_ def jeans(self): return def total_mass(self): return self.light_profile.total_mass() #def fast(self): # return None #gdfast.Galaxy1C_constant_anisotropy(self.stellar_profilef, self.distance, self.beta)
# -*- coding: utf-8 -*- from __future__ import absolute_import import ConfigParser as configparser import sys import os import numpy from math import * import mab.gd.logging as logging import math import collections import mab.ordereddict import glob logger = logging.getLogger("gd.utils.iniscope") enable_logging = True enable_imports = True module_exts = [".py"] imported = [] def recursive_import(basedirname, dirname): if not enable_imports: return if dirname in imported: return imported.append(dirname) filenames = glob.glob(os.path.join(dirname, "*")) for filename in filenames: if os.path.isdir(filename):
import mab.gd.schw.grid import mab.utils.progressbar import mab.gd.logging as logging import mab.gd.configure import mab.gd.schw.configure import mab.gd.jeans.configure import mab.gd.configure import mab.gd.logging as logging import mab.logging as logging_new import mab.utils.iniscope #import mab.utils.numpy #import itertools #import mab.parallelize #import bz2 logger = logging.getLogger("utils.program") class Dummy(object): pass class Extra(object): pass def load(self, name): return mab.gd.configure.createobj_fromini(self.ini, name, self.scope) from mab.utils.iniscope import IniScope def program2(kaplot=False, **kwargs): from optparse import OptionParser if kaplot:
# -*- coding: utf-8 -*- from numpy import * import numpy import os import mab.gd.logging as logging logger = logging.getLogger("gd.photometry") import mab.astrometry class FakePhotometry(object): def __init__(self, light_model, axis_ratio, N, aperture, dirname, name="photometry"): self.light_model = light_model self.axis_ratio = axis_ratio self.N = N self.aperture = aperture self.dirname = dirname self.name = name self.filename_xy = os.path.join(self.dirname, self.name + "_xy.npy") self.filename_grid = os.path.join(self.dirname, self.name + "_grid.npy") def run(self, args, opts, scope): self.x = [] self.y = [] logger.info("aperture: %dx%d (size=%d)" %