add count_nonzero function

This commit is contained in:
wyq 2025-04-12 22:46:55 +08:00
parent ac4813b1a6
commit 80a028992a
10 changed files with 72 additions and 21 deletions

View File

@ -36,12 +36,21 @@ public class MIMath {
* @return boolean
*/
public static boolean doubleEquals(double a, double b) {
double difference = Math.abs(a * 0.00001);
if (Math.abs(a - b) <= difference) {
return true;
} else {
return false;
}
return doubleEquals(a, b, 0.0000001);
}
/**
* Determine if two double data equal
*
* @param a double a
* @param b double b
* @param epsilon Epsilon
* @return boolean
*/
public static boolean doubleEquals(final double a, final double b, final double epsilon) {
final double diff = Math.abs(a - b);
return diff < epsilon
|| (Double.isNaN(diff) && a == b); // Handle the case where a = b = Double.POSITIVE_INFINITY or a = b = Double.NEGATIVE_INFINITY.
}
/**

View File

@ -736,7 +736,7 @@ public class GridArray {
* @param aFile File path
*/
public void saveAsESRIASCIIFile(String aFile) {
if (this.getXDelt() != this.getYDelt()){
if (!MIMath.doubleEquals(this.getXDelt(), this.getYDelt())){
JOptionPane.showMessageDialog(null, "X cell size is not equal y cell size!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

View File

@ -60,6 +60,9 @@ public class NCUtil {
if (dt == DataType.OBJECT && ncArray.getObject(0).getClass() == String.class){
dt = DataType.STRING;
}
//Array array = Array.factory(dt, ncArray.getShape(), ncArray.getStorage());
Array array = null;
switch (dt) {
case STRUCTURE:
@ -70,7 +73,7 @@ public class NCUtil {
default:
array = Array.factory(dt, ncArray.getShape(), ncArray.getStorage());
break;
}
}
return array;
}

View File

@ -1,28 +1,32 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MeteoInfo File="milconfig.xml" Type="configurefile">
<Path OpenPath="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map\webmap"/>
<Path OpenPath="D:\Working\MIScript\Jython\mis\array">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map\geoshow"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\contour"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\json"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\grib"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\linalg"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\burf"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\json"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map\webmap"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
</Path>
<File>
<OpenedFiles>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\contour\conoutm.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\map\webmap\hy_conc_nuclear.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\array\count_nonzero.py"/>
</OpenedFiles>
<RecentFiles>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\contour\conoutm.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\map\webmap\hy_conc_nuclear.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\array\count_nonzero.py"/>
</RecentFiles>
</File>
<Font>

View File

@ -28,7 +28,7 @@ class DimVariable(object):
@staticmethod
def factory(variable=None, dataset=None, ncvariable=None):
"""
Factor method.
Factory method.
"""
if variable.getDataType().isStructure():
return StructureVariable(variable, dataset)

View File

@ -39,8 +39,8 @@ __all__ = [
'argmin','argmax','argsort','array','array_split','amax','amin','asanyarray','asarray',
'arcsin','asin','asmiarray','atleast_1d','atleast_2d','arctan','atan',
'arctan2','atan2','average','histogram','broadcast_to','cdiff','ceil',
'concatenate','conj','conjugate','convolve','corrcoef','cos','cosh','cylinder','degrees','delnan','diag','diff',
'dot','empty','empty_like','exp','eye','flatnonzero','floor',
'concatenate','conj','conjugate','convolve','corrcoef','cos','cosh','count_nonzero','cylinder',
'degrees','delnan','diag','diff','dot','empty','empty_like','exp','eye','flatnonzero','floor',
'fmax','fmin','full','hcurl','hdivg','hstack','hypot','identity','indices','interp2d','interpn','isarray',
'isclose','isfinite','isinf','isnan','isscalar','linspace','log','log10','logical_not','logspace',
'magic','magnitude','max','maximum','mean','median','meshgrid','min','minimum','monthname',
@ -445,6 +445,36 @@ def full(shape, fill_value, dtype=None):
return NDArray(ArrayUtil.full(shapelist, fill_value.asarray(), dtype))
else:
return NDArray(ArrayUtil.full(shapelist, fill_value, dtype))
def count_nonzero(a, axis=None):
"""
Counts the number of non-zero values in the array ``a``.
Parameters
----------
a : array_like
The array for which to count non-zeros.
axis : int or tuple, optional
Axis or tuple of axes along which to count non-zeros.
Default is None, meaning that non-zeros will be counted
along a flattened version of ``a``.
Returns
-------
count : int or array of int
Number of non-zero values in the array along a given axis.
Otherwise, the total number of non-zero values in the array
is returned.
"""
a = asanyarray(a)
a = a.astype(_dtype.bool)
r = a.sum(axis=axis)
if isinstance(r, NDArray):
r = r.astype(_dtype.int)
else:
r = int(r)
return r
def identity(n, dtype='float'):
"""

View File

@ -286,7 +286,7 @@ public class ArrayObject extends Array {
* not legal, throw ForbiddenConversionException
*/
public String getString(Index i) {
throw new ForbiddenConversionException();
return getObject(i).toString();
}
/**
@ -403,7 +403,7 @@ public class ArrayObject extends Array {
}
public String getString(int index) {
throw new ForbiddenConversionException();
return getObject(index).toString();
}
public void setString(int index, String value) {

View File

@ -7397,7 +7397,12 @@ public class ArrayMath {
}
shape[idx] = dataShape[i];
}
Array r = Array.factory(a.getDataType(), shape);
DataType dataType = a.getDataType();
if (dataType == DataType.BOOLEAN) {
dataType = DataType.INT;
}
Array r = Array.factory(dataType, shape);
double s;
Index indexr = r.getIndex();
int[] current;