use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.
the class ComBat method computeDesignMatrix.
/**
* ComBat parameterizes the model without an intercept, instead using all possible columns for batch (what the heck
* do you call this parameterization?) This is important. Each batch has its own parameter.
*
* @return double matrix 2d
*/
private DoubleMatrix2D computeDesignMatrix() {
DoubleMatrix2D design;
/*
* Find the batch
*/
DesignMatrix d = null;
int batchFactorColumnIndex = sampleInfo.getColIndexByName(ComBat.BATCH_COLUMN_NAME);
Object[] batchFactor = sampleInfo.getColumn(batchFactorColumnIndex);
if (batchFactor != null) {
d = new DesignMatrix(batchFactor, 1, ComBat.BATCH_COLUMN_NAME);
}
if (d == null) {
throw new IllegalStateException("No batch factor was found");
}
ObjectMatrix<String, String, Object> sampleInfoWithoutBatchFactor = this.getSampleInfoWithoutBatchFactor(batchFactorColumnIndex);
d.add(sampleInfoWithoutBatchFactor);
design = d.getDoubleMatrix();
return design;
}
use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.
the class ComBat method runParametric.
private void runParametric(final DoubleMatrix2D sdata, DoubleMatrix2D gammastar, DoubleMatrix2D deltastar) {
int batchIndex = 0;
for (String batchId : batches.keySet()) {
DoubleMatrix2D batchData = this.getBatchData(sdata, batchId);
DoubleMatrix1D[] batchResults;
batchResults = this.itSol(batchData, gammaHat.viewRow(batchIndex), deltaHat.viewRow(batchIndex), gammaBar.get(batchIndex), t2.get(batchIndex), aPrior.get(batchIndex), bPrior.get(batchIndex));
for (int j = 0; j < batchResults[0].size(); j++) {
gammastar.set(batchIndex, j, batchResults[0].get(j));
}
for (int j = 0; j < batchResults[1].size(); j++) {
deltastar.set(batchIndex, j, batchResults[1].get(j));
}
batchIndex++;
}
}
use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.
the class ComBat method stepSum.
private DoubleMatrix1D stepSum(DoubleMatrix2D matrix, DoubleMatrix1D gnew) {
Algebra s = new Algebra();
DoubleMatrix2D g = new DenseDoubleMatrix2D(1, gnew.size());
for (int i = 0; i < gnew.size(); i++) {
g.set(0, i, gnew.get(i));
}
DoubleMatrix2D a = new DenseDoubleMatrix2D(1, matrix.columns());
a.assign(1.0);
/*
* subtract column gnew from each column of data; square; then sum over each row.
*/
DoubleMatrix2D deltas = matrix.copy().assign((s.mult(s.transpose(g), a)), Functions.minus).assign(Functions.square);
DoubleMatrix1D sumsq = new DenseDoubleMatrix1D(deltas.rows());
sumsq.assign(0.0);
for (int i = 0; i < deltas.rows(); i++) {
sumsq.set(i, DescriptiveWithMissing.sum(new DoubleArrayList(deltas.viewRow(i).toArray())));
}
return sumsq;
}
use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.
the class ComBat method runNonParametric.
private void runNonParametric(final DoubleMatrix2D sdata, DoubleMatrix2D gammastar, DoubleMatrix2D deltastar) {
final ConcurrentHashMap<String, DoubleMatrix1D[]> results = new ConcurrentHashMap<>();
int numThreads = Math.min(batches.size(), Runtime.getRuntime().availableProcessors());
ComBat.log.info("Runing nonparametric estimation on " + numThreads + " threads");
Future<?>[] futures = new Future[numThreads];
ExecutorService service = Executors.newCachedThreadPool();
/*
* Divvy up batches over threads.
*/
int batchesPerThread = batches.size() / numThreads;
final String[] batchIds = batches.keySet().toArray(new String[] {});
for (int i = 0; i < numThreads; i++) {
final int firstBatch = i * batchesPerThread;
final int lastBatch = i == (numThreads - 1) ? batches.size() : firstBatch + batchesPerThread;
futures[i] = service.submit(new Runnable() {
@Override
public void run() {
for (int k = firstBatch; k < lastBatch; k++) {
String batchId = batchIds[k];
DoubleMatrix2D batchData = ComBat.this.getBatchData(sdata, batchId);
DoubleMatrix1D[] batchResults = ComBat.this.nonParametricFit(batchData, gammaHat.viewRow(k), deltaHat.viewRow(k));
results.put(batchId, batchResults);
}
}
});
}
service.shutdown();
boolean allDone = false;
do {
for (Future<?> f : futures) {
allDone = true;
if (!f.isDone() && !f.isCancelled()) {
allDone = false;
break;
}
}
} while (!allDone);
for (int i = 0; i < batchIds.length; i++) {
String batchId = batchIds[i];
DoubleMatrix1D[] batchResults = results.get(batchId);
for (int j = 0; j < batchResults[0].size(); j++) {
gammastar.set(i, j, batchResults[0].get(j));
}
for (int j = 0; j < batchResults[1].size(); j++) {
deltastar.set(i, j, batchResults[1].get(j));
}
}
}
use of cern.colt.matrix.DoubleMatrix2D in project Gemma by PavlidisLab.
the class ComBat method gammaHat.
private void gammaHat(DoubleMatrix2D sdata) {
DoubleMatrix2D Xb = x.viewPart(0, 0, x.rows(), numBatches);
gammaHat = new LeastSquaresFit(Xb, sdata).getCoefficients();
}
Aggregations