Conversation
Summary of ChangesHello @lazarusA, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the data access capabilities of the NetCDF4-WASM library by introducing advanced slicing and striding functionality. It allows users to precisely select and retrieve subsets of variable data using a familiar Python-like syntax, including support for negative steps and custom strides. This improvement makes the library more powerful and intuitive for data manipulation, aligning its data access patterns with common data science tools and improving the overall user experience for interacting with NetCDF data. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces strided data reading capabilities, a significant enhancement. The implementation includes new C wrappers, TypeScript logic for slicing, and a new UI component for testing slices. The code is generally well-structured, especially the new slice.ts file. I've identified a few issues: a bug in the new slice tester UI, some opportunities to improve React hook usage for better maintainability, and an inconsistency in pointer types within the WASM module wrapper that could lead to issues with large datasets. My detailed comments and suggestions are below.
|
|
||
| {/* Selection preview */} | ||
| <div className="text-xs font-mono text-muted-foreground bg-muted/50 rounded px-2 py-1.5 break-all"> | ||
| get("{info.name}", [{selectionPreview}]) |
There was a problem hiding this comment.
The selection preview string is not using a template literal, so it will display {info.name} literally instead of interpolating the variable's name. This should be converted to a template literal to correctly display the preview.
| get("{info.name}", [{selectionPreview}]) | |
| get(`"${info.name}"`, [${selectionPreview}]) |
| nc_get_vars_generic: (ncid: number, varid: number, start: number[], count: number[], stride: number[], nctype: number) => { | ||
| const elementSize = DATA_TYPE_SIZE[nctype]; | ||
| const totalLength = stridedLength(count, stride); | ||
| const dataPtr = module._malloc(totalLength * elementSize); | ||
| const startPtr = module._malloc(start.length * 4); | ||
| const countPtr = module._malloc(count.length * 4); | ||
| const stridePtr = module._malloc(stride.length * 4); | ||
| start .forEach((v, i) => module.setValue(startPtr + i * 4, v, 'i32')); | ||
| count .forEach((v, i) => module.setValue(countPtr + i * 4, v, 'i32')); | ||
| stride.forEach((v, i) => module.setValue(stridePtr + i * 4, v, 'i32')); | ||
| const result = nc_get_vars_wrapper(ncid, varid, startPtr, countPtr, stridePtr, dataPtr); | ||
| let data; | ||
| if (result === NC_CONSTANTS.NC_NOERR) { | ||
| switch (nctype) { | ||
| case NC_CONSTANTS.NC_BYTE: data = new Int8Array(module.HEAP8.buffer, dataPtr, totalLength).slice(); break; | ||
| case NC_CONSTANTS.NC_UBYTE: data = new Uint8Array(module.HEAPU8.buffer, dataPtr, totalLength).slice(); break; | ||
| case NC_CONSTANTS.NC_SHORT: data = new Int16Array(module.HEAP16.buffer, dataPtr, totalLength).slice(); break; | ||
| case NC_CONSTANTS.NC_USHORT: data = new Uint16Array(module.HEAPU16.buffer, dataPtr, totalLength).slice(); break; | ||
| case NC_CONSTANTS.NC_INT: data = new Int32Array(module.HEAP32.buffer, dataPtr, totalLength).slice(); break; | ||
| case NC_CONSTANTS.NC_UINT: data = new Uint32Array(module.HEAPU32.buffer, dataPtr, totalLength).slice(); break; | ||
| case NC_CONSTANTS.NC_INT64: data = new BigInt64Array(module.HEAP64.buffer, dataPtr, totalLength).slice(); break; | ||
| case NC_CONSTANTS.NC_UINT64: data = new BigUint64Array(module.HEAPU64.buffer, dataPtr, totalLength).slice(); break; | ||
| } | ||
| } | ||
| module._free(dataPtr); module._free(startPtr); module._free(countPtr); module._free(stridePtr); | ||
| return { result, data }; | ||
| }, |
There was a problem hiding this comment.
The comment on line 1512 raises a valid concern. This generic function uses i32 for start, count, and stride pointers, which is inconsistent with the typed nc_get_vars_* functions that use i64. Using i32 can limit array indexing to 2GB elements and may cause issues with large datasets.
For consistency and to prevent potential overflow issues, this function should also use i64 for these pointers, similar to the other nc_get_vars_* and nc_get_vara_* wrappers.
nc_get_vars_generic: (ncid: number, varid: number, start: number[], count: number[], stride: number[], nctype: number) => {
const elementSize = DATA_TYPE_SIZE[nctype];
const totalLength = stridedLength(count, stride);
const dataPtr = module._malloc(totalLength * elementSize);
const startPtr = module._malloc(start.length * 8);
const countPtr = module._malloc(count.length * 8);
const stridePtr = module._malloc(stride.length * 8);
start .forEach((v, i) => module.setValue(startPtr + i * 8, v, 'i64'));
count .forEach((v, i) => module.setValue(countPtr + i * 8, v, 'i64'));
stride.forEach((v, i) => module.setValue(stridePtr + i * 8, v, 'i64'));
const result = nc_get_vars_wrapper(ncid, varid, startPtr, countPtr, stridePtr, dataPtr);
let data;
if (result === NC_CONSTANTS.NC_NOERR) {
switch (nctype) {
case NC_CONSTANTS.NC_BYTE: data = new Int8Array(module.HEAP8.buffer, dataPtr, totalLength).slice(); break;
case NC_CONSTANTS.NC_UBYTE: data = new Uint8Array(module.HEAPU8.buffer, dataPtr, totalLength).slice(); break;
case NC_CONSTANTS.NC_SHORT: data = new Int16Array(module.HEAP16.buffer, dataPtr, totalLength).slice(); break;
case NC_CONSTANTS.NC_USHORT: data = new Uint16Array(module.HEAPU16.buffer, dataPtr, totalLength).slice(); break;
case NC_CONSTANTS.NC_INT: data = new Int32Array(module.HEAP32.buffer, dataPtr, totalLength).slice(); break;
case NC_CONSTANTS.NC_UINT: data = new Uint32Array(module.HEAPU32.buffer, dataPtr, totalLength).slice(); break;
case NC_CONSTANTS.NC_INT64: data = new BigInt64Array(module.HEAP64.buffer, dataPtr, totalLength).slice(); break;
case NC_CONSTANTS.NC_UINT64: data = new BigUint64Array(module.HEAPU64.buffer, dataPtr, totalLength).slice(); break;
}
}
module._free(dataPtr); module._free(startPtr); module._free(countPtr); module._free(stridePtr);
return { result, data };
},| useEffect(() => { | ||
| setSliceResult(null); | ||
| setSliceError(null); | ||
| setExpandedSliceTester(false); | ||
| if (selectedVariable && variables[selectedVariable]?.info?.shape) { | ||
| setSliceSelections( | ||
| (variables[selectedVariable].info.shape as any[]).map(() => defaultSelection()) | ||
| ); | ||
| } else { | ||
| setSliceSelections([]); | ||
| } | ||
| }, [selectedVariable]); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
|
||
| // Init slice selections when info first loads for the selected variable | ||
| useEffect(() => { | ||
| if ( | ||
| selectedVariable && | ||
| variables[selectedVariable]?.info?.shape && | ||
| sliceSelections.length === 0 | ||
| ) { | ||
| setSliceSelections( | ||
| (variables[selectedVariable].info.shape as any[]).map(() => defaultSelection()) | ||
| ); | ||
| } | ||
| }, [variables, selectedVariable]); // eslint-disable-line react-hooks/exhaustive-deps |
There was a problem hiding this comment.
These two useEffect hooks manage related state and have disabled ESLint warnings for exhaustive dependencies, which can lead to subtle bugs and make the code harder to maintain. They can be combined into a single, more robust useEffect hook with a complete dependency array. This simplifies the logic for resetting and initializing the slice selections.
| useEffect(() => { | |
| setSliceResult(null); | |
| setSliceError(null); | |
| setExpandedSliceTester(false); | |
| if (selectedVariable && variables[selectedVariable]?.info?.shape) { | |
| setSliceSelections( | |
| (variables[selectedVariable].info.shape as any[]).map(() => defaultSelection()) | |
| ); | |
| } else { | |
| setSliceSelections([]); | |
| } | |
| }, [selectedVariable]); // eslint-disable-line react-hooks/exhaustive-deps | |
| // Init slice selections when info first loads for the selected variable | |
| useEffect(() => { | |
| if ( | |
| selectedVariable && | |
| variables[selectedVariable]?.info?.shape && | |
| sliceSelections.length === 0 | |
| ) { | |
| setSliceSelections( | |
| (variables[selectedVariable].info.shape as any[]).map(() => defaultSelection()) | |
| ); | |
| } | |
| }, [variables, selectedVariable]); // eslint-disable-line react-hooks/exhaustive-deps | |
| // Reset and initialize slice tester state | |
| useEffect(() => { | |
| setSliceResult(null); | |
| setSliceError(null); | |
| setExpandedSliceTester(false); | |
| if (selectedVariable && variables[selectedVariable]?.info?.shape) { | |
| setSliceSelections( | |
| (variables[selectedVariable].info.shape as any[]).map(() => defaultSelection()) | |
| ); | |
| } else { | |
| setSliceSelections([]); | |
| } | |
| }, [selectedVariable, variables]); |
| size="sm" | ||
| onClick={onRun} | ||
| disabled={loadingSlice} | ||
| style={{ backgroundColor: '#644FF0', color: 'white' }} |
|
done in #57 |
WIP:
This adds the
nc_get_vars_*which will allow us to usestrides when reading variable data.sliceclass is addedget("temperature", [slice(0,1), null, null]), which mirrors fully the functionality from zarrita.See: https://docs.unidata.ucar.edu/netcdf-c/current/group__variables.html#details