Skip to content

Commit a5f8711

Browse files
authored
Merge pull request #39 from DEMENT-Model/iss22-remove-f64
Remove temporary allocations
2 parents 5975059 + 1499480 commit a5f8711

7 files changed

Lines changed: 26 additions & 5 deletions

File tree

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ dev = [
1414
"pre-commit>=4.2.0",
1515
"ruff>=0.11.4",
1616
"pytest>=7.0",
17+
"pytest-profiling",
18+
"flameprof",
1719
]
1820

1921
[tool.pytest.ini_options]
2022
testpaths = ["tests"]
2123
python_files = "test_*.py"
22-
addopts = "-v"
24+
addopts = "-v -m 'not profiling'"

src/dementpy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,5 @@ def main():
103103
os.chdir('../'+output_folder)
104104
export(Output_init, site, outname)
105105

106-
main()
106+
if __name__ == '__main__':
107+
main()

src/grid.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ def metabolism(self,day):
420420
# Update Substrates pools with dead enzymes
421421
DeadEnz_df = pd.concat(
422422
[Enzyme_Loss,
423-
Enzyme_Loss.mul(self.Enz_Attrib['N_cost'].tolist()*self.gridsize,axis=0),
424-
Enzyme_Loss.mul(self.Enz_Attrib['P_cost'].tolist()*self.gridsize,axis=0)],
423+
Enzyme_Loss.mul(np.repeat(self.Enz_Attrib['N_cost'].values, self.gridsize), axis=0),
424+
Enzyme_Loss.mul(np.repeat(self.Enz_Attrib['P_cost'].values, self.gridsize), axis=0)],
425425
axis=1
426426
)
427427
DeadEnz_df.index = [np.arange(self.gridsize).repeat(self.n_enzymes), DeadEnz_df.index] # create a multi-index
@@ -713,5 +713,5 @@ def reinitialization(self,initialization,microbes_pp,output,mode,pulse,*args):
713713
# last: assign microbes to each grid box randomly based on prior densities
714714
choose_taxa = np.zeros((self.n_taxa,self.gridsize), dtype='int8')
715715
for i in range(self.n_taxa):
716-
choose_taxa[i,:] = np.random.choice([1,0], self.gridsize, replace=True, p=[frequencies[i], 1-frequencies[i]])
716+
choose_taxa[i,:] = np.random.binomial(1, frequencies[i], self.gridsize)
717717
self.Microbes.loc[np.ravel(choose_taxa,order='F')==0] = np.float32(0) # NOTE order='F'

tests/profiling/test_profiling.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Test(s) to use with pytest-profiling to identify bottlenecks in the code."""
2+
import pytest
3+
4+
@pytest.mark.profiling
5+
def test_profiling(monkeypatch):
6+
7+
# Define the command line arguments
8+
import sys
9+
argv = ['dementpy.py', 'grassland', 'output', '20250402', 'scrubland']
10+
monkeypatch.setattr(sys, 'argv', argv)
11+
12+
# Move to subfolder so input and output folders will be correct
13+
import os
14+
os.chdir('src')
15+
16+
# Run dementpy
17+
import dementpy
18+
dementpy.main()

0 commit comments

Comments
 (0)