Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes made to make mogi and cdm run properly #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion altar/bayesian/Metropolis.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def initialize(self, application):
# get the capsule of the random number generator
rng = application.rng.rng
# set up the distribution for building the sample multiplicities
self.uniform = altar.pdf.uniform(support=(0,1), rng=rng)
self.uniform = altar.pdf.uniform(support=(1.0e-15,1), rng=rng)
# set up the distribution for the random walk displacement vectors
self.uninormal = altar.pdf.ugaussian(rng=rng)

Expand Down
2 changes: 1 addition & 1 deletion models/cdm/cdm/CDM.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def initialize(self, application):
theta = record.theta
phi = record.phi
# form the projection vectors and store them
self.los[obs, 0] = sin(theta) * cos(phi)
self.los[obs, 0] = -sin(theta) * cos(phi)
self.los[obs, 1] = sin(theta) * sin(phi)
self.los[obs, 2] = cos(theta)

Expand Down
7 changes: 3 additions & 4 deletions models/cdm/cdm/Fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ def initialize(self, model, **kwds):
libcdm.oid(source, oid)
# inform the source about the parameter layout; assumes contiguous parameter sets
libcdm.layout(source,
model.xIdx, model.dIdx,
model.openingIdx, model.aXIdx, model.omegaXIdx,
model.xIdx, model.dIdx, model.openingIdx, model.aXIdx, model.omegaXIdx,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified function variable referencing/order for consistency.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like merging two lines into one; am i missing something

model.offsetIdx)

# nothing to do
return self

Expand Down Expand Up @@ -90,7 +88,8 @@ def dataLikelihood(self, model, step):
# get the residuals
residuals = predicted.getRow(sample)
# compute the norm, and normalize it
llk = normalization - norm.eval(v=residuals, sigma_inv=cd_inv) / 2
normeval = norm.eval(v=residuals, sigma_inv=cd_inv)
llk = normalization - normeval**2.0 / 2.0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected the calculation of LLK.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

# store it
dataLLK[sample] = llk

Expand Down
17 changes: 9 additions & 8 deletions models/cdm/cdm/Native.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ def dataLikelihood(self, model, step):
samples = θ.rows
# get the parameter sets
psets = model.psets

# get the offsets of the various parameter sets
xIdx = model.xIdx
yIdx = model.yIdx
dIdx = model.dIdx
openingIdx = model.openingIdx
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing significant. Just reordered the variable for consistency.

aXIdx = model.aXIdx
aYIdx = model.aYIdx
aZIdx = model.aZIdx
omegaXIdx = model.omegaXIdx
omegaYIdx = model.omegaYIdx
omegaZIdx = model.omegaZIdx
openingIdx = model.openingIdx
offsetIdx = model.offsetIdx

# get the observations
los = model.los
oid = model.oid
Expand Down Expand Up @@ -91,9 +91,10 @@ def dataLikelihood(self, model, step):
omegaZ = parameters[omegaZIdx]

# make a source using the sample parameters
cdm = source(x=x, y=y, d=d,
cdm = source(x=x, y=y, d=d, opening=opening,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified function variable referencing/order for consistency.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with python pass-by-name, the order of arguments is not important

ax=aX, ay=aY, az=aZ, omegaX=omegaX, omegaY=omegaY, omegaZ=omegaZ,
opening=opening, v=model.nu)
v=model.nu)

# compute the expected displacement
u = cdm.displacements(locations=locations, los=los)

Expand All @@ -103,11 +104,11 @@ def dataLikelihood(self, model, step):
for obs in range(observations):
# appropriate for the corresponding dataset
u[obs] -= parameters[offsetIdx + oid[obs]]

# compute the norm of the displacements
residual = norm.eval(v=u, sigma_inv=cd_inv)
normeval = norm.eval(v=u, sigma_inv=cd_inv)
# normalize and store it as the data log likelihood
dataLLK[sample] = normalization - residual/2
dataLLK[sample] = normalization - normeval**2.0 /2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected the calculation of the LLK.


