Skip to content

Commit

Permalink
Restore expected default behavior when initializing from a dense array.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Oct 10, 2024
1 parent c1c105b commit 37c715c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
8 changes: 4 additions & 4 deletions js/initializeScranMatrixFromArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { ScranMatrix } from "./ScranMatrix.js";
* @param {WasmArray|Array|TypedArray} values Values of all elements in the matrix, stored in column-major order.
* This is generally expected to contain non-negative integers; otherwise, users should set `forceInteger = false`.
* @param {object} [options={}] - Optional parameters.
* @param {boolean} [options.forceInteger=true] - Whether to coerce `values` to integers via truncation.
* @param {boolean} [options.sparse=true] - Whether to return a sparse matrix, assuming that `values` contains mostly zeros.
* @param {boolean} [options.forceInteger=false] - Whether to coerce `values` to integers via truncation.
* @param {boolean} [options.sparse=false] - Whether to return a sparse matrix, assuming that `values` contains mostly zeros.
* @param {boolean} [options.layered=true] - Whether to create a layered sparse matrix, see [**tatami_layered**](https://github.com/tatami-inc/tatami_layered) for more details.
* Only used if `sparse = true` and one of the following is true: `values` contains an integer type and/or `forceInteger = true`.
* Only used if `sparse = true` and at least one of the following is true: `values` contains an integer type and/or `forceInteger = true`.
* Setting `layered = true` assumes that `values` contains only non-negative integers.
*
* @return {ScranMatrix} A matrix object.
*/
export function initializeScranMatrixFromDenseArray(numberOfRows, numberOfColumns, values, { forceInteger = true, sparse = true, layered = true } = {}) {
export function initializeScranMatrixFromDenseArray(numberOfRows, numberOfColumns, values, { forceInteger = false, sparse = false, layered = true } = {}) {
var val_data;
var output;

Expand Down
16 changes: 10 additions & 6 deletions tests/initializeScranMatrixFromArrays.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ test("initialization from dense array works correctly", () => {
var vals = scran.createInt32WasmArray(15);
vals.set([1, 5, 0, 0, 7, 0, 0, 10, 4, 2, 0, 0, 0, 5, 8]);

var mat = scran.initializeScranMatrixFromDenseArray(nr, nc, vals);
var mat = scran.initializeScranMatrixFromDenseArray(nr, nc, vals, { sparse: true });
expect(mat.numberOfRows()).toBe(nr);
expect(mat.numberOfColumns()).toBe(nc);
expect(mat.isSparse()).toBe(true);

// Compare to a non-layered initialization.
var mat2 = scran.initializeScranMatrixFromDenseArray(nr, nc, vals, { layered: false });
var mat2 = scran.initializeScranMatrixFromDenseArray(nr, nc, vals, { sparse: true, layered: false });
expect(mat2.numberOfRows()).toBe(nr);
expect(mat2.numberOfColumns()).toBe(nc);
expect(mat2.isSparse()).toBe(true);

// Compare to a dense initialization.
var dense = scran.initializeScranMatrixFromDenseArray(nr, nc, vals, { sparse: false });
var dense = scran.initializeScranMatrixFromDenseArray(nr, nc, vals);
expect(dense.numberOfRows()).toBe(nr);
expect(dense.numberOfColumns()).toBe(nc);
expect(dense.isSparse()).toBe(false);

// Properly column-major.
// Extracts values correctly.
for (var i = 0; i < nc; i++) {
let ref = vals.slice(i * nr, (i + 1) * nr);
expect(compare.equalArrays(mat.column(i), ref)).toBe(true);
Expand Down Expand Up @@ -64,10 +64,12 @@ test("forced integers from dense array works correctly", () => {
var vals = scran.createFloat64WasmArray(15);
vals.set([1.2, 2.5, 0, 0, 7.1, 0, 0, 10.1, 4.2, 2.3, 0, 0, 0, 5.3, 8.1]);

var smat1 = scran.initializeScranMatrixFromDenseArray(nr, nc, vals, { forceInteger: true, layered: false });
var smat2 = scran.initializeScranMatrixFromDenseArray(nr, nc, vals, { forceInteger: false });
var smat1 = scran.initializeScranMatrixFromDenseArray(nr, nc, vals, { sparse: true, forceInteger: true, layered: false });
var smat2 = scran.initializeScranMatrixFromDenseArray(nr, nc, vals, { sparse: true, forceInteger: false });
var dmat1 = scran.initializeScranMatrixFromDenseArray(nr, nc, vals, { sparse: false, forceInteger: true });
var dmat2 = scran.initializeScranMatrixFromDenseArray(nr, nc, vals, { sparse: false, forceInteger: false });
var default_dmat = scran.initializeScranMatrixFromDenseArray(nr, nc, vals);
expect(default_dmat.isSparse()).toBe(false);

for (var i = 0; i < nc; i++) {
let ref = vals.slice(i * nr, (i + 1) * nr);
Expand All @@ -77,6 +79,7 @@ test("forced integers from dense array works correctly", () => {
expect(compare.equalArrays(smat2.column(i), ref)).toBe(true);
expect(compare.equalArrays(dmat1.column(i), trunc)).toBe(true);
expect(compare.equalArrays(dmat2.column(i), ref)).toBe(true);
expect(compare.equalArrays(default_dmat.column(i), ref)).toBe(true);
}

// Cleaning up.
Expand All @@ -85,6 +88,7 @@ test("forced integers from dense array works correctly", () => {
smat2.free();
dmat1.free();
dmat2.free();
default_dmat.free();
})

test("initialization from compressed values works correctly", () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function simulateMatrix(numberOfRows, numberOfColumns, density = 0.2, max
}
}

output = scran.initializeScranMatrixFromDenseArray(numberOfRows, numberOfColumns, buffer, { forceInteger });
output = scran.initializeScranMatrixFromDenseArray(numberOfRows, numberOfColumns, buffer, { forceInteger: forceInteger, sparse: true });
} finally {
buffer.free();
}
Expand Down Expand Up @@ -96,7 +96,7 @@ export function simulatePermutedMatrix(numberOfRows, numberOfColumns, density =
}
}

output = scran.initializeScranMatrixFromDenseArray(numberOfRows, numberOfColumns, buffer);
output = scran.initializeScranMatrixFromDenseArray(numberOfRows, numberOfColumns, buffer, { sparse: true });
} finally {
buffer.free();
}
Expand Down

0 comments on commit 37c715c

Please sign in to comment.