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

Development #17

Merged
merged 3 commits into from
Jun 17, 2024
Merged

Development #17

merged 3 commits into from
Jun 17, 2024

Conversation

leaver2000
Copy link
Owner

@leaver2000 leaver2000 commented Jun 17, 2024

Summary by Sourcery

This pull request includes significant refactoring and enhancements to the core functions and tests in the project. Key changes include improved handling of NaN values, conditional checks, and type safety. New test cases have been added to ensure robustness, and obsolete files have been removed.

  • Enhancements:
    • Refactored assert_nan function to handle both single and multiple NaN checks.
    • Introduced pressure_levels function to generate pressure levels array.
    • Enhanced moist_lapse tests with new test cases for element-wise, broadcasting, and matrix operations.
    • Improved downdraft_cape function to handle conditional parcel calculations and NaN values.
    • Refactored el and lfc functions to improve handling of NaN values and conditional checks.
    • Updated type annotations and type handling across multiple functions for better type safety and clarity.
    • Refactored PVectorNd class to include new methods for conditional checks and handling NaN values.
  • Tests:
    • Added new test cases for moist_lapse, downdraft_cape, el, and lfc functions.
    • Refactored existing tests to align with updated function implementations and type annotations.
    • Removed outdated test files and data files.
  • Chores:
    • Deleted obsolete files including data.json, notebook.ipynb, nb.ipynb, moist_lapse_test.py, libcore_test.py, and dcape_test.py.

Copy link

sourcery-ai bot commented Jun 17, 2024

Reviewer's Guide by Sourcery

This pull request refactors and adds new test functions, updates type annotations, and optimizes existing functions for better performance and readability. It also deletes unused and redundant files to clean up the codebase.

File-Level Changes

Files Changes
tests/core_test.py
tests/functional_test.py
Refactored and added new test functions to improve test coverage and handle new data structures.
src/nzthermo/_typing.pyi
src/nzthermo/utils.py
src/nzthermo/core.py
src/nzthermo/functional.py
src/nzthermo/_core.pyi
Updated type annotations and refactored functions to use new data structures and improve performance.
src/include/libthermo.hpp
src/nzthermo/_core.pyx
src/nzthermo/_C.pxd
Reformatted and optimized Cython and C++ code for better readability and performance.
tests/data.json
notebook.ipynb
src/nzthermo/_types.pyi
nb.ipynb
tests/moist_lapse_test.py
tests/libcore_test.py
tests/dcape_test.py
Deleted unused and redundant files to clean up the codebase.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

@leaver2000 leaver2000 merged commit e200d8d into master Jun 17, 2024
1 check failed
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @leaver2000 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 6 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 5 issues found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

