Skip to content

Commit

Permalink
applied style
Browse files Browse the repository at this point in the history
  • Loading branch information
matakleo committed Jun 18, 2024
1 parent 52f6564 commit a751329
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 96 deletions.
106 changes: 22 additions & 84 deletions cdm/core/src/main/java/ucar/nc2/filter/Classifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,26 @@

import java.io.IOException;
import ucar.ma2.Array;
import ucar.ma2.DataType;
import ucar.ma2.IndexIterator;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import ucar.nc2.dataset.NetcdfDatasets;
import ucar.nc2.dataset.VariableDS;
import ucar.nc2.Attribute;
import ucar.nc2.dataset.NetcdfDatasets;
import ucar.nc2.constants.CDM;
import java.nio.*;
import java.util.HashMap;
import java.util.Map;
import ucar.nc2.Attribute;
import ucar.nc2.Variable;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Arrays;

import static ucar.ma2.DataType.*;
import static ucar.ma2.MAMath.nearlyEquals;

public class Classifier implements Enhancement {
private Classifier classifier = null;
private static Classifier emptyClassifier;
private static Classifier specidifedClassifier;
private int classifiedVal;
private static int CatStart;
private static int CatEnd;
private static int CatLabel;
private int[] classifiedArray;
private List<Attribute> AttCat;
private List<int[]> rules = new ArrayList<>();

public static Classifier createFromVariable(VariableDS var) {
try {
Array arr = var.read();
// // DataType type = var.getDataType();
Attribute classifier_config = var.findAttribute(CDM.RANGE_CAT);
if (classifier_config != null && !classifier_config.isString()) {
System.out.println(classifier_config.getNumericValue());

// var.remove(scaleAtt);
}
return emptyClassifier();
} catch (IOException e) {
return emptyClassifier();
}
}

public static Classifier createFromVariable(Variable var) {
try {
Array arr = var.read();
return emptyClassifier();
} catch (IOException e) {
return emptyClassifier();
Expand All @@ -69,22 +36,22 @@ public static Classifier emptyClassifier() {

/** Enough of a constructor */
public Classifier() {}

public Classifier(List<Attribute> AttCat) {
this.AttCat = AttCat;
this.rules = loadClassificationRules();

}

public int[] classifyWithAttributes (Array arr){
public int[] classifyWithAttributes(Array arr) {
int[] classifiedArray = new int[(int) arr.getSize()];
IndexIterator iterArr = arr.getIndexIterator();
int i =0;
int i = 0;
while (iterArr.hasNext()) {
Number value = (Number) iterArr.getObjectNext();
if (!Double.isNaN(value.doubleValue())) {
classifiedArray[i] = classifyArrayAttribute(value.doubleValue(),this.rules);
}
else {
classifiedArray[i] = classifyArrayAttribute(value.doubleValue());
} else {
classifiedArray[i] = Integer.MIN_VALUE;
}
i++;
Expand All @@ -109,28 +76,23 @@ public int[] classifyDoubleArray(Array arr) {
return classifiedArray;
}





/** for a single double */
public int classifyArray(double val) {
if (val >= 0) {
classifiedVal = 1;
} else {
classifiedVal = 0;
}

return classifiedVal;
}

public int classifyArrayAttribute(double val, List<int[]> rules) {
public int classifyArrayAttribute(double val) {
for (int[] rule : rules) {
if (val > rule[0] && val <= rule[1]) {
return rule[2]; // Return the matched rule's value
return rule[2]; // Return the matched rule's value
}
}
// Return Integer.MIN_VALUE if no rule matches
// Return min possible int if no rule matches
return Integer.MIN_VALUE;
}

Expand All @@ -147,58 +109,34 @@ public List<int[]> loadClassificationRules() {
public double convert(double val) {
return emptyClassifier.classifyArray(val);
}

public static int[] stringToIntArray(String str) {
String[] stringArray = str.split(" "); // Split the string by spaces
int[] intArray = new int[stringArray.length]; // Create an array to hold the parsed integers

for (int i = 0; i < stringArray.length; i++) {
try {
double value = Double.parseDouble(stringArray[i]); // Parse each string to a double

if (Double.isNaN(value)) {
// Check if the entry is NaN and assign Integer.MIN_VALUE or Integer.MAX_VALUE based on the index
if (i == 0) {
intArray[i] = Integer.MIN_VALUE;
} else if (i == 1) {
intArray[i] = Integer.MAX_VALUE;
} else {
intArray[i] = -99999; // Default value for other indices if needed
}

double value = Double.parseDouble(stringArray[i]); // Parse each string to a double

if (Double.isNaN(value)) {
// Check if the entry is NaN and assign Integer.MIN_VALUE or Integer.MAX_VALUE based on the index
if (i == 0) {
intArray[i] = Integer.MIN_VALUE;
} else if (i == 1) {
intArray[i] = Integer.MAX_VALUE;
} else {
intArray[i] = (int) value; // Convert the value to int if it is not NaN
intArray[i] = -99999; // Default value for other indices if needed
}
} catch (NumberFormatException e) {
// Handle the case where the string cannot be parsed to a number
intArray[i] = 0; // Default value for invalid numbers
} else {
intArray[i] = (int) value; // Convert the value to int if it is not NaN
}
}

return intArray;
}

public static void main(String[] args) {

String dataDir = "/Users/lmatak/Desktop/netCDF-Java/netcdf-java/cdm/core/src/test/data/ncml/enhance/";

try (NetcdfFile ncfile = NetcdfDatasets.openDataset(dataDir + "testAddToClassifier.ncml", true, null)) {

Variable Classify_Specsx = ncfile.findVariable("class_specs");
Array Data = Classify_Specsx.read();
List<Attribute> Whatever = Classify_Specsx.getAttributes();
Classifier classifier = new Classifier(Whatever);
int[] ClassifiedArray = classifier.classifyWithAttributes(Data);
// Print the entire array
System.out.println(Arrays.toString(ClassifiedArray));

} catch (IOException e) {
throw new RuntimeException(e);
}


return intArray;
}



}


8 changes: 4 additions & 4 deletions cdm/core/src/test/data/ncml/enhance/testAddToClassifier.ncml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" enhance="all">

<variable name="class_specs" shape="14" type="int">
<attribute name="Cat1" type="string" value="-100 0 0" />
<variable name="class_specs" shape="14" type="double">
<attribute name="Cat1" type="string" value="NaN 0 0" />
<attribute name="Cat2" type="string" value="0 10 10" />
<attribute name="Cat3" type="string" value="10 100 100" />
<attribute name="Cat4" type="string" value="100 1000 -1000" />
<values>-5000 -20 -10 0 1 2 3 11 25 29 99 100 150 121 102</values>
<attribute name="Cat4" type="string" value="100 NaN 1000" />
<values>-500000 NaN -10 0 1 2 3 11 25 29 NaN 100 150 121 102 199999 12211</values>
</variable>


Expand Down
28 changes: 20 additions & 8 deletions cdm/core/src/test/java/ucar/nc2/ncml/TestEnhanceClassifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import static ucar.ma2.MAMath.nearlyEquals;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import ucar.ma2.Array;
import ucar.ma2.DataType;
import ucar.nc2.Attribute;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import ucar.nc2.dataset.NetcdfDatasets;
import ucar.nc2.filter.Classifier;
import ucar.unidata.util.test.TestDir;

public class TestEnhanceClassifier {
Expand All @@ -22,7 +26,9 @@ public class TestEnhanceClassifier {
public static final Array DATA_all_zeroes = Array.makeFromJavaArray(all_zeroes);
public static final int[] mixNumbers = {1, 0, 1, 1, 0};
public static final Array DATA_mixNumbers = Array.makeFromJavaArray(mixNumbers);

public static final int[] Classification_test =
{0, -2147483648, 0, 0, 10, 10, 10, 100, 100, 100, -2147483648, 100, 1000, 1000, 1000, 1000, 1000};
public static final Array CLASSIFICATION_TEST = Array.makeFromJavaArray(Classification_test);

/** test on doubles, all positives, all negatives and a mixed array */
@Test
Expand Down Expand Up @@ -112,18 +118,24 @@ public void testEnhanceClassifier_integers() throws IOException {
}

}

@Test
public void testEnhanceClassifier_classification() throws IOException {

try (NetcdfFile ncfile = NetcdfDatasets.openDataset(dataDir + "testAddToClassifier.ncml", true, null)) {

Variable class_specs = ncfile.findVariable("class_specs");
assertThat((Object) class_specs).isNotNull();
assertThat(!class_specs.attributes().isEmpty());
assertThat(class_specs.attributes().hasAttribute("habla"));
Array numbers_to_classify = class_specs.read();
/** So here we are, we want to read an array and apply the attribute classifications */
// assertThat(nearlyEquals(datafloats, DATA_all_ones)).isTrue();
Variable Classify_Specsx = ncfile.findVariable("class_specs");
assertThat((Object) Classify_Specsx).isNotNull();
assertThat(!Classify_Specsx.attributes().isEmpty()).isTrue();
Array Data = Classify_Specsx.read();
List<Attribute> Whatever = Classify_Specsx.getAttributes();
Classifier classifier = new Classifier(Whatever);
int[] ClassifiedArray = classifier.classifyWithAttributes(Data);
assertThat(nearlyEquals(Array.makeFromJavaArray(ClassifiedArray), CLASSIFICATION_TEST)).isTrue();


} catch (IOException e) {
throw new RuntimeException(e);
}

}
Expand Down

0 comments on commit a751329

Please sign in to comment.