Skip to content

Commit

Permalink
Fix flattening of values fetched with selection param
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Nov 3, 2021
1 parent fa9708f commit 4399b7b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/app/src/providers/h5grove/h5grove-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { isString } from 'lodash';
import { ProviderApi } from '../api';
import type { ValuesStoreParams } from '../models';
import { ProviderError } from '../models';
import { convertDtype, handleAxiosError } from '../utils';
import { convertDtype, flattenValue, handleAxiosError } from '../utils';
import type {
H5GroveAttribute,
H5GroveAttrValuesResponse,
Expand Down Expand Up @@ -48,7 +48,7 @@ export class H5GroveApi extends ProviderApi {
public async getValue(
params: ValuesStoreParams
): Promise<H5GroveDataResponse> {
const { path } = params;
const { path, selection } = params;
const entity = await this.getEntity(path);
assertDataset(entity);
assertNonNullShape(entity);
Expand All @@ -62,7 +62,7 @@ export class H5GroveApi extends ProviderApi {

const value = await this.fetchData(params);
return hasArrayShape(entity)
? (value as unknown[]).flat(entity.shape.length - 1)
? flattenValue(value, entity, selection)
: value;
}

Expand Down
8 changes: 2 additions & 6 deletions packages/app/src/providers/hsds/hsds-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { ProviderApi } from '../api';
import type { ValuesStoreParams } from '../models';
import { ProviderError } from '../models';
import { handleAxiosError } from '../utils';
import { flattenValue, handleAxiosError } from '../utils';
import type {
HsdsDatasetResponse,
HsdsDatatypeResponse,
Expand Down Expand Up @@ -118,11 +118,7 @@ export class HsdsApi extends ProviderApi {
// https://github.com/HDFGroup/hsds/issues/88
// HSDS does not reduce the number of dimensions when selecting indices
// Therefore the flattening must be done on all dimensions regardless of the selection
if (hasArrayShape(entity)) {
return (value as unknown[]).flat(entity.shape.length - 1);
}

return value;
return hasArrayShape(entity) ? flattenValue(value, entity) : value;
}

private async fetchRootId(): Promise<HsdsId> {
Expand Down
15 changes: 13 additions & 2 deletions packages/app/src/providers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DType } from '@h5web/shared';
import { Endianness, DTypeClass } from '@h5web/shared';
import type { ArrayShape, Dataset, DType } from '@h5web/shared';
import { Endianness, DTypeClass, assertArray } from '@h5web/shared';
import axios from 'axios';

import type { ProviderError } from './models';
Expand Down Expand Up @@ -81,6 +81,17 @@ export function convertDtype(dtype: string): DType {
}
}

export function flattenValue(
value: unknown,
dataset: Dataset<ArrayShape>,
selection?: string
): unknown[] {
assertArray(value);
const slicedDims = selection?.split(',').filter((s) => s.includes(':'));
const dims = slicedDims || dataset.shape;
return value.flat(dims.length - 1);
}

export async function handleAxiosError<T>(
func: () => Promise<T>,
getErrorToThrow: (
Expand Down

0 comments on commit 4399b7b

Please sign in to comment.