Comment on lines +24 to +25
_DualArrayLike = (
SupportsArray[_Dtype_t] | NestedSequence[SupportsArray[_Dtype_t]] | _T | NestedSequence[_T]
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider using a type alias for readability.

The type definition for _DualArrayLike is quite complex. Consider using a type alias to improve readability and maintainability.

Suggested change
_DualArrayLike = (
SupportsArray[_Dtype_t] | NestedSequence[SupportsArray[_Dtype_t]] | _T | NestedSequence[_T]
_SupportsArrayOrNested = SupportsArray[_Dtype_t] | NestedSequence[SupportsArray[_Dtype_t]]
_TOrNested = _T | NestedSequence[_T]
_DualArrayLike = _SupportsArrayOrNested | _TOrNested

Comment on lines +79 to +85
if broadcasted := pressure.shape == temperature.shape:
p_layer, t_layer, td_layer = np.where(
mask[newaxis, :, :], [pressure, temperature, dewpoint], NaN
)
else:
p_layer = np.where(mask, pressure, NaN)
t_layer, td_layer = np.where(mask[newaxis, :, :], [temperature, dewpoint], NaN)
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider simplifying the conditional logic.

The conditional logic for broadcasted is a bit complex. Consider simplifying or breaking it down for better readability.

Suggested change
if broadcasted := pressure.shape == temperature.shape:
p_layer, t_layer, td_layer = np.where(
mask[newaxis, :, :], [pressure, temperature, dewpoint], NaN
)
else:
p_layer = np.where(mask, pressure, NaN)
t_layer, td_layer = np.where(mask[newaxis, :, :], [temperature, dewpoint], NaN)
broadcasted = pressure.shape == temperature.shape
if broadcasted:
p_layer, t_layer, td_layer = np.where(
mask[newaxis, :, :], [pressure, temperature, dewpoint], NaN
)
else:
p_layer = np.where(mask, pressure, NaN)
t_layer, td_layer = np.where(mask[newaxis, :, :], [temperature, dewpoint], NaN)

@@ -170,7 +164,7 @@
pressure, temperature, dewpoint
)

N, Z = temperature.shape
N = temperature.shape[0]
Copy link

Choose a reason for hiding this comment

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

nitpick: Remove unused variable assignment.

The variable N is assigned but not used in the subsequent code. Consider removing it to avoid confusion.

Comment on lines +165 to +166
x_full[nx, z0] = x
y_full[nx, z0] = y
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Check for potential index out-of-bounds.

Ensure that nx and z0 indices are within the bounds of x_full and y_full arrays to avoid potential index out-of-bounds errors.

@@ -18,43 +18,43 @@ _float = TypeVar("_float", np.float32, np.float64)
OPENMP_ENABLED: bool

@overload
def moist_lapse[T: np.float_](
def moist_lapse[T: np.floating[Any]](
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider adding type constraints for dtype.

The dtype parameter accepts type[T | float] | L["float32", "float64"] | None. Consider adding type constraints to ensure it aligns with the expected floating-point types.

Suggested change
def moist_lapse[T: np.floating[Any]](
def moist_lapse[T: np.floating[Any]](
pressure: Pascal[np.ndarray[shape[N], np.dtype[T]]],
temperature: Kelvin[np.ndarray[shape[N], np.dtype[np.floating[Any]]]],
reference_pressure: Pascal[np.ndarray[shape[N], np.dtype[np.floating[Any]]]],
*,
dtype: type[T] | type[float] | Literal["float32", "float64"] | None = ...,
) -> Kelvin[np.ndarray[shape[N], np.dtype[T]]]: ...

Comment on lines +102 to +104
DCAPE = Rd * F.nantrapz(p_vt - e_vt, np.log(pressure), axis=1)

dcape = -(Rd * F.nantrapz(delta, np.log(pressure), axis=1))

return dcape
return DCAPE
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Inline variable that is immediately returned (inline-immediately-returned-variable)

Suggested change
DCAPE = Rd * F.nantrapz(p_vt - e_vt, np.log(pressure), axis=1)
dcape = -(Rd * F.nantrapz(delta, np.log(pressure), axis=1))
return dcape
return DCAPE
return Rd * F.nantrapz(p_vt - e_vt, np.log(pressure), axis=1)

Comment on lines +30 to +33
if len(args) == 1:
return args[0]

return tuple(values)
return args
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): We've found these issues:

Suggested change
if len(args) == 1:
return args[0]
return tuple(values)
return args
return args[0] if len(args) == 1 else args

Comment on lines +38 to +46
try:
import pint
except ImportError:
pint = None
except AttributeError:
raise ImportError(
"The environment has a version mismatch with pint and numpy. "
"Upgrade pint to the latest version."
)
Copy link

Choose a reason for hiding this comment

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

issue (code-quality): Explicitly raise from a previous error (raise-from-previous-error)

Comment on lines +63 to +65
if isinstance(x, pint.Quantity):
return x.to(unit).magnitude
return np.asarray(x)
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): We've found these issues:

Suggested change
if isinstance(x, pint.Quantity):
return x.to(unit).magnitude
return np.asarray(x)
return x.to(unit).magnitude if isinstance(x, pint.Quantity) else np.asarray(x)

Comment on lines +88 to +91
if method == "__call__":
return ufunc(self.pressure)

return NotImplemented
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): We've found these issues:

Suggested change
if method == "__call__":
return ufunc(self.pressure)
return NotImplemented
return ufunc(self.pressure) if method == "__call__" else NotImplemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant