Skip to content

Commit

Permalink
fix pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
matakleo committed Jun 25, 2024
1 parent a47ebc5 commit ece1779
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
46 changes: 22 additions & 24 deletions cdm/core/src/main/java/ucar/nc2/filter/Classifier.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
package ucar.nc2.filter;

import java.io.IOException;
import ucar.ma2.Array;
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.constants.CDM;
import ucar.nc2.Attribute;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;


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

public static Classifier createFromVariable(VariableDS var) {
try {
Array arr = var.read();
return emptyClassifier();
} catch (IOException e) {
return emptyClassifier();
}
}

public static Classifier emptyClassifier() {
emptyClassifier = new Classifier();
return emptyClassifier;
// Constructor with no arguments
public Classifier() {
this.AttCat = new ArrayList<>();
this.rules = new ArrayList<>();
}

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

// Constructor with attributes
public Classifier(List<Attribute> AttCat) {
this.AttCat = AttCat;
this.rules = loadClassificationRules();
}

// Factory method to create a Classifier from a Variable
public static Classifier createFromVariable(Variable var) {

List<Attribute> attributes = var.getAttributes();

if (var.attributes().hasAttribute(CDM.RANGE_CAT)) {
return new Classifier(attributes);
} else {
return new Classifier();
}

}


public int[] classifyWithAttributes(Array arr) {
int[] classifiedArray = new int[(int) arr.getSize()];
IndexIterator iterArr = arr.getIndexIterator();
Expand Down Expand Up @@ -95,7 +93,7 @@ public int classifyArrayAttribute(double val) {
}

// Method to load classification rules from the attributes
public List<int[]> loadClassificationRules() {
private List<int[]> loadClassificationRules() {
for (Attribute attribute : this.AttCat) {
int[] rule = stringToIntArray(attribute.getStringValue());
this.rules.add(rule);
Expand All @@ -105,7 +103,7 @@ public List<int[]> loadClassificationRules() {

@Override
public double convert(double val) {
return emptyClassifier.classifyArray(val);
return classifyArray(val);
}

public static int[] stringToIntArray(String str) {
Expand Down
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 @@ -8,10 +8,10 @@
<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" enhance="all">

<variable name="class_specs" shape="17" 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 NaN 1000" />
<attribute name="range_cat" type="string" value="NaN 0 0" />
<attribute name="range_cat1" type="string" value="0 10 10" />
<attribute name="range_cat2" type="string" value="10 100 100" />
<attribute name="range_cat3" 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
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ public void testEnhanceClassifier_classification() throws IOException {
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);
Classifier classifier = Classifier.createFromVariable(Classify_Specsx);
int[] ClassifiedArray = classifier.classifyWithAttributes(Data);
assertThat(nearlyEquals(Array.makeFromJavaArray(ClassifiedArray), CLASSIFICATION_TEST)).isTrue();

Expand Down

0 comments on commit ece1779

Please sign in to comment.