Skip to content

Commit

Permalink
Add more projection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mooreryan committed Mar 13, 2018
1 parent f2ba692 commit 2f42313
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
30 changes: 27 additions & 3 deletions app/assets/javascripts/fn/complex/lalolib.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
fn.lalolib = {}; // These interact with the LALOLib structures directly.
fn.lalolib.svd = {}; // For dealing with SVD

/**
* Scales columns of the matrix from 0 to 1.
*
* TODO needs specs.
*
* @param M
* @returns {returns}
*/
fn.lalolib.scale_cols_from_0_to_1 = function (M) {
var func = function (col) {
var min = fn.ary.min(col);
var max = fn.ary.max(col);
var new_min = 0;
var new_max = 1;

return col.map(function (val) {
return fn.math.scale(val, min, max, new_min, new_max);
});
};

return fn.lalolib.apply_to_cols(M, func);
};


/**
* Applies the func to each col in the Matrix M.
*
Expand Down Expand Up @@ -296,7 +320,7 @@ fn.lalolib.svd.svd = function (M, center) {
numeric_svd = null;

try {
svd = lalolib.svd(the_matrix, "thinU");
svd = lalolib.svd(the_matrix, "full");
}
catch (error) {
console.log("lalolib.svd() failed with error: " + error + ". Trying numeric.svd() instead.");
Expand All @@ -312,7 +336,7 @@ fn.lalolib.svd.svd = function (M, center) {
svd = {
S: lalolib.array2mat(numeric.diag(numeric_svd.S)),
U: lalolib.array2mat(numeric_svd.V),
V: undefined,
V: lalolib.array2mat(numeric_svd.U),
s: lalolib.array2mat(numeric_svd.S)
};
}
Expand All @@ -324,7 +348,7 @@ fn.lalolib.svd.svd = function (M, center) {
svd = {
S: lalolib.array2mat(numeric.diag(numeric_svd.S)),
U: lalolib.array2mat(numeric_svd.U),
V: undefined,
V: lalolib.array2mat(numeric_svd.V),
s: lalolib.array2mat(numeric_svd.S)
};
}
Expand Down
5 changes: 4 additions & 1 deletion app/assets/javascripts/fn/complex/parsed_biom.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,14 +972,17 @@ fn.parsed_biom.new = function (params) {
// fully_parsed_biom.count_matrix = fn.parsed_biom.count_matrix(fully_parsed_biom.leaf_names, fully_parsed_biom.counts_for_each_leaf);
fully_parsed_biom.count_matrix = fn.parsed_biom.count_matrix(fully_parsed_biom.leaf_names, fully_parsed_biom.counts_for_each_leaf);

fully_parsed_biom.projection = TODO;
fully_parsed_biom.projection = null;
if (projection_type === "pc") {
fully_parsed_biom.projection = fn.project.project_with_num_pcs_cutoff(fully_parsed_biom.count_matrix, sing_vals_to_keep);
}
else {
fully_parsed_biom.projection = fn.project.project_with_variance_cutoff(fully_parsed_biom.count_matrix, sing_vals_to_keep);
}

fully_parsed_biom.projection_leaves_1d = fn.project.projection_leaves_1d(fully_parsed_biom.count_matrix);
fully_parsed_biom.projection_samples_1d = fn.project.projection_samples_1d(fully_parsed_biom.count_matrix);

return fully_parsed_biom;
};

28 changes: 28 additions & 0 deletions app/assets/javascripts/fn/complex/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,31 @@ fn.project.project_with_num_pcs_cutoff = function (M, num_pcs_to_keep) {
return fn.lalolib.first_n_cols(pca_scores, num_pcs_to_keep);
}
};

/**
* Return a 1D projection of the samples in PC space.
*
* The output is scaled from 0 to 1 for use in the chroma.js color scale functions.
*
* @param M
* @returns {Matrix}
*/
fn.project.projection_samples_1d = function (M) {
var svd = fn.lalolib.svd.svd(M, true);

var scores = fn.lalolib.scale_cols_from_0_to_1(lalolib.mul(svd.V, svd.S));

return fn.lalolib.first_n_cols(scores, 1);
};

/**
* Return a 1D projection of leaves in PC space.
*
* The output is scaled from 0 to 1 for use in the chroma.js color scale functions.
*
* @param M
* @returns {Object}
*/
fn.project.projection_leaves_1d = function (M) {
return fn.lalolib.scale_cols_from_0_to_1(fn.project.project_with_num_pcs_cutoff(M, 1));
};
9 changes: 9 additions & 0 deletions spec/javascripts/fn.parsed_biom_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,15 @@ describe("fn", function () {
it("sets projection", function () {
spec_helper.expect_stringify_equal(actual.projection, expected.projection);
});

it("sets projection_leaves_1d", function () {
spec_helper.expect_stringify_equal(actual.projection_leaves_1d, expected.projection_leaves_1d);
});

it("sets projection_samples_1d", function () {
// Note that actual is a 6x1 matrix, but the expected is just a vector, so take the val of actual.
spec_helper.expect_stringify_equal(actual.projection_samples_1d.val, expected.projection_samples_1d);
});
});
});
});
13 changes: 12 additions & 1 deletion spec/javascripts/spec_helper_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,14 @@ spec_helper.test_case.COUNT_MATRIX_PCA_SCORES = lalolib.mul(spec_helper.test_cas
// The projection will give two singular values.
spec_helper.test_case.PROJECTION = fn.lalolib.first_n_cols(spec_helper.test_case.COUNT_MATRIX_PCA_SCORES, 2);


// These come from scaling the projection by hand
spec_helper.test_case.PROJECTION_LEAVES_1D = lalolib.array2mat([
[1],
[0]
]);
spec_helper.test_case.PROJECTION_SAMPLES_1D = lalolib.array2mat([0.3333333333333333, 0.3333333333333333, 0.8333333333333334, 0, 1, 0.5]);

spec_helper.test_case.COUNTS_WITH_ZEROS_REPLACED = {
apple: [global.ZERO_REPLACEMENT_VAL, 1, 2, global.ZERO_REPLACEMENT_VAL, 3, 4],
pie: [1, 2, global.ZERO_REPLACEMENT_VAL, 3, global.ZERO_REPLACEMENT_VAL, 4]
Expand Down Expand Up @@ -691,7 +699,10 @@ spec_helper.test_case.FULLY_PARSED_BIOM = {
approx_starting_colors_html: spec_helper.test_case.APPROX_STARTING_COLORS_HTML,

count_matrix: spec_helper.test_case.COUNT_MATRIX,
projection: spec_helper.test_case.PROJECTION
projection: spec_helper.test_case.PROJECTION,

projection_leaves_1d: spec_helper.test_case.PROJECTION_LEAVES_1D,
projection_samples_1d: spec_helper.test_case.PROJECTION_SAMPLES_1D
};

// See svd.r for how this was generated.
Expand Down

0 comments on commit 2f42313

Please sign in to comment.