Skip to content

Commit

Permalink
Merge pull request #149 from ImperialCollegeLondon/tcai-profiling
Browse files Browse the repository at this point in the history
Add small dataset for profiling
  • Loading branch information
tztsai authored Jan 12, 2024
2 parents c2109fc + 63ce5cf commit d562fba
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/pyrealm_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ jobs:
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: Setup Git LFS
id: lfs
if: ${{ steps.codecov.outcome == 'success' && github.event_name != 'push' }}
run: |
git lfs pull
git lfs ls-files -n | xargs git lfs track # track all LFS files
- name: Run profiling
id: profiling
if: steps.lfs.outcome == 'success'
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ pyrealm.egg-info
# Coverage
.coverage

# mypy
.mypy_cache/

# Data
pyrealm_build_data/*.nc
pyrealm_build_data/inputs_data_24.25.nc
pyrealm_build_data/eda.py

# Profiling
prof/
8 changes: 5 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,18 @@ depending on the os. Dedicated profiling tests have been created for PyRealm.
Please see the relevant testing directory.

```bash
poetry run pytest <test_path> --profile
poetry run pytest --profile
```

to generate a report. You can run

```bash
poetry run pytest <test_path> --profile-svg
poetry run pytest --profile-svg
```

to generate a call graph. The graph is stored with the name `combined.svg`.
to generate a call graph. The graph is saved at `prof/combined.svg`.

To enable profiling of a test function or class, decorate it with `@pytest.mark.profiling`.

#### Using 'pytest-coverage'

Expand Down
1 change: 1 addition & 0 deletions profiling/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
).str.extract(r"(.*?):(.*?)\((.*)\)", expand=True)
dt = datetime.datetime.fromtimestamp(prof_path.stat().st_mtime)
df.index = pd.Series([dt.strftime("%Y-%m-%d %H:%M:%S")] * len(df), name="timestamp")
print(df)
df["label"] = df["filename"].str.extract(r"(\w+).py").squeeze() + "." + df["function"]
df["event"] = [args.event] * len(df)
report_path = root / "profiling/prof-report.csv"
Expand Down
Binary file added pyrealm_build_data/inputs_data_reduced_1y.nc
Binary file not shown.
14 changes: 9 additions & 5 deletions tests/test_profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TestClass:
def setup(self):
"""Setting up the pmodel and loading the test data set."""
# Loading the dataset:
dpath = resources.files("pyrealm_build_data") / "inputs_data_24.25.nc"
dpath = resources.files("pyrealm_build_data") / "inputs_data_reduced_1y.nc"

ds = xarray.load_dataset(dpath)

Expand All @@ -32,9 +32,10 @@ def setup(self):
# offset from the UTC times in the data. This step reduces the input data
# to complete daily blocks of data using local time

ds = ds.sel(time=slice("2000-01-01T01:59", "2001-12-31T01:59"))
self.local_offset = np.timedelta64(int((24.25 * (24 / 360)) * 60 * 60), "s")
self.local_time = ds["time"].to_numpy() - self.local_offset
ds = ds.sel(time=slice("2000-01-01T01:59", "2000-12-31T01:59"))
ds["local_time_offset"] = (ds["lon"] / 15 * 3600).astype("timedelta64[s]")
# 15° per hour, 3600 seconds per hour
ds["local_time"] = ds["time"] - ds["local_time_offset"]

# Variable set up
# Air temperature in Kelvin
Expand All @@ -49,7 +50,10 @@ def setup(self):
# Gather PPFD µmole/m2/s1
self.ppfd = ds["ppfd"].to_numpy()
# Define atmospheric CO2 concentration (ppm)
self.co2 = np.ones_like(self.tc) * 400
self.co2 = ds["co2"].to_numpy()
# Define the local time at different longitudes
self.local_time = ds["local_time"].to_numpy().squeeze()
# TODO: make the code applicable to a dataset with multiple longitudes

# Generate and check the PModelEnvironment
self.pm_env = PModelEnvironment(
Expand Down

0 comments on commit d562fba

Please sign in to comment.