diff --git a/pyproject.toml b/pyproject.toml index 3c8646a..85bd93d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,9 +14,11 @@ dev = [ "pre-commit>=4.2.0", "ruff>=0.11.4", "pytest>=7.0", + "pytest-profiling", + "flameprof", ] [tool.pytest.ini_options] testpaths = ["tests"] python_files = "test_*.py" -addopts = "-v" +addopts = "-v -m 'not profiling'" diff --git a/src/dementpy.py b/src/dementpy.py index c04f8b6..daa0148 100644 --- a/src/dementpy.py +++ b/src/dementpy.py @@ -103,4 +103,5 @@ def main(): os.chdir('../'+output_folder) export(Output_init, site, outname) -main() \ No newline at end of file +if __name__ == '__main__': + main() diff --git a/src/grid.py b/src/grid.py index f86065a..ec1cb4c 100644 --- a/src/grid.py +++ b/src/grid.py @@ -420,8 +420,8 @@ def metabolism(self,day): # Update Substrates pools with dead enzymes DeadEnz_df = pd.concat( [Enzyme_Loss, - Enzyme_Loss.mul(self.Enz_Attrib['N_cost'].tolist()*self.gridsize,axis=0), - Enzyme_Loss.mul(self.Enz_Attrib['P_cost'].tolist()*self.gridsize,axis=0)], + Enzyme_Loss.mul(np.repeat(self.Enz_Attrib['N_cost'].values, self.gridsize), axis=0), + Enzyme_Loss.mul(np.repeat(self.Enz_Attrib['P_cost'].values, self.gridsize), axis=0)], axis=1 ) 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): # last: assign microbes to each grid box randomly based on prior densities choose_taxa = np.zeros((self.n_taxa,self.gridsize), dtype='int8') for i in range(self.n_taxa): - choose_taxa[i,:] = np.random.choice([1,0], self.gridsize, replace=True, p=[frequencies[i], 1-frequencies[i]]) + choose_taxa[i,:] = np.random.binomial(1, frequencies[i], self.gridsize) self.Microbes.loc[np.ravel(choose_taxa,order='F')==0] = np.float32(0) # NOTE order='F' \ No newline at end of file diff --git a/tests/profiling/test_profiling.py b/tests/profiling/test_profiling.py new file mode 100644 index 0000000..5a00973 --- /dev/null +++ b/tests/profiling/test_profiling.py @@ -0,0 +1,18 @@ +"""Test(s) to use with pytest-profiling to identify bottlenecks in the code.""" +import pytest + +@pytest.mark.profiling +def test_profiling(monkeypatch): + + # Define the command line arguments + import sys + argv = ['dementpy.py', 'grassland', 'output', '20250402', 'scrubland'] + monkeypatch.setattr(sys, 'argv', argv) + + # Move to subfolder so input and output folders will be correct + import os + os.chdir('src') + + # Run dementpy + import dementpy + dementpy.main() diff --git a/tests/test_grid.py b/tests/unit/test_grid.py similarity index 100% rename from tests/test_grid.py rename to tests/unit/test_grid.py diff --git a/tests/test_initialization.py b/tests/unit/test_initialization.py similarity index 100% rename from tests/test_initialization.py rename to tests/unit/test_initialization.py diff --git a/tests/test_placeholder.py b/tests/unit/test_placeholder.py similarity index 100% rename from tests/test_placeholder.py rename to tests/unit/test_placeholder.py