Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support numpy/python 32 bit float type names #800

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion doc/BMIconventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,19 @@ Since the [BMI Documentation] simply states that, "Use of native language type n
* `get_var_itemsize` result **must** match the size of the data type in the compiler used to build ngen
* Python
* `int`, `long`, `long long`, `int64`, `longlong`, `float`, `float64`, `long double`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This missed documenting that float32 is accepted

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(without a numpy. or np. prefix)

* also accepts `numpy.float64` and `np.float64` but this usage is discouraged! Please use the non-namespaced names above.
* also accepts the following numpy types
- `numpy.float64` and `np.float64` for double precision floats
- `numpy.float32`, `np.float32`, `numpy.single`, and `np.single` for single precision floats

Use of these namespaced types should be limited to variables that are implemented as numpy arrays in the model, as the data type is part of the of the array metadata. E.g.
```python
import numpy as np
variable = np.ndarray((1,1), dtype=np.single)
print(variable.dtype)
```
will produce `dtype('float32')`.

For variables where the dtype is not directly accessed from the numpy meta data, this usage is discouraged! Please use the non-namespaced names above.

# Input and output published variables

Expand Down
3 changes: 2 additions & 1 deletion include/bmi/Bmi_Py_Adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ namespace models {
return "long long";
} else if (py_type_name == "longlong" && item_size == sizeof(long long)) {
return "long long"; //numpy type
} else if (py_type_name == "float" && item_size == sizeof(float)) {
} else if ( (py_type_name == "float" || py_type_name == "float32" || py_type_name == "np.float32" ||
py_type_name == "numpy.float32" || py_type_name == "np.single" || py_type_name == "numpy.single") && item_size == sizeof(float)) {
return "float";
} else if ((py_type_name == "float" || py_type_name == "float64" || py_type_name == "np.float64" ||
py_type_name == "numpy.float64") && item_size == sizeof(double)) {
Expand Down
Loading