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

Optimize ndarray uses in MappedVis #198

Merged
merged 1 commit into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"lodash-es": "^4.17.15",
"nanoid": "^2.1.11",
"ndarray": "^1.0.19",
"ndarray-ops": "^1.2.2",
"ndarray-unpack": "^1.0.0",
"normalize.css": "^8.0.1",
"react": "^16.13.1",
Expand Down
15 changes: 10 additions & 5 deletions src/h5web/visualizations/shared/MappedVis.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { ReactElement, useMemo, ElementType } from 'react';
import { isNumber } from 'lodash-es';
import ndarray from 'ndarray';
import unpack from 'ndarray-unpack';
import ops from 'ndarray-ops';
import type { DimensionMapping } from '../../dataset-visualizer/models';
import type { HDF5Dataset, HDF5SimpleShape } from '../../providers/models';

Expand All @@ -16,9 +16,11 @@ function MappedVis<T>(props: Props<T>): ReactElement {
const { component: Component, dataset, value, mapperState } = props;
const rawDims = (dataset.shape as HDF5SimpleShape).dims;

const dataArray = useMemo(() => {
const baseArray = ndarray<T>(value.flat(Infinity) as T[], rawDims);
const baseArray = useMemo(() => {
return ndarray<T>(value.flat(Infinity) as T[], rawDims);
}, [rawDims, value]);

const dataArray = useMemo(() => {
if (mapperState === undefined) {
return baseArray;
}
Expand All @@ -32,8 +34,11 @@ function MappedVis<T>(props: Props<T>): ReactElement {
const mappedView = isXBeforeY ? slicedView.transpose(1, 0) : slicedView;

// Create ndarray from mapped view so `dataArray.data` only contains values relevant to vis
return ndarray<T>(unpack(mappedView).flat(), mappedView.shape);
}, [value, rawDims, mapperState]);
const mappedArray = ndarray<T>([], mappedView.shape);
ops.assign(mappedArray, mappedView);

return mappedArray;
}, [mapperState, baseArray]);

return <Component dataArray={dataArray} />;
}
Expand Down
6 changes: 6 additions & 0 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ declare module 'ndarray-unpack' {

export = unpack;
}

declare module 'ndarray-ops' {
function assign(a: ndarray<T>, b: ndarray<T>);

export = { assign };
}