# all done
return self
Expand Down
11 changes: 6 additions & 5 deletions models/cdm/cdm/Source.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,22 @@ def displacements(self, locations, los):
# allocate space for the result
u = altar.vector(shape=len(locations))
# compute the displacements
ue, un, uv = CDM(Xf, Yf, x_src, y_src, d_src,
omegaX_src, omegaY_src, omegaZ_src, ax_src, ay_src, az_src,
opening, v)
ue, un, uv = CDM(Xf, Yf, x_src, y_src, d_src, opening,
Copy link
Author

@gracebato gracebato Apr 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 81-83: Modified function variable referencing/order for consistency.

ax_src, ay_src, az_src, omegaX_src, omegaY_src, omegaZ_src,
v)
# go through each observation location
for idx, (ux,uy,uz) in enumerate(zip(ue, un, uv)):
# project the expected displacement along LOS and store
print(ux, ' ', uy, ' ', uz)
u[idx] = ux * los[idx,0] + uy * los[idx,1] + uz * los[idx,2]

# all done
return u


# meta-methods
def __init__(self, x=x, y=y, d=d, omegaX=omegaX, omegaY=omegaY, omegaZ=omegaZ,
ax=ax, ay=ay, az=az, opening=opening, v=v, **kwds):
def __init__(self, x=x, y=y, d=d, opening=opening, ax=ax, ay=ay, az=az,
Copy link
Author

@gracebato gracebato Apr 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 95-96: Same here--modified function variable referencing/order for consistency.

omegaX=omegaX, omegaY=omegaY, omegaZ=omegaZ, v=v, **kwds):
# chain up
super().__init__(**kwds)
# store the location
Expand Down
5 changes: 3 additions & 2 deletions models/cdm/cdm/libcdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def norm(v):
return numpy.sqrt(v.dot(v))


def CDM(X, Y, X0, Y0, depth, omegaX, omegaY, omegaZ, ax, ay, az, opening, nu):
def CDM(X, Y, X0, Y0, depth, opening, ax, ay, az, omegaX, omegaY, omegaZ, nu):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here--modified function variable referencing/order for consistency.

"""
CDM
calculates the surface displacements and potency associated with a CDM
Expand Down Expand Up @@ -146,7 +146,8 @@ def CDM(X, Y, X0, Y0, depth, omegaX, omegaY, omegaZ, ax, ay, az, opening, nu):
April 2018 by Eric Gurrola
Jet Propulsion Lab/Caltech
"""

#print(depth, opening, ax, ay, az, omegaX, omegaY, omegaZ, nu)
#print(grace)
ue=0
un=0
uv=0
Expand Down
30 changes: 17 additions & 13 deletions models/cdm/examples/synthetic/cdm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3
h#!/usr/bin/env python3
# -*- python -*-
# -*- coding: utf-8 -*-
#
Expand All @@ -17,7 +17,6 @@
# my model
import altar.models.cdm


