Skip to content

Releases: MAGALA-RICHARD/apsimNGpy

Automatic file deletion made easy

09 Nov 02:24

Choose a tag to compare

### apsimNGpy 0.39.11.20 – Release Notes

This release introduces a dedicated simulation file context manager that automatically removes transient .apsimx files and all associated output database files (.db, .db-wal, .db-sha, etc.) at the end of a with block.
This allows users to run temporary simulations with confidence that intermediate files will not accumulate on disk — especially important for large workflows, such as optimization loops, Monte Carlo runs, or multi-site batch simulations.

To achieve this, the context manager now coordinates more carefully with APSIM-NG’s I/O locking behavior. In particular, it triggers the underlying C# garbage collector (when necessary) to ensure that the write locks on the result database are fully released before deletion.
As a result, there is a small (but practically negligible) performance overhead; future versions may introduce a switch to disable this behavior for users who do not require automatic cleanup.

This release also introduces a second context manager for managing temporary APSIM bin paths.
It allows users to specify a local APSIM-NG installation path for a given script/module while preserving the global default in memory — enabling cleaner multi-version testing or workflow portability without rewriting environment variables.

### 1) Temporally simulations using a context manager

   from apsimNGpy.core.apsim import ApsimModel
    with Apsim_model("template.apsimx") as temp_model:
        temp_model.run()   # temporary .apsimx + .db files created
        df= temp_model.results
        print(df)

Immediately after exiting the with block, the temporary .apsimx file (and its associated .db files) are deleted,
since only clones of the original model file are used inside the context.

2) Temporary APSIM-bin path

    from apsimNGpy.core.config import apsim_bin_context
     with apsim_bin_context("C:/APSIM/2025.05.1234/bin"):
          from Models.Core import Simulations   # uses this bin path for loading
          from apsimNGpy.core.apsim import ApsimModel

Immediately after exiting the with block, the path is restored back to the global APSIM path — meaning that other projects and modules can continue to access their own settings normally. The importance of this pattern lies in its reproducibility, as it allows your project to be “locked” to a specific APSIM binary path.

APSIM versions change frequently, and a future run of your current project may fail or yield different results if a newer APSIM version is installed without your knowledge. By scoping a local APSIM bin path for this project, you ensure that reruns in the future use exactly the same APSIM version that generated the original results.
This makes the workflow both reproducible and stable.

The bin path can also be substituted with just the project .env path as follows

with apsim_bin_context( dotenv_path = './config/.env', bin_key ='APSIM_BIN_PATH'):
      from Models.Core import Simulations   # uses this bin path for loading
      from apsimNGpy.core.apsim import ApsimModel # uses this bin path for loading

Note
Since the model assemblies are already loaded into memory inside the apsim_bin_context, you do not need to remain inside the
with block to keep using them. Once loaded, those modules (and their namespaces) are global within the process, and they retain
their reference to the APSIM bin path that was supplied during loading.

v0.39.8.14

27 Aug 22:08

Choose a tag to compare

v0.39.8.14 – APSIM file structure patches

Fixes

  • Additional patches to handle recent APSIM NG file-structure changes.
  • Improved detection and fallback logic in readers/writers.

Compatibility

  • Works with recent APSIM NG builds that modified file layout.
  • If you hit issues, please open an issue with your APSIM build number and a minimal .apsimx.

Notes

  • No API changes; backward-compatible.

v0.35

02 May 20:19

Choose a tag to compare

v0.35 Pre-release
Pre-release

apsimNGpy version: v0.35 – April 2025

APSIMNGpy is a Python interface for APSIM Next Generation (.apsimx) models. It enables seamless model inspection, execution, batch simulation, optimization, and diagnostic reporting—directly from Python. Designed for reproducible research and integration with scientific workflows. Please take a look at the new improvements below.

✨ New Features

Model Introspection: inspect_model_inputs() enables inspection of internal parameters (Weather, Clock, Soil, Report, etc.).

Model Actions: Support for get, delete, and check operations on models within simulations via get_or_check_model().

Flexible Model Lookup: Added _eval_model() and find_model() utilities for robust model resolution by name or namespace.

Batch Simulation Support: Functions for handling multiple simulations and reports concurrently.

Model editing: Model editing has been streamlined by introducing a unified method, edit_model, which allows users to specify the model type, name, and target simulation in a single call.

Model inspection: Model inspection has been enhanced with the inspect_model_parameters method, enabling users to retrieve parameter values by specifying the model type, name, and simulation. Additionally, the inspect_model method can be used to explore available model names and their paths within simulations.

Cultivar Modification: Access and manipulation of PMF.Cultivar parameters, including dynamic cultivar switching.

More CLI model edits and interactions

🛠 Improvements

Refactored internal utilities for better maintainability and reuse.

Improved error handling and descriptive exceptions.

Memory-safe operations with explicit use of garbage collection to clear memory after expensive recursions.

Logging minimization inside event loops for improved performance.

🐛 Bug Fixes

Fixed incorrect type handling in soil model inspection.

Resolved missing models from deeply nested namespaces like Models.Factorial.

📚 Documentation

Enhanced docstrings and type hints across all public APIs.

Added inline usage instructions for key functions.