Skip to content

Commit 62fd4ad

Browse files
fix: implicit conversion to float64
There was an implicit conversion to float64 taking place due to a cast to the Python's list (and hence Python's float). This resultued in a serious (fator ~10) degradation in performance, which should now be fixed. The performance degradation was a result of temporary allocation performed by pandas when a dtype of a frame was implicitly changed in updates of the form e.g.: ```python import pandas as pd import numpy as np df = pd.DataFrame({"f32": np.ones(2, dtype="float32")}) df.iloc[1:2] = np.float64(1/3) ``` since pandas 2.1, such operations raise a FutureWarning. All occurences of that warning in DEMENTpy are resolved in this commit.
1 parent 10731c0 commit 62fd4ad

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

src/grid.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,13 @@ 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), # Implicit f64 conversion
424+
Enzyme_Loss.mul(np.repeat(self.Enz_Attrib['P_cost'].values, self.gridsize), axis=0)], # Implicit f64 conversion
425425
axis=1
426426
)
427427
DeadEnz_df.index = [np.arange(self.gridsize).repeat(self.n_enzymes), DeadEnz_df.index] # create a multi-index
428428
DeadEnz_gridcell = DeadEnz_df.groupby(level=0).sum() # total dead mass across taxa in each grid cell
429-
self.Substrates.loc[is_deadEnz] += DeadEnz_gridcell.values
429+
self.Substrates.loc[is_deadEnz] += DeadEnz_gridcell.values
430430

431431

432432
def mortality(self,day):
@@ -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'

0 commit comments

Comments
 (0)