Skip to content

Commit

Permalink
validate reducing the dimensions age and space to length one
Browse files Browse the repository at this point in the history
  • Loading branch information
twallema committed Jun 14, 2024
1 parent 6cf7eaf commit d18a9cf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
13 changes: 5 additions & 8 deletions src/pySODM/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ def int_to_date(actual_start_date, t):
date = actual_start_date + timedelta(days=t)
return date

def list_to_dict(y, shape_dictionary, retain_floats=True):
def list_to_dict(y, shape_dictionary, retain_floats=False):
"""
A function to reconstruct model states, given a flat array of values and a dictionary with the variables name and desired shapes
Retains floats.
Parameters
----------
Expand All @@ -19,9 +18,6 @@ def list_to_dict(y, shape_dictionary, retain_floats=True):
shape_dictionary: dict
A dictionary containing the desired names and shapes of the output dictionary
retain_floats: bool
If False, float/np.float are converted in a 0D np.ndarray. Default: True.
Returns
-------
Expand All @@ -33,10 +29,11 @@ def list_to_dict(y, shape_dictionary, retain_floats=True):
offset=0
for s in shape_dictionary.values():
n = np.prod(s)
# Reshape changes type of floats to np.ndarray which is not desirable
if ((n == 1) & (retain_floats==True)):
if ((n == 1) & (retain_floats == True)):
restoredArray.append(y[offset])
elif ((n == 1) & (retain_floats == False)):
restoredArray.append(y[offset].reshape(s))
else:
restoredArray.append(y[offset:(offset+n)].reshape(s))
offset+=n
return dict(zip(shape_dictionary.keys(), restoredArray))
return dict(zip(shape_dictionary.keys(), restoredArray))
2 changes: 1 addition & 1 deletion tutorials/IDD/spatial_SIR/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def integrate(t, S, I, R, beta, gamma, f_v, N, M):
dI_v = beta * S_v * np.transpose(matmul_2D_3D_matrix(np.transpose(I_v/T_v), f_v*N))

# distribute the number of new infections on visited patch to the home patch
dI_v = S * np.transpose(M @ np.transpose(dI_v/S_v))
dI_v = S * np.transpose(np.atleast_2d(M) @ np.transpose(dI_v/S_v))

# Calculate differentials
dS = - (dI_h + dI_v)
Expand Down
9 changes: 5 additions & 4 deletions tutorials/IDD/spatial_SIR/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@

# OR
# Exactly the same model without age groups
# Validated the reduction to one spatial unit as well (becomes a plain SIR)

coordinates = {'age': ['0+'],
'location': ['A', 'B']
}
init_states = {'S': np.array([[500, 5000]]),
'I': np.array([[1, 0]])
init_states = {'S': np.atleast_2d([500, 5000]),
'I': np.atleast_2d([1, 0])
}
params = {'beta': 0.03, # infectivity (-)
params = {'beta': 0.03, # infectivity (-)
'gamma': 5, # duration of infection (d)
'f_v': 0.1, # fraction of total contacts on visited patch
'N': np.mean(np.sum(params['N'], axis=1)), # contact matrix (BE, Van Hoang, 2020)
'N': np.mean(np.sum(params['N'], axis=1)), # contact matrix
'M': np.array([[0.8, 0.2], [0, 1]]) # origin-destination mobility matrix
}

Expand Down

0 comments on commit d18a9cf

Please sign in to comment.