Skip to content

Commit 1499480

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 resulted in a serious (factor ~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 1499480

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

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'

0 commit comments

Comments
 (0)