use of ffx.crystal.ReflectionSpline in project ffx by mjschnie.
the class SigmaAMinimize method setWEstimate.
private void setWEstimate() {
// generate initial w estimate
ReflectionSpline spline = new ReflectionSpline(reflectionList, refinementData.nbins);
int[] nmean = new int[refinementData.nbins];
for (int i = 0; i < refinementData.nbins; i++) {
nmean[i] = 0;
}
double mean = 0.0, tot = 0.0;
double[][] fc = refinementData.fctot;
double[][] fo = refinementData.fsigf;
for (HKL ih : reflectionList.hkllist) {
int i = ih.index();
if (ih.allowed() == 0.0 || Double.isNaN(fc[i][0]) || Double.isNaN(fo[i][0])) {
continue;
}
double s2 = Crystal.invressq(crystal, ih);
double epsc = ih.epsilonc();
ComplexNumber fct = new ComplexNumber(fc[i][0], fc[i][1]);
double ecscale = spline.f(s2, refinementData.fcesq);
double eoscale = spline.f(s2, refinementData.foesq);
double ec = fct.times(sqrt(ecscale)).abs();
double eo = fo[i][0] * sqrt(eoscale);
double wi = pow(eo - ec, 2.0) / epsc;
nmean[spline.i1()]++;
tot++;
x[spline.i1() + refinementData.nbins] += (wi - x[spline.i1() + refinementData.nbins]) / nmean[spline.i1()];
mean += (wi - mean) / tot;
}
logger.info(String.format(" Starting mean w: %8.3f", mean));
logger.info(String.format(" Starting w scaling: %8.3f", 1.0 / mean));
for (int i = 0; i < refinementData.nbins; i++) {
x[i] -= x[i + refinementData.nbins];
x[i] *= scaling[i];
scaling[i + refinementData.nbins] = 1.0 / mean;
x[i + refinementData.nbins] *= scaling[i + refinementData.nbins];
}
sigmaAEnergy.setScaling(scaling);
}
use of ffx.crystal.ReflectionSpline in project ffx by mjschnie.
the class CrystalStats method getSigmaA.
/**
* simply return the current sigmaA value
*
* @return sigmaA
*/
public double getSigmaA() {
double sum = 0.0;
int nhkl = 0;
ReflectionSpline sigmaaspline = new ReflectionSpline(reflectionlist, refinementdata.sigmaa.length);
for (HKL ih : reflectionlist.hkllist) {
int i = ih.index();
// ignored cases
if (Double.isNaN(fc[i][0]) || Double.isNaN(fo[i][0]) || fo[i][1] <= 0.0) {
continue;
}
// spline setup
double ss = Crystal.invressq(crystal, ih);
double fh = spline.f(ss, refinementdata.spline);
double sa = sigmaaspline.f(ss, refinementdata.sigmaa);
nhkl++;
sum += (sa - sum) / nhkl;
}
return sum;
}
use of ffx.crystal.ReflectionSpline in project ffx by mjschnie.
the class CrystalStats method getSigmaW.
/**
* simply return the current sigmaW value
*
* @return sigmaW
*/
public double getSigmaW() {
double sum = 0.0;
int nhkl = 0;
ReflectionSpline sigmaaspline = new ReflectionSpline(reflectionlist, refinementdata.sigmaa.length);
for (HKL ih : reflectionlist.hkllist) {
int i = ih.index();
// ignored cases
if (Double.isNaN(fc[i][0]) || Double.isNaN(fo[i][0]) || fo[i][1] <= 0.0) {
continue;
}
// spline setup
double ss = Crystal.invressq(crystal, ih);
double fh = spline.f(ss, refinementdata.spline);
double wa = sigmaaspline.f(ss, refinementdata.sigmaw);
nhkl++;
sum += (wa - sum) / nhkl;
}
return sum;
}
use of ffx.crystal.ReflectionSpline in project ffx by mjschnie.
the class CrystalStats method printRStats.
/**
* print R factors and associated statistics in a binned fashion
*/
public void printRStats() {
double[][] res = new double[n][2];
double[] nhkl = new double[n + 1];
double[][] rb = new double[n + 1][2];
double[][] sumfo = new double[n + 1][2];
double[][] s = new double[n + 1][4];
double numer = 0.0;
double denom = 0.0;
double sumall = 0.0;
double sumfoall = 0.0;
ReflectionSpline sigmaaspline = new ReflectionSpline(reflectionlist, refinementdata.sigmaa.length);
for (int i = 0; i < n; i++) {
res[i][0] = Double.NEGATIVE_INFINITY;
res[i][1] = Double.POSITIVE_INFINITY;
}
for (HKL ih : reflectionlist.hkllist) {
int i = ih.index();
int b = ih.bin();
// ignored cases
if (Double.isNaN(fc[i][0]) || Double.isNaN(fo[i][0]) || fo[i][1] <= 0.0) {
continue;
}
// spline setup
double ss = Crystal.invressq(crystal, ih);
double fh = spline.f(ss, refinementdata.spline);
double sa = sigmaaspline.f(ss, refinementdata.sigmaa);
double wa = sigmaaspline.f(ss, refinementdata.sigmaw);
double eoscale = sigmaaspline.f(ss, refinementdata.foesq);
// determine res limits of each bin
double rs = Crystal.res(crystal, ih);
if (rs > res[b][0]) {
res[b][0] = rs;
}
if (rs < res[b][1]) {
res[b][1] = rs;
}
ComplexNumber c = new ComplexNumber(fc[i][0], fc[i][1]);
numer = abs(abs(fo[i][0]) - fh * abs(c.abs()));
denom = abs(fo[i][0]);
if (refinementdata.isFreeR(i)) {
rb[b][1] += numer;
sumfo[b][1] += denom;
rb[n][1] += numer;
sumfo[n][1] += denom;
sumall += numer;
sumfoall += denom;
} else {
rb[b][0] += numer;
sumfo[b][0] += denom;
rb[n][0] += numer;
sumfo[n][0] += denom;
sumall += numer;
sumfoall += denom;
}
nhkl[b]++;
nhkl[n]++;
s[b][0] += (sa - s[b][0]) / nhkl[b];
s[b][1] += (wa - s[b][1]) / nhkl[b];
s[b][2] += ((wa / Math.sqrt(eoscale)) - s[b][2]) / nhkl[b];
s[b][3] += (fomphi[i][0] - s[b][3]) / nhkl[b];
s[n][0] += (sa - s[n][0]) / nhkl[n];
s[n][1] += (wa - s[n][1]) / nhkl[n];
s[n][2] += ((wa / Math.sqrt(eoscale)) - s[n][2]) / nhkl[n];
s[n][3] += (fomphi[i][0] - s[n][3]) / nhkl[n];
}
StringBuilder sb = new StringBuilder(String.format("\n %15s | %7s | %7s | %7s | %7s | %7s | %7s\n", "Res. Range", " R", "Rfree", "s", "w(E)", "w(F)", "FOM"));
for (int i = 0; i < n; i++) {
sb.append(String.format(" %7.3f %7.3f | ", res[i][0], res[i][1]));
sb.append(String.format("%7.2f | %7.2f | %7.4f | %7.4f | %7.2f | %7.4f\n", (rb[i][0] / sumfo[i][0]) * 100.0, (rb[i][1] / sumfo[i][1]) * 100.0, s[i][0], s[i][1], s[i][2], s[i][3]));
}
sb.append(String.format(" %7.3f %7.3f | ", res[0][0], res[n - 1][1]));
sb.append(String.format("%7.2f | %7.2f | %7.4f | %7.4f | %7.2f | %7.4f\n", (rb[n][0] / sumfo[n][0]) * 100.0, (rb[n][1] / sumfo[n][1]) * 100.0, s[n][0], s[n][1], s[n][2], s[n][3]));
sb.append(" s and w are analagous to D and sum_wc");
reslow = res[0][0];
reshigh = res[n - 1][1];
highreslow = res[n - 1][0];
highreshigh = res[n - 1][1];
r = (rb[n][0] / sumfo[n][0]) * 100.0;
rfree = (rb[n][1] / sumfo[n][1]) * 100.0;
rall = (sumall / sumfoall) * 100.0;
highr = (rb[n - 1][0] / sumfo[n - 1][0]) * 100.0;
highrfree = (rb[n - 1][1] / sumfo[n - 1][1]) * 100.0;
if (print) {
logger.info(sb.toString());
}
}
use of ffx.crystal.ReflectionSpline in project ffx by mjschnie.
the class MTZWriter method write.
/**
* <p>
* write</p>
*/
public void write() {
ByteOrder byteOrder = ByteOrder.nativeOrder();
FileOutputStream fileOutputStream;
DataOutputStream dataOutputStream;
try {
if (logger.isLoggable(Level.INFO)) {
StringBuilder sb = new StringBuilder();
sb.append(format("\n Writing MTZ HKL file: \"%s\"\n", fileName));
logger.info(sb.toString());
}
fileOutputStream = new FileOutputStream(fileName);
dataOutputStream = new DataOutputStream(fileOutputStream);
byte[] bytes = new byte[80];
int offset = 0;
int writeLen = 0;
int iMapData;
float fMapData;
// Header.
StringBuilder sb = new StringBuilder();
sb.append("MTZ ");
dataOutputStream.writeBytes(sb.toString());
// Header offset.
int headerOffset = n * nCol + 21;
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
byteBuffer.order(byteOrder).putInt(headerOffset);
// 0x4441 for LE, 0x1111 for BE
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
iMapData = 0x4441;
} else {
iMapData = 0x1111;
}
byteBuffer.order(byteOrder).putInt(iMapData);
dataOutputStream.write(bytes, offset, 8);
sb = new StringBuilder();
sb.append(" ");
sb.setLength(68);
dataOutputStream.writeBytes(sb.toString());
// Data.
Vector<String> colname = new Vector<>(nCol);
char[] colType = new char[nCol];
double[] res = new double[2];
res[0] = Double.POSITIVE_INFINITY;
res[1] = Double.NEGATIVE_INFINITY;
float[][] colMinMax = new float[nCol][2];
for (int i = 0; i < nCol; i++) {
colMinMax[i][0] = Float.POSITIVE_INFINITY;
colMinMax[i][1] = Float.NEGATIVE_INFINITY;
}
ReflectionSpline sigmaASpline = new ReflectionSpline(reflectionList, refinementData.sigmaa.length);
int col = 0;
colname.add("H");
colType[col++] = 'H';
colname.add("K");
colType[col++] = 'H';
colname.add("L");
colType[col++] = 'H';
writeLen += 12;
if (mtzType != MTZType.FCONLY) {
colname.add("FO");
colType[col++] = 'F';
colname.add("SIGFO");
colType[col++] = 'Q';
colname.add("FreeR");
colType[col++] = 'I';
writeLen += 12;
}
if (mtzType != MTZType.DATAONLY) {
colname.add("Fs");
colType[col++] = 'F';
colname.add("PHIFs");
colType[col++] = 'P';
colname.add("Fc");
colType[col++] = 'F';
colname.add("PHIFc");
colType[col++] = 'P';
writeLen += 16;
}
if (mtzType == MTZType.ALL) {
colname.add("FOM");
colType[col++] = 'W';
colname.add("PHIW");
colType[col++] = 'P';
colname.add("SigmaAs");
colType[col++] = 'F';
colname.add("SigmaAw");
colType[col++] = 'Q';
colname.add("FWT");
colType[col++] = 'F';
colname.add("PHWT");
colType[col++] = 'P';
colname.add("DELFWT");
colType[col++] = 'F';
colname.add("PHDELWT");
colType[col++] = 'P';
writeLen += 32;
}
for (HKL ih : reflectionList.hkllist) {
col = 0;
int i = ih.index();
// Skip the 0 0 0 reflection.
if (ih.h() == 0 && ih.k() == 0 && ih.l() == 0) {
continue;
}
double ss = Crystal.invressq(crystal, ih);
res[0] = min(ss, res[0]);
res[1] = max(ss, res[1]);
// HKL first (3)
fMapData = ih.h();
colMinMax[col][0] = min(fMapData, colMinMax[0][0]);
colMinMax[col][1] = max(fMapData, colMinMax[0][1]);
byteBuffer.rewind();
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = ih.k();
colMinMax[col][0] = min(fMapData, colMinMax[1][0]);
colMinMax[col][1] = max(fMapData, colMinMax[1][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = ih.l();
colMinMax[col][0] = min(fMapData, colMinMax[2][0]);
colMinMax[col][1] = max(fMapData, colMinMax[2][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
if (mtzType != MTZType.FCONLY) {
// F/sigF (2)
fMapData = (float) refinementData.getF(i);
if (!isNaN(fMapData)) {
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
}
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = (float) refinementData.getSigF(i);
if (!isNaN(fMapData)) {
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
}
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
// Free R (1)
fMapData = (float) refinementData.getFreeR(i);
if (!isNaN(fMapData)) {
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
}
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
}
if (mtzType == MTZType.FCONLY) {
// Fs (2)
fMapData = (float) refinementData.fsF(i);
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = (float) toDegrees(refinementData.fsPhi(i));
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
// Fc (unscaled!) (2)
fMapData = (float) refinementData.fcF(i);
if (!isNaN(fMapData)) {
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
}
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = (float) Math.toDegrees(refinementData.fcPhi(i));
if (!isNaN(fMapData)) {
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
}
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
}
if (mtzType == MTZType.ALL) {
// Fs (2)
fMapData = (float) refinementData.fsF(i);
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = (float) toDegrees(refinementData.fsPhi(i));
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
// Fctot (2)
fMapData = (float) refinementData.fcTotF(i);
if (!isNaN(fMapData)) {
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
}
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = (float) toDegrees(refinementData.fcTotPhi(i));
if (!isNaN(fMapData)) {
colMinMax[col][0] = Math.min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = Math.max(fMapData, colMinMax[col][1]);
}
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
// FOM/phase (2)
fMapData = (float) refinementData.fomphi[i][0];
if (!isNaN(fMapData)) {
colMinMax[col][0] = Math.min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = Math.max(fMapData, colMinMax[col][1]);
}
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = (float) toDegrees(refinementData.fomphi[i][1]);
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
// Spline setup.
double fh = spline.f(ss, refinementData.spline);
double sa = sigmaASpline.f(ss, refinementData.sigmaa);
double wa = sigmaASpline.f(ss, refinementData.sigmaw);
// sigmaA/w (2)
fMapData = (float) sa;
if (!isNaN(fMapData)) {
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
}
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = (float) wa;
if (!isNaN(fMapData)) {
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
}
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
// Map coeffs (4).
fMapData = (float) refinementData.FoFc2F(i);
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = (float) toDegrees(refinementData.FoFc2Phi(i));
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = (float) refinementData.foFc1F(i);
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
fMapData = (float) toDegrees(refinementData.foFc1Phi(i));
colMinMax[col][0] = min(fMapData, colMinMax[col][0]);
colMinMax[col][1] = max(fMapData, colMinMax[col][1]);
byteBuffer.order(byteOrder).putFloat(fMapData);
col++;
}
dataOutputStream.write(bytes, offset, writeLen);
}
// Header.
sb = new StringBuilder();
sb.append("VERS MTZ:V1.1 ");
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss ");
sb = new StringBuilder();
sb.append("TITLE FFX output: " + sdf.format(now));
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append(String.format("NCOL %8d %12d %8d", nCol, n, 0));
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append("SORT 0 0 0 0 0 ");
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
char cdata = spaceGroup.shortName.charAt(0);
if (cdata == 'H') {
cdata = 'R';
}
sb.append(String.format("SYMINF %3d %2d %c %5d %22s %5s", spaceGroup.getNumberOfSymOps(), spaceGroup.numPrimitiveSymEquiv, cdata, spaceGroup.number, "'" + spaceGroup.shortName + "'", spaceGroup.pointGroupName));
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
for (int i = 0; i < spaceGroup.symOps.size(); i++) {
sb = new StringBuilder();
sb.append("SYMM ");
SymOp symop = spaceGroup.symOps.get(i);
sb.append(symop.toXYZString());
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
}
sb = new StringBuilder();
sb.append(String.format("RESO %8.6f%13s%8.6f", res[0], " ", res[1]));
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append("VALM NAN ");
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append("NDIF 1 ");
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append("PROJECT 1 project ");
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append("CRYSTAL 1 crystal ");
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append("DATASET 1 dataset ");
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
for (int j = 0; j < nCol; j++) {
sb = new StringBuilder();
sb.append(String.format("COLUMN %-30s %c %17.4f %17.4f 1", colname.get(j), colType[j], colMinMax[j][0], colMinMax[j][1]));
dataOutputStream.writeBytes(sb.toString());
}
sb = new StringBuilder();
sb.append(String.format("CELL %10.4f %9.4f %9.4f %9.4f %9.4f %9.4f ", crystal.a, crystal.b, crystal.c, crystal.alpha, crystal.beta, crystal.gamma));
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append(String.format("DCELL %9d %10.4f %9.4f %9.4f %9.4f %9.4f %9.4f ", 1, crystal.a, crystal.b, crystal.c, crystal.alpha, crystal.beta, crystal.gamma));
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append("DWAVEL 1 1.00000 ");
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append("END ");
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
sb = new StringBuilder();
sb.append("MTZENDOFHEADERS ");
while (sb.length() < 80) {
sb.append(" ");
}
dataOutputStream.writeBytes(sb.toString());
dataOutputStream.close();
} catch (Exception e) {
String message = "Fatal exception evaluating structure factors.\n";
logger.log(Level.SEVERE, message, e);
}
}
Aggregations