# app
class CDM(altar.application, family="altar.applications.cdm"):
"""
Expand Down Expand Up @@ -46,13 +45,13 @@ class CDM(altar.application, family="altar.applications.cdm"):
omegaX = altar.properties.float(default=0)
omegaX.doc = "the CDM rotation about the x axis"

omegaY = altar.properties.float(default=0)
omegaY = altar.properties.float(default=-45)
omegaY.doc = "the CDM rotation about the y axis"

omegaZ = altar.properties.float(default=0)
omegaZ.doc = "the CDM rotation about the z axis"

opening = altar.properties.float(default=1e-3)
opening = altar.properties.float(default=100.0)
opening.doc = "the tensile component of the Burgers vector of the dislocation"

nu = altar.properties.float(default=.25)
Expand Down Expand Up @@ -97,16 +96,16 @@ def cdm(self):
observations = len(stations)
# make a source
source = altar.models.cdm.source(
x=self.x, y=self.y, d=self.d,
x=self.x, y=self.y, d=self.d, opening=self.opening,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 99-102: Modified function variable referencing/order for consistency.

ax=self.aX, ay=self.aY, az=self.aZ,
omegaX=self.omegaX, omegaY=self.omegaY, omegaZ=self.omegaZ,
opening=self.opening, v=self.nu)
v=self.nu)

# observe all displacements from the same angle for now
theta = π/4 # the azimuthal angle
phi = π # the polar angle
# build the common projection vector
s = sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta)
s = -sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta)

# allocate a matrix to hold the projections
los = altar.matrix(shape=(observations,3))
Expand Down Expand Up @@ -134,7 +133,7 @@ def cdm(self):
# western stations
if x < 0:
# come from a different data set
observation.oid = 1
observation.oid = 0
# than
else:
# eastern stations
Expand All @@ -158,7 +157,7 @@ def cdm(self):
# go through the observations
for idx, observation in enumerate(data):
# set the covariance to a fraction of the "observed" displacement
correlation[idx,idx] = 1.0 #.01 * observation.d
correlation[idx,idx] = 0.0001 #1.0 #.01 * observation.d
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.0 m is quite large for an observation error (variance).


# all done
return data, correlation
Expand All @@ -169,11 +168,16 @@ def makeStations(self):
Create a set of station coordinate
"""
# get some help
import itertools
# build a set of points on a grid
stations = itertools.product(range(-5,6), range(-5,6))
import numpy as np
Copy link
Author

@gracebato gracebato Apr 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 171-177: I think for the synthetic case, this is better to constrain the number of points.

xx = np.linspace(-6000,6000, 15)
yy = np.linspace(-6000,6000, 15)
x,y = np.meshgrid(xx, yy)
x=x.flatten()
y = y.flatten()
stations = [tuple((y[i], x[i])) for i in range(0,len(x))]

# and return it
return tuple((x*1000, y*1000) for x,y in stations)
return stations


# bootstrap
Expand Down
17 changes: 7 additions & 10 deletions models/cdm/lib/libcdm/Source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,14 @@ displacements(gsl_matrix_view * samples, gsl_matrix * predicted) const {
auto omegaY = gsl_matrix_get(&samples->matrix, sample, _omegaYIdx);
auto omegaZ = gsl_matrix_get(&samples->matrix, sample, _omegaZIdx);


// compute the displacements
cdm(_locations,
xSrc, ySrc, dSrc,
openingSrc,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified function variable referencing/order for consistency.

aX, aY, aZ,
omegaX, omegaY, omegaZ,
openingSrc,
_nu,
disp);

// apply the location specific projection to LOS vector and dataset shift
for (auto loc=0; loc<_locations->size1; ++loc) {
// compute the components of the unit LOS vector
Expand All @@ -109,16 +107,16 @@ displacements(gsl_matrix_view * samples, gsl_matrix * predicted) const {

// get the three components of the predicted displacement for this location
auto ux = gsl_matrix_get(disp, loc, 0);
auto uy = gsl_matrix_get(disp, loc, 0);
auto ud = gsl_matrix_get(disp, loc, 0);
auto uy = gsl_matrix_get(disp, loc, 1);
auto ud = gsl_matrix_get(disp, loc, 2);

// project; don't forget {ud} is positive into the ground
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a depth value. It is the vertical component.

auto u = ux*nx + uy*ny - ud*nz;
// project
auto u = ux*nx + uy*ny + ud*nz;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a depth value and should be '+' sign.

// find the shift that corresponds to this observation
auto shift = gsl_matrix_get(&samples->matrix, sample, _offsetIdx+_oids[loc]);
// and apply it to the projected displacement
u -= shift;

// save
gsl_matrix_set(predicted, sample, loc, u);
}
Expand Down Expand Up @@ -147,7 +145,6 @@ residuals(gsl_matrix * predicted) const {
// unpack the number of samples and number of observations
auto nSamples = predicted->size1;
auto nObservations = predicted->size2;

// go through all observations
for (auto obs=0; obs < nObservations; ++obs) {
// get the corresponding measurement
Expand Down
Loading