Skip to content

Commit

Permalink
added quantiletransformer and robustScaler functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanho committed Sep 20, 2024
1 parent a34b5ef commit fae9174
Show file tree
Hide file tree
Showing 2 changed files with 1,102 additions and 1 deletion.
104 changes: 103 additions & 1 deletion src/ucar/unidata/data/grid/GridMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package ucar.unidata.data.grid;


import ucar.unidata.data.DataUtil;
import ucar.unidata.util.Misc;
import ucar.unidata.util.Range;
Expand Down Expand Up @@ -3775,4 +3774,107 @@ public static Statistics statisticsFF(FlatField grid)
return s;

}

public static FlatField quantileTransformerFF(FlatField grid)
throws VisADException {

FlatField newField = null;
try {
GriddedSet domainSet =
(GriddedSet) GridUtil.getSpatialDomain(grid);
int[] lengths = domainSet.getLengths();
int sizeX = lengths[0];
int sizeY = lengths[1];
int sizeZ = ((lengths.length == 2)
|| (domainSet.getManifoldDimension() == 2))
? 1
: lengths[2];
float[] samples = grid.getFloats()[0];

float[][] newValues = new float[1][sizeX * sizeY * sizeZ];
GridUtil.QuantileTransformer qt = new GridUtil.QuantileTransformer(samples.length, samples.length);
qt.fit(samples);

// Get the quantiles
float[] x_transformed = qt.transform(samples, float[].class, true);
qt.shutdown();
FunctionType newFT = (FunctionType)grid.getType();
newValues[0] = x_transformed;
newField = new FlatField(newFT, grid.getDomainSet());
newField.setSamples(newValues, false);

} catch (RemoteException re) {
throw new VisADException("RemoteException checking missing data");
}
return newField;

}

public static FlatField powerTransformerFF(FlatField grid, double lambda)
throws VisADException {

FlatField newField = null;
try {
GriddedSet domainSet =
(GriddedSet) GridUtil.getSpatialDomain(grid);
int[] lengths = domainSet.getLengths();
int sizeX = lengths[0];
int sizeY = lengths[1];
int sizeZ = ((lengths.length == 2)
|| (domainSet.getManifoldDimension() == 2))
? 1
: lengths[2];
float[] samples = grid.getFloats()[0];

float[][] newValues = new float[1][sizeX * sizeY * sizeZ];

// Get the quantiles
float[] x_transformed = GridUtil.yeoJohnsonTransform(samples, lambda);

FunctionType newFT = (FunctionType)grid.getType();
newValues[0] = x_transformed;
newField = new FlatField(newFT, grid.getDomainSet());
newField.setSamples(newValues, false);

} catch (RemoteException re) {
throw new VisADException("RemoteException checking missing data");
}
return newField;

}

public static FlatField robustScalerFF(FlatField grid)
throws VisADException {

FlatField newField = null;
try {
GriddedSet domainSet =
(GriddedSet) GridUtil.getSpatialDomain(grid);
int[] lengths = domainSet.getLengths();
int sizeX = lengths[0];
int sizeY = lengths[1];
int sizeZ = ((lengths.length == 2)
|| (domainSet.getManifoldDimension() == 2))
? 1
: lengths[2];
float[][] samples = grid.getFloats();

float[][] newValues = new float[1][sizeX * sizeY * sizeZ];
GridUtil.RobustScaler scaler = new GridUtil.RobustScaler();
scaler.fit(samples); // Compute medians and IQR

// Transform the data
float[][] scaledData = scaler.transform(samples);

FunctionType newFT = (FunctionType)grid.getType();

newField = new FlatField(newFT, grid.getDomainSet());
newField.setSamples(scaledData, false);

} catch (RemoteException re) {
throw new VisADException("RemoteException checking missing data");
}
return newField;

}
}
Loading

0 comments on commit fae9174

Please sign in to comment.