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

feat: add ndarray/base/mskfilter #2604

Draft
wants to merge 36 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2836e0d
chore: create folder structure and add example
Jaysukh-409 Jul 15, 2024
67dfb52
feat: add kernels for 0d ndarray
Jaysukh-409 Jul 29, 2024
2f0ba4c
feat: add kernels for 1d ndarray
Jaysukh-409 Jul 30, 2024
d5bcb07
refactor: update the implementation for 1d kernels
Jaysukh-409 Aug 1, 2024
5010468
feat: add kernels for 2d ndarray
Jaysukh-409 Aug 1, 2024
6f3992f
docs: update description of loop kernel
Jaysukh-409 Aug 2, 2024
ecffc55
docs: refactor JSDoc example for loop kernels
Jaysukh-409 Aug 2, 2024
f531567
refactor: update variable name for consistent naming convention
Jaysukh-409 Aug 2, 2024
8ff83b4
feat: add kernels for 3d ndarray
Jaysukh-409 Aug 3, 2024
4039020
feat: add kernels for nd ndarray
Jaysukh-409 Aug 3, 2024
9422eb5
feat: add kernels for 4d ndarray
Jaysukh-409 Aug 5, 2024
f318edb
feat: add kernels for 5d ndarray
Jaysukh-409 Aug 5, 2024
7235a97
feat: add kernels for 6d ndarray
Jaysukh-409 Aug 5, 2024
d7683e0
feat: add kernels for 7d ndarray
Jaysukh-409 Aug 5, 2024
ad939d1
feat: add kernels for 8d ndarray
Jaysukh-409 Aug 5, 2024
f149847
feat: add kernels for 9d ndarray
Jaysukh-409 Aug 5, 2024
121db5a
feat: add kernels for 10d ndarray
Jaysukh-409 Aug 5, 2024
f1e0a81
docs: fix copy-paste error
Jaysukh-409 Aug 5, 2024
611cbcc
Merge branch 'develop' into ndarray/base/mskfilter
Jaysukh-409 Aug 8, 2024
467a34f
refactor: apply suggestions
Jaysukh-409 Aug 8, 2024
db07de8
test: add tests for 0d ndarray
Jaysukh-409 Aug 10, 2024
da81461
test: add tests for 1d ndarray
Jaysukh-409 Aug 10, 2024
7515b00
bench: add benchmark files
Jaysukh-409 Aug 13, 2024
e2ac30c
docs: added examples in readme
Jaysukh-409 Aug 14, 2024
f8926fb
Apply suggestions from code review
kgryte Aug 14, 2024
c193d8b
test: add tests for 2d ndaray
Jaysukh-409 Aug 16, 2024
11cb348
Merge branch 'ndarray/base/mskfilter' of https://github.com/Jaysukh-4…
Jaysukh-409 Aug 16, 2024
3ed9b3d
docs: add repl and typescript declaration
Jaysukh-409 Aug 16, 2024
59c2e81
test: add test for 3d to nd ndarray
Jaysukh-409 Aug 19, 2024
099e730
refactor: updated the implementation of mskfilter
Jaysukh-409 Aug 20, 2024
eaec078
refactor: apply suggestions
Jaysukh-409 Aug 21, 2024
1e848e9
Merge branch 'ndarray/base/mskfilter' of https://github.com/Jaysukh-4…
Jaysukh-409 Aug 21, 2024
1d1acea
feat: update scripts
Jaysukh-409 Sep 16, 2024
9c1003c
feat: update scripts and templates
Jaysukh-409 Sep 16, 2024
4eeeb46
feat: update templates and run script
Jaysukh-409 Sep 24, 2024
4e40c79
feat: add macros for 1d and 2d ndarray
Jaysukh-409 Sep 30, 2024
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
167 changes: 167 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/mskfilter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<!--

@license Apache-2.0

Copyright (c) 2024 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# mskfilter

> Apply a mask to a provided input ndarray and assign unmasked values to elements in a provided one-dimensional output ndarray.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var mskfilter = require( '@stdlib/ndarray/base/mskfilter' );
```

#### mskfilter( arrays )

Apply a mask to a provided input ndarray and assign unmasked values to elements in a provided one-dimensional output ndarray.
Jaysukh-409 marked this conversation as resolved.
Show resolved Hide resolved

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var Uint8Array = require( '@stdlib/array/uint8' );

// Define the shape of the input and mask arrays:
var shape = [ 3, 1, 2 ];

// Define the array strides:
var sx = [ 2, 2, 1 ];
var sm = [ 2, 2, 1 ];
var sy = [ 1 ];

// Define the index offsets:
var ox = 1;
var om = 0;

// Create the input, mask and output ndarray-like objects:
var x = {
'dtype': 'float64',
'data': new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ),
Jaysukh-409 marked this conversation as resolved.
Show resolved Hide resolved
'shape': shape,
'strides': sx,
'offset': ox,
'order': 'row-major'
};
var mask = {
'dtype': 'uint8',
'data': new Uint8Array( [ 1, 0, 1, 0, 1, 0 ] ),
'shape': shape,
'strides': sm,
'offset': om,
'order': 'row-major'
};
var out = {
'dtype': 'float64',
'data': new Float64Array( 3 ),
'shape': [ 3 ],
'strides': sy,
'offset': 0,
'order': 'row-major'
};

mskfilter( [ x, mask, out ] );

console.log( out.data );
// => <Float64Array>[ 2.0, 4.0, 6.0 ]
```

