Skip to content

Commit d951d72

Browse files
authored
Merge pull request #27 from EarthyScience/quick-lru
implement quickLRU and added loading element
2 parents 3370e54 + 788f527 commit d951d72

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"jotai": "^2.10.0",
3333
"js-colormaps-es": "^0.0.5",
3434
"leva": "^0.10.0",
35+
"quick-lru": "^7.0.1",
3536
"react": "^19.1.0",
3637
"react-dom": "^19.1.0",
3738
"three": "^0.175.0",

src/App.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,16 @@ html, body, #root {
207207
padding: 0 5px;
208208
}
209209
}
210+
211+
.loading{
212+
position: absolute;
213+
left: 50%;
214+
top: 50%;
215+
background-color: rgba(255, 255, 255, 0.596);
216+
z-index: 10;
217+
padding: 20px;
218+
font-size: 48px;
219+
color: #222222;
220+
border-radius: 20px;
221+
transform: translate(-50%,-50%);
222+
}

src/components/CanvasGeometry.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function CanvasGeometry() {
5656
const [showTimeSeries,setShowTimeSeries] = useState<boolean>(false)
5757
const [colormap,setColormap] = useState<THREE.DataTexture>(GetColorMapTexture())
5858
const [timeSeries, setTimeSeries] = useState<number[]>([0]);
59+
const [showLoading, setShowLoading] = useState<boolean>(false);
5960

6061

6162
const ZarrDS = useMemo(()=>new ZarrDataset(storeURL),[])
@@ -68,6 +69,7 @@ export function CanvasGeometry() {
6869
//DATA LOADING
6970
useEffect(() => {
7071
if (variable != "Default") {
72+
setShowLoading(true);
7173
//Need to add a check somewhere here to swap to 2D or 3D based on shape. Probably export two variables from GetArray
7274
ZarrDS.GetArray(variable).then((result) => {
7375
// result now contains: { data: TypedArray, shape: number[], dtype: string }
@@ -91,6 +93,7 @@ export function CanvasGeometry() {
9193
}
9294
const shapeRatio = result.shape[1] / result.shape[2] * 2;
9395
setShape(new THREE.Vector3(2, shapeRatio, 2));
96+
setShowLoading(false)
9497
})
9598
}
9699
else{
@@ -112,6 +115,11 @@ export function CanvasGeometry() {
112115

113116
return (
114117
<>
118+
<div className='messages'>
119+
{showLoading && <div className='loading'>
120+
Loading...
121+
</div>}
122+
</div>
115123
<div className='canvas'>
116124
<Canvas shadows camera={{ position: [-4.5, 3, 4.5], fov: 50 }}
117125
frameloop="demand"

src/components/ZarrLoaderLRU.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as zarr from "zarrita";
22
import * as THREE from 'three';
3+
import QuickLRU from 'quick-lru';
34

45
function GetZarrVariables(obj: Record<string, { path?: string; kind?: string }>) {
56
//Parses out variables in a Zarr group for variable list
@@ -84,8 +85,6 @@ function parseUVCoords({normal,uv}:{normal:THREE.Vector3,uv:THREE.Vector2}){
8485
}
8586
}
8687

87-
88-
8988
export async function GetTimeSeries({TimeSeriesObject}:GetTimeSeries){
9089
const {uv,normal,variable, storePath} = TimeSeriesObject
9190
const d_store = zarr.tryWithConsolidated(
@@ -102,29 +101,31 @@ export async function GetTimeSeries({TimeSeriesObject}:GetTimeSeries){
102101
return arr
103102
}
104103

104+
105105
interface TimeSeriesInfo{
106106
uv:THREE.Vector2,
107107
normal:THREE.Vector3
108108
}
109109

110+
110111
export class ZarrDataset{
111112
private storePath: string;
112113
private variable: string;
113-
private cache: { [key: string]: any };
114+
private cache: QuickLRU<string,any>;
114115

115116
constructor(storePath: string){
116117
this.storePath = storePath;
117118
this.variable = "Default";
118-
this.cache = {};
119+
this.cache = new QuickLRU({maxSize: 2000});
119120
}
120121

121122
async GetArray(variable: string){
122123
//This checks if variable is stored in cache
123124
this.variable = variable;
124125
let outVar = null;
125-
if (this.cache.hasOwnProperty(variable)){
126+
if (this.cache.has(variable)){
126127
console.log("Using Cache")
127-
return this.cache[variable]
128+
return this.cache.get(variable)
128129
}
129130
const d_store = zarr.tryWithConsolidated(
130131
new zarr.FetchStore(this.storePath)
@@ -137,7 +138,7 @@ export class ZarrDataset{
137138
if (outVar.is("number") || outVar.is("bigint")) {
138139
const chunk = await zarr.get(outVar)
139140
const typedArray = new Float32Array(chunk.data);
140-
this.cache[variable] = chunk;
141+
this.cache.set(variable, chunk);
141142
// TypeScript will now infer the correct numeric type
142143
return {
143144
data: typedArray,
@@ -151,10 +152,10 @@ export class ZarrDataset{
151152

152153
async GetTimeSeries(TimeSeriesInfo:TimeSeriesInfo){
153154
const {uv,normal} = TimeSeriesInfo
154-
if (!this.cache[this.variable]){
155+
if (!this.cache.has(this.variable)){
155156
return [0]
156157
}
157-
const {data,shape,stride} = this.cache[this.variable]
158+
const {data,shape,stride} = this.cache.get(this.variable)
158159
//This is a complicated logic check but it works bb
159160
const sliceSize = parseUVCoords({normal,uv})
160161

0 commit comments

Comments
 (0)