Skip to content

Commit 6e2c504

Browse files
authored
Merge pull request #481 from EarthyScience/jp/small-values
Very very small values
2 parents cbc41a7 + 8487370 commit 6e2c504

File tree

6 files changed

+43
-29
lines changed

6 files changed

+43
-29
lines changed

src/components/plots/Plot.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,8 @@ const Plot = () => {
203203
shape: result.shape
204204
})
205205
setTextures(tempTexture)
206-
if (result.scalingFactor){
207-
const {maxVal, minVal} = scaling
208-
setValueScales({ maxVal: maxVal*(Math.pow(10,result.scalingFactor)), minVal: minVal*(Math.pow(10,result.scalingFactor)) });
209-
}else{
210-
setValueScales(scaling as { maxVal: number; minVal: number });
211-
}
206+
setValueScales(scaling as { maxVal: number; minVal: number });
207+
useGlobalStore.getState().setScalingFactor(result.scalingFactor)
212208
const shapeLength = result.shape.length
213209
if (shapeLength == 2){
214210
setIsFlat(true)

src/components/textures/TextureMakers.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function StoreData(array: Array, valueScales?: {maxVal: number, minVal: number})
1414
const data = array.data;
1515
const [minVal,maxVal] = valueScales ? [valueScales.minVal, valueScales.maxVal] : ArrayMinMax(data)
1616
const textureData = new Uint8Array(
17-
array.data.map((i) => {
17+
data.map((i) => {
1818
const normed = (i - minVal) / (maxVal - minVal);
1919
return isNaN(normed) ? 255 : normed * 254;
2020
})

src/components/ui/Colorbar.tsx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,19 @@ const operationMap = {
3838
CUMSUM3D: "Cumulative Sum"
3939
};
4040

41+
function Num2String(value: number){
42+
if (Math.abs(value) > 1e3 && Math.abs(value) < 1e6 || value === 0){
43+
return value.toFixed(2)
44+
} else{
45+
return value.toExponential(2)
46+
}
47+
}
48+
4149
const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Record<string, any>, valueScales: {maxVal: number, minVal:number}}) => {
42-
const {colormap, variable} = useGlobalStore(useShallow(state => ({
50+
const {colormap, variable, scalingFactor} = useGlobalStore(useShallow(state => ({
4351
colormap: state.colormap,
44-
variable: state.variable
52+
variable: state.variable,
53+
scalingFactor: state.scalingFactor
4554
})))
4655
const {cScale, cOffset, setCScale, setCOffset} = usePlotStore(useShallow(state => ({
4756
cScale: state.cScale,
@@ -61,8 +70,8 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
6170
const scaling = useRef<boolean>(false)
6271
const prevPos = useRef<{ x: number | null; y: number | null }>({ x: null, y: null });
6372
const {origMin, origMax} = useMemo(()=>({
64-
origMin: TwoDecimals(valueScales.minVal),
65-
origMax: TwoDecimals(valueScales.maxVal)
73+
origMin: valueScales.minVal,
74+
origMax: valueScales.maxVal
6675
}),[valueScales])
6776
const range = origMax - origMin
6877
const zeroPoint = (0 - origMin) / range; // Used to account for shifting values
@@ -105,9 +114,8 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
105114
const thisOffset = deltaX / 100
106115
const lastMin = prevVals.current.min
107116
const lastMax = prevVals.current.max
108-
setNewMin(TwoDecimals(lastMin+(range*thisOffset)))
109-
setNewMax(TwoDecimals(lastMax+(range*thisOffset)))
110-
117+
setNewMin(lastMin+(range*thisOffset))
118+
setNewMax(lastMax+(range*thisOffset))
111119
};
112120

113121
// Mouse up handler
@@ -175,7 +183,6 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
175183
return `[${units}]`
176184
}
177185
},[analysisMode, execute])
178-
179186
return (
180187
<>
181188
<div className='colorbar' >
@@ -185,13 +192,13 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
185192
left: `0%`,
186193
top:'100%',
187194
position:'absolute',
188-
width:`${String(newMin).length*8}px`,
195+
width:`${Num2String(newMin*Math.pow(10,scalingFactor??0)).length*8}px`,
189196
transform:'translateX(-50%)',
190197
textAlign:'right',
191198
minWidth:'30px'
192199
}}
193-
value={newMin}
194-
onChange={e=>setNewMin(TwoDecimals(parseFloat(e.target.value)))}
200+
value={Num2String(newMin*Math.pow(10,scalingFactor??0))}
201+
onChange={e=>setNewMin(parseFloat(e.target.value))}
195202
/>
196203
{Array.from({length: tickCount}).map((_val,idx)=>{
197204
if (idx == 0 || idx == tickCount-1){
@@ -205,7 +212,7 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
205212
position:'absolute',
206213
transform:'translateX(-50%)',
207214
}}
208-
>{vals[idx].toFixed(2)}
215+
>{Num2String(vals[idx]*Math.pow(10,scalingFactor??0))}
209216
</p>)}
210217
)}
211218
<input type="number"
@@ -214,13 +221,13 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
214221
left: `100%`,
215222
top:'100%',
216223
position:'absolute',
217-
width:`${String(newMax).length*9+1}px`,
224+
width:`${Num2String(newMax*Math.pow(10,scalingFactor??0)).length*9+1}px`,
218225
transform:'translateX(-50%)',
219226
textAlign:'right',
220227
minWidth:'30px'
221228
}}
222-
value={newMax}
223-
onChange={e=>setNewMax(TwoDecimals(parseFloat(e.target.value)))}
229+
value={Num2String(newMax*Math.pow(10,scalingFactor??0))}
230+
onChange={e=>setNewMax(parseFloat(e.target.value))}
224231
/>
225232
<canvas id="colorbar-canvas" ref={canvasRef} width={512} height={24} onMouseDown={handleMouseDown}/>
226233
<p className="colorbar-title"
@@ -250,7 +257,6 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
250257
<FaPlus className='cursor-pointer' onClick={()=>setTickCount(Math.min(tickCount+1, 10))}/>
251258
</div>
252259
</div>
253-
254260
</>
255261

256262
)

src/components/zarr/ZarrLoaderLRU.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,34 @@ function ToFloat16(array : Float32Array, scalingFactor: number | null) : [Float1
1616
let newArray : Float16Array;
1717
let newScalingFactor: number | null = null;
1818
const [minVal, maxVal] = ArrayMinMax(array)
19-
if (maxVal <= 65504 && minVal >= -65504 && (Math.abs(maxVal) > 1e-3)){ // If values fit in Float16, use that to save memory
19+
if (maxVal <= 65504 && minVal >= -65504 && Math.abs(maxVal) > 1e-3 && Math.abs(minVal) > 1e-3){ // If values fit in Float16, use that to save memory
2020
newArray = new Float16Array(array)
2121
}
2222
else{
2323
if ((Math.abs(maxVal) < 1e-3)){ // If low precision it will bump up
2424
newScalingFactor = Math.floor(Math.log10(maxVal))
2525
newScalingFactor = scalingFactor ? (scalingFactor < newScalingFactor ? scalingFactor : newScalingFactor) : newScalingFactor
26-
console.log(Math.pow(10,newScalingFactor))
2726
for (let i = 0; i < array.length; i++) {
2827
array[i] /= Math.pow(10,newScalingFactor);
2928
}
30-
newArray = new Float16Array(array)
29+
} else if (Math.abs(minVal) < 1e-3 ){
30+
const minFactor = Math.floor(Math.log10(Math.abs(minVal)))
31+
const newMax = maxVal/Math.pow(10, minFactor)
32+
const maxFactor = Math.ceil(Math.log10(newMax /65504))
33+
newScalingFactor = maxFactor + minFactor
34+
newScalingFactor = scalingFactor ? (scalingFactor < newScalingFactor ? scalingFactor : newScalingFactor) : newScalingFactor
35+
for (let i = 0; i < array.length; i++) {
36+
array[i] /= Math.pow(10,newScalingFactor);
37+
}
38+
3139
} else {
3240
newScalingFactor = Math.ceil(Math.log10(maxVal/65504))
3341
newScalingFactor = scalingFactor ? (scalingFactor > newScalingFactor ? scalingFactor : newScalingFactor) : newScalingFactor
3442
for (let i = 0; i < array.length; i++) {
3543
array[i] /= Math.pow(10,newScalingFactor);
3644
}
37-
newArray = new Float16Array(array)
3845
}
46+
newArray = new Float16Array(array)
3947
}
4048
return [newArray, newScalingFactor]
4149
}
@@ -243,7 +251,7 @@ export async function GetArray(): Promise<{
243251

244252
//---- 3. Determine Structure ----//
245253
const fullShape = outVar.shape;
246-
let [totalSize, _chunkSize, chunkShape] = GetSize(outVar);
254+
let [_totalSize, _chunkSize, chunkShape] = GetSize(outVar);
247255

248256
if (is4D){
249257
chunkShape = chunkShape.slice(1);

src/utils/GlobalStates.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type StoreState = {
4141
status: string | null;
4242
progress: number;
4343
DPR: number,
44-
44+
scalingFactor: number | null;
4545
is4D: boolean;
4646
idx4D: number | null;
4747
titleDescription: { title: string | null; description: string | null };
@@ -78,6 +78,7 @@ type StoreState = {
7878
setTextureArrayDepths: (textureArrayResolution: number[] ) => void;
7979
setTextureData: (textureData: Uint8Array ) => void;
8080
setDPR: (DPR: number) => void;
81+
setScalingFactor: (scalingFactor: number | null) => void;
8182
};
8283

8384
export const useGlobalStore = create<StoreState>((set, get) => ({
@@ -108,6 +109,7 @@ export const useGlobalStore = create<StoreState>((set, get) => ({
108109
textureArrayDepths: [1,1,1],
109110
textureData: new Uint8Array(1),
110111
DPR: 1,
112+
scalingFactor: null,
111113

112114
setDataShape: (dataShape) => set({ dataShape }),
113115
setShape: (shape) => set({ shape }),
@@ -157,6 +159,7 @@ export const useGlobalStore = create<StoreState>((set, get) => ({
157159
setTextureArrayDepths: (textureArrayDepths) => set({ textureArrayDepths }),
158160
setTextureData: (textureData) => set({ textureData }),
159161
setDPR: (DPR) => set({ DPR }),
162+
setScalingFactor: (scalingFactor) => set({ scalingFactor }),
160163
}));
161164

162165
type PlotState ={

src/utils/HelperFuncs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ export function TwoDecimals(val: number){
283283
return Math.round(val * 100)/100
284284
}
285285

286+
286287
export async function GetDimInfo(variable:string){
287288
const {cache} = useCacheStore.getState();
288289
const {initStore} = useGlobalStore.getState();

0 commit comments

Comments
 (0)