The function accepts the following arguments:

- **arrays**: array-like object containing input ndarray, mask ndarray and one dimensional output ndarray.
Jaysukh-409 marked this conversation as resolved.
Show resolved Hide resolved

Each provided ndarray should be an object with the following properties:

- **dtype**: data type.
- **data**: data buffer.
- **shape**: dimensions.
- **strides**: stride lengths.
- **offset**: index offset.
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).

</section>

<!-- /.usage -->

<section class="notes">

</section>

<!-- /.notes -->

<section class="examples">

## Examples

```javascript
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var filledarray = require( '@stdlib/array/filled' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
var mskfilter = require( '@stdlib/ndarray/base/mskfilter' );

var N = 10;
var shape = [ 5, 2 ];
var x = {
'dtype': 'float64',
'data': filledarrayBy( N, 'float64', discreteUniform( -100, 100 ) ),
Copy link
Member

Choose a reason for hiding this comment

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

Let's use random/array/discrete-uniform, random/array/bernoulli, and array/zeros for creating the array buffers.

'shape': shape,
'strides': [ 2, 1 ],
'offset': 0,
'order': 'row-major'
};
var mask = {
'dtype': 'uint8',
'data': filledarrayBy( N, 'uint8', discreteUniform( 0, 1 ) ),
'shape': shape,
'strides': [ 2, 1 ],
'offset': 0,
'order': 'row-major'
};
var y = {
'dtype': 'float64',
'data': filledarray( 0, N, 'float64' ),
'shape': [ N ],
'strides': [ 1 ],
'offset': 0,
'order': 'row-major'
};

mskfilter( [ x, mask, y ] );
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
Jaysukh-409 marked this conversation as resolved.
Show resolved Hide resolved
```

</section>

<!-- /.examples -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var floor = require( '@stdlib/math/base/special/floor' );
var filledarray = require( '@stdlib/array/filled' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var pkg = require( './../package.json' ).name;
var mskfilter = require( './../lib/10d_blocked.js' );


// VARIABLES //

var types = [ 'float64' ];
var order = 'column-major';


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - ndarray length
* @param {NonNegativeIntegerArray} shape - ndarray shape
* @param {string} dtype - input and output ndarray data type
* @returns {Function} benchmark function
*/
function createBenchmark( len, shape, dtype ) {
var mask;
var x;
var y;

x = filledarrayBy( len, dtype, discreteUniform( -100, 100 ) );
mask = filledarrayBy( len, 'uint8', discreteUniform( 0, 1 ) );
y = filledarray( 0.0, len, dtype );
x = {
'dtype': dtype,
'data': x,
'shape': shape,
'strides': shape2strides( shape, order ),
'offset': 0,
'order': order
};
mask = {
'dtype': 'uint8',
'data': mask,
'shape': shape,
'strides': shape2strides( shape, order ),
'offset': 0,
'order': order
};
y = {
'dtype': dtype,
'data': y,
'shape': [ len ],
'strides': [ 1 ],
'offset': 0,
'order': order
};
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
mskfilter( x, mask, y );
if ( isnan( y.data[ i%len ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y.data[ i%len ] ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var sh;
var t;
var f;
var i;
var j;

min = 1; // 10^min
max = 6; // 10^max

for ( j = 0; j < types.length; j++ ) {
t = types[ j ];
for ( i = min; i <= max; i++ ) {
len = pow( 10, i );

sh = [ len/2, 2, 1, 1, 1, 1, 1, 1, 1, 1 ];
f = createBenchmark( len, sh, t );
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],order='+order+',dtype='+t, f );

sh = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, len/2 ];
f = createBenchmark( len, sh, t );
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],order='+order+',dtype='+t, f );

len = floor( pow( len, 1.0/10.0 ) );
sh = [ len, len, len, len, len, len, len, len, len, len ];
len *= pow( len, 9 );
f = createBenchmark( len, sh, t );
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],order='+order+',dtype='+t, f );
}
}
}

main();
Loading
Loading