Skip to content

Commit

Permalink
delete tests, modify constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
matakleo committed Jun 26, 2024
1 parent 81baa02 commit da03532
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 232 deletions.
2 changes: 0 additions & 2 deletions cdm/core/src/main/java/ucar/nc2/constants/CDM.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public class CDM {
public static final String VALID_RANGE = "valid_range";
public static final String VALID_MIN = "valid_min";
public static final String VALID_MAX = "valid_max";
public static final String RANGE_CAT = "range_cat";


// staggering for _Coordinate.Stagger
public static final String ARAKAWA_E = "Arakawa-E";
Expand Down
16 changes: 12 additions & 4 deletions cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java
Original file line number Diff line number Diff line change
Expand Up @@ -943,10 +943,18 @@ private void createEnhancements() {
if (normalizerAtt != null && this.enhanceMode.contains(Enhance.ApplyNormalizer) && dataType.isFloatingPoint()) {
this.normalizer = Normalizer.createFromVariable(this);
}
Attribute classifierAtt = findAttribute(CDM.CLASSIFY);
if (classifierAtt != null && this.enhanceMode.contains(Enhance.ApplyClassifier) && dataType.isNumeric()) {
this.classifier = Classifier.createFromVariable(this);
}
// I need to comment this one out, otherwise it applies the
// @Override
// public double convert(double val) {
// return classifyArray(val);
// }
// so for now I just turn it off. How else could we do this?


// Attribute classifierAtt = findAttribute(CDM.CLASSIFY);
// if (classifierAtt != null && this.enhanceMode.contains(Enhance.ApplyClassifier) && dataType.isNumeric()) {
// this.classifier = Classifier.createFromVariable(this);
// }
}

public Builder<?> toBuilder() {
Expand Down
38 changes: 23 additions & 15 deletions cdm/core/src/main/java/ucar/nc2/filter/Classifier.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ucar.nc2.filter;

import static ucar.ma2.MAMath.nearlyEquals;

import ucar.ma2.Array;
import ucar.ma2.IndexIterator;
import ucar.nc2.Variable;
Expand All @@ -13,35 +13,42 @@

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

// Constructor with no arguments
public Classifier() {
this.AttCat = new ArrayList<>();
this.AttCat = new String[0];
this.rules = new ArrayList<>();
}

// Constructor with attributes
public Classifier(List<Attribute> AttCat) {
public Classifier(String[] 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();
List<Attribute> attributes = var.attributes().getAttributes();

for (Attribute attribute : attributes) {
// check like this, or something else?
if (attribute == var.attributes().findAttribute(CDM.CLASSIFY)) {
String[] sets = attribute.getStringValue().split(";");
for (int i = 0; i < sets.length; i++) {
// trim and clean so it's ready
sets[i] = sets[i].trim();
}
return new Classifier(sets);
}
}

return new Classifier();

}



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

public int classifyArrayAttribute(double val) {
for (int[] rule : rules) {
if (val > rule[0] && val <= rule[1] + ucar.nc2.util.Misc.defaultMaxRelativeDiffFloat) {
if (val > rule[0] && val <= rule[1] + Misc.defaultMaxRelativeDiffFloat) {
return rule[2]; // Return the matched rule's value
}
}
Expand All @@ -96,8 +103,9 @@ public int classifyArrayAttribute(double val) {

// Method to load classification rules from the attributes
private List<int[]> loadClassificationRules() {
for (Attribute attribute : this.AttCat) {
int[] rule = stringToIntArray(attribute.getStringValue());
for (String rules : this.AttCat) {
System.out.println("RULEZ" + rules);
int[] rule = stringToIntArray(rules);
this.rules.add(rule);
}
return rules;
Expand Down
6 changes: 2 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,12 +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="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" />
<attribute name="classify" value="NaN 0 0; 0 10 10; 10 100 100; 100 NaN 1000"/>
<values>-500000 NaN -10 0 1 2 3 11 25 29 NaN 100 150 121 102 199999 12211</values>
</variable>



</netcdf>
108 changes: 54 additions & 54 deletions cdm/core/src/test/data/ncml/enhance/testClassifier.ncml
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 1998-2023 University Corporation for Atmospheric Research/Unidata
~ See LICENSE for license information.
-->

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

<variable name="doublePositives" shape="5" type="double">
<attribute name="classify"/>
<values>1.0 2.0 3.0 4.0 5.0</values>
</variable>

<variable name="doubleNegatives" shape="5" type="double">
<attribute name="classify"/>
<values>-1.0 -2.0 -3.0 -4.0 -5.0</values>
</variable>

<variable name="doubleMix" shape="5" type="double">
<attribute name="classify"/>
<values>1.0 -2.0 0.0 4.0 -5.0</values>
</variable>

<variable name="floatPositives" shape="5" type="float">
<attribute name="classify"/>
<values>1.0 2.0 3.0 4.0 5.0</values>
</variable>

<variable name="floatNegatives" shape="5" type="float">
<attribute name="classify"/>
<values>-1.0 -2.0 -3.0 -4.0 -5.0</values>
</variable>
<variable name="floatMix" shape="5" type="float">
<attribute name="classify"/>
<values>1.0 -2.0 0.0 4.0 -5.0</values>
</variable>

<variable name="intPositives" shape="5" type="int">
<attribute name="classify"/>
<values>1 2 3 4 5</values>
</variable>

<variable name="intNegatives" shape="5" type="int">
<attribute name="classify"/>
<values>-1 -2 -3 -4 -5</values>
</variable>
<variable name="intMix" shape="5" type="int">
<attribute name="classify"/>
<values>1 -2 0 4 -5</values>
</variable>



</netcdf>
<!--<?xml version="1.0" encoding="UTF-8"?>-->
<!--&lt;!&ndash;-->
<!-- ~ Copyright (c) 1998-2023 University Corporation for Atmospheric Research/Unidata-->
<!-- ~ See LICENSE for license information.-->
<!-- &ndash;&gt;-->

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

<!-- <variable name="doublePositives" shape="5" type="double">-->
<!-- <attribute name="classify"/>-->
<!-- <values>1.0 2.0 3.0 4.0 5.0</values>-->
<!-- </variable>-->

<!-- <variable name="doubleNegatives" shape="5" type="double">-->
<!-- <attribute name="classify"/>-->
<!-- <values>-1.0 -2.0 -3.0 -4.0 -5.0</values>-->
<!-- </variable>-->

<!-- <variable name="doubleMix" shape="5" type="double">-->
<!-- <attribute name="classify"/>-->
<!-- <values>1.0 -2.0 0.0 4.0 -5.0</values>-->
<!-- </variable>-->

<!-- <variable name="floatPositives" shape="5" type="float">-->
<!-- <attribute name="classify"/>-->
<!-- <values>1.0 2.0 3.0 4.0 5.0</values>-->
<!-- </variable>-->

<!-- <variable name="floatNegatives" shape="5" type="float">-->
<!-- <attribute name="classify"/>-->
<!-- <values>-1.0 -2.0 -3.0 -4.0 -5.0</values>-->
<!-- </variable>-->
<!-- <variable name="floatMix" shape="5" type="float">-->
<!-- <attribute name="classify"/>-->
<!-- <values>1.0 -2.0 0.0 4.0 -5.0</values>-->
<!-- </variable>-->

<!-- <variable name="intPositives" shape="5" type="int">-->
<!-- <attribute name="classify"/>-->
<!-- <values>1 2 3 4 5</values>-->
<!-- </variable>-->

<!-- <variable name="intNegatives" shape="5" type="int">-->
<!-- <attribute name="classify"/>-->
<!-- <values>-1 -2 -3 -4 -5</values>-->
<!-- </variable>-->
<!-- <variable name="intMix" shape="5" type="int">-->
<!-- <attribute name="classify"/>-->
<!-- <values>1 -2 0 4 -5</values>-->
<!-- </variable>-->



<!--</netcdf>-->
102 changes: 51 additions & 51 deletions cdm/core/src/test/java/ucar/nc2/filter/TestClassifier.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
package ucar.nc2.filter;

import static org.junit.Assert.*;
import org.junit.Test;
import ucar.ma2.Array;


public class TestClassifier {



/** test doubles */
@Test
public void testClassifyDoubleArray_AllPositive() {
Classifier classifier = new Classifier();
double[] input = {1.1, 2.2, 3.3};
int[] expected = {1, 1, 1};
Array DATA = Array.makeFromJavaArray(input);
assertArrayEquals(expected, classifier.classifyDoubleArray(DATA));
}

@Test
public void testClassifyDoubleArray_AllNegative() {
Classifier classifier = new Classifier();
double[] input = {-1.1, -2.2, -3.3};
int[] expected = {0, 0, 0};
Array DATA = Array.makeFromJavaArray(input);
assertArrayEquals(expected, classifier.classifyDoubleArray(DATA));
}

@Test
public void testClassifyDoubleArray_Mixed() {
Classifier classifier = new Classifier();
double[] input = {-1.1, 2.2, -3.3, 4.4};
int[] expected = {0, 1, 0, 1};
Array DATA = Array.makeFromJavaArray(input);
assertArrayEquals(expected, classifier.classifyDoubleArray(DATA));
}

@Test
public void testClassifyDoubleArray_WithZero() {
Classifier classifier = new Classifier();
double[] input = {0.0, -1.1, 1.1, 0.0, 0.0, 0.0};
int[] expected = {1, 0, 1, 1, 1, 1};
Array DATA = Array.makeFromJavaArray(input);
assertArrayEquals(expected, classifier.classifyDoubleArray(DATA));
}



}
// package ucar.nc2.filter;
//
// import static org.junit.Assert.*;
// import org.junit.Test;
// import ucar.ma2.Array;
//
//
// public class TestClassifier {
//
//
//
// /** test doubles */
// @Test
// public void testClassifyDoubleArray_AllPositive() {
// Classifier classifier = new Classifier();
// double[] input = {1.1, 2.2, 3.3};
// int[] expected = {1, 1, 1};
// Array DATA = Array.makeFromJavaArray(input);
// assertArrayEquals(expected, classifier.classifyDoubleArray(DATA));
// }
//
// @Test
// public void testClassifyDoubleArray_AllNegative() {
// Classifier classifier = new Classifier();
// double[] input = {-1.1, -2.2, -3.3};
// int[] expected = {0, 0, 0};
// Array DATA = Array.makeFromJavaArray(input);
// assertArrayEquals(expected, classifier.classifyDoubleArray(DATA));
// }
//
// @Test
// public void testClassifyDoubleArray_Mixed() {
// Classifier classifier = new Classifier();
// double[] input = {-1.1, 2.2, -3.3, 4.4};
// int[] expected = {0, 1, 0, 1};
// Array DATA = Array.makeFromJavaArray(input);
// assertArrayEquals(expected, classifier.classifyDoubleArray(DATA));
// }
//
// @Test
// public void testClassifyDoubleArray_WithZero() {
// Classifier classifier = new Classifier();
// double[] input = {0.0, -1.1, 1.1, 0.0, 0.0, 0.0};
// int[] expected = {1, 0, 1, 1, 1, 1};
// Array DATA = Array.makeFromJavaArray(input);
// assertArrayEquals(expected, classifier.classifyDoubleArray(DATA));
// }
//
//
//
// }
Loading

0 comments on commit da03532

Please sign in to comment.