22import numpy as np
33from unittest .mock import patch , MagicMock
44from mimic .model_infer .infer_CRM_bayes import inferCRMbayes
5+ from mimic .model_simulate .sim_CRM import sim_CRM
56import xarray as xr
67import arviz as az
78import matplotlib .pyplot as plt
@@ -16,47 +17,81 @@ def setup_data(request):
1617 Fixture to provide mock data for testing.
1718 """
1819 # Generate mock data for testing purposes with 2 species
20+
1921 num_species = 2
2022 num_resources = 2
21- X = np .random .randn (100 , num_species + 1 ) # Random design matrix for 100 samples and 2 species
22- F = np .random .randn (100 , num_species ) # Random data matrix for 100 samples and 2 species
23+ times = np .arange (0 , 10 , 0.1 )
2324
24-
25- prior_tau_mean = 0
26- prior_tau_sigma = 1
27- prior_m_mean = - 0.1
28- prior_m_sigma = 0.1
29- prior_r_mean = 0
30- prior_r_sigma = 1
31- prior_w_mean = - 0.1
32- prior_w_sigma = 0.1
33- prior_K_mean = 0
34- prior_K_sigma = 1
35- prior_c_mean = - 0.1
36- prior_c_sigma = 0.1
37-
38- noise_stddev = 0.1
39- draws = 500
40- tune = 500
25+
26+ tau = np .random .uniform (0.1 , 0.9 , num_species ) # species timescales
27+ w = np .random .uniform (0.1 , 0.9 , num_resources ) # resource quality
28+ c = np .random .uniform (0.1 , 0.9 , (num_species , num_resources )) # relative resource preferences
29+ m = np .random .uniform (0.3 , 0.7 , num_species ) # mortality rates
30+ r = np .random .uniform (0.1 , 0.9 , num_resources ) # resource timescales
31+ K = np .random .uniform (1.0 , 10.0 , num_resources ) # resource carrying capacities
32+
33+ # initial conditions
34+ init_species = 10 * np .ones (num_species + num_resources )
35+
36+ # instantiate simulator
37+
38+ simulator = sim_CRM ()
39+
40+ simulator .set_parameters (num_species = num_species ,
41+ num_resources = num_resources ,
42+ tau = tau ,
43+ w = w ,
44+ c = c ,
45+ m = m ,
46+ r = r ,
47+ K = K )
48+
49+
50+ observed_species , observed_resources = simulator .simulate (times , init_species )
51+
52+ # add Guassian noise to the data
53+ observed_species = observed_species + np .random .normal (loc = 0 , scale = 0.1 , size = observed_species .shape )
54+ observed_resources = observed_resources + np .random .normal (loc = 0 , scale = 0.1 , size = observed_resources .shape )
55+
56+ # Replace negative values with 0
57+ observed_species = np .maximum (observed_species , 0.0001 )
58+ observed_resources = np .maximum (observed_resources , 0.0001 )
59+
60+ yobs = np .hstack ((observed_species , observed_resources ))
61+
62+
63+ # num_species = 2
64+ # num_resources = 2
65+ # times = np.random.randn(100, num_species+1) #
66+ # yobs = np.random.randn(100, num_species) #
67+
68+ # tau = np.array([0.7, 0.3])
69+ # m = np.array([0.5, 0.4])
70+ # r = np.array([0.4, 0.7])
71+ # w = np.array([0.3, 0.5])
72+ # K = np.array([5.0, 1.5])
73+
74+
75+ prior_c_mean = 0.6
76+ prior_c_sigma = 0.2
77+ num_species = 2
78+ num_resources = 2
79+
80+ draws = 20
81+ tune = 20
4182 chains = 4
4283 cores = 4
4384
4485 return {
45- "X" : X ,
46- "F" : F ,
47- "prior_tau_mean" : prior_tau_mean ,
48- "prior_tau_sigma" : prior_tau_sigma ,
49- "prior_m_mean" : prior_m_mean ,
50- "prior_m_sigma" : prior_m_sigma ,
51- "prior_r_mean" : prior_r_mean ,
52- "prior_r_sigma" : prior_r_sigma ,
53- "prior_w_mean" : prior_w_mean ,
54- "prior_w_sigma" : prior_w_sigma ,
55- "prior_K_mean" : prior_K_mean ,
56- "prior_K_sigma" : prior_K_sigma ,
86+ "times" : times ,
87+ "yobs" : yobs ,
88+ "tau" : tau ,
89+ "m" : m ,
90+ "r" : r ,
91+ "w" : w ,
92+ "K" : K ,
5793 "prior_c_mean" : prior_c_mean ,
5894 "prior_c_sigma" : prior_c_sigma ,
59- "noise_stddev" : noise_stddev ,
6095 "draws" : draws ,
6196 "tune" : tune ,
6297 "chains" : chains ,
@@ -75,21 +110,17 @@ def bayes_CRM_instance(setup_data):
75110
76111 # Set the parameters using the set_parameters method
77112 bayes_CRM .set_parameters (
78- X = data ["X" ],
79- F = data ["F" ],
80- prior_tau_mean = data ["prior_tau_mean" ],
81- prior_tau_sigma = data ["prior_tau_sigma" ],
82- prior_m_mean = data ["prior_m_mean" ],
83- prior_m_sigma = data ["prior_m_sigma" ],
84- prior_r_mean = data ["prior_r_mean" ],
85- prior_r_sigma = data ["prior_r_sigma" ],
86- prior_w_mean = data ["prior_w_mean" ],
87- prior_w_sigma = data ["prior_w_sigma" ],
88- prior_K_mean = data ["prior_K_mean" ],
89- prior_K_sigma = data ["prior_K_sigma" ],
113+ times = data ["times" ],
114+ yobs = data ["yobs" ],
115+ num_species = 2 ,
116+ num_resources = 2 ,
117+ tau = data ["tau" ],
118+ m = data ["m" ],
119+ r = data ["r" ],
120+ w = data ["w" ],
121+ K = data ["K" ],
90122 prior_c_mean = data ["prior_c_mean" ],
91123 prior_c_sigma = data ["prior_c_sigma" ],
92- noise_stddev = data ["noise_stddev" ],
93124 draws = data ["draws" ],
94125 tune = data ["tune" ],
95126 chains = data ["chains" ],
@@ -103,28 +134,28 @@ def test_run_inference(bayes_CRM_instance):
103134 """
104135 Test the `_bayes_run_inference` function to check if it returns the correct output without shrinkage or perturbation.
105136 """
106-
137+
107138 # Call the method to test without shrinkage or perturbation
108139 idata = bayes_CRM_instance .run_inference ()
109140
110141 # Check that the output is an ArviZ InferenceData object
111142 assert isinstance (idata , az .InferenceData ), "The output is not an InferenceData object."
112143
113144 # Check that the expected variables are in the posterior
114- assert "tau_hat" in idata .posterior , "'tau_hat' is not in the posterior."
115- assert "m_hat" in idata .posterior , "'m_hat' is not in the posterior."
116- assert "r_hat" in idata .posterior , "'r_hat' is not in the posterior."
117- assert "w_hat" in idata .posterior , "'w_hat' is not in the posterior."
118- assert "K_hat" in idata .posterior , "'K_hat' is not in the posterior."
145+ # assert "tau_hat" in idata.posterior, "'tau_hat' is not in the posterior."
146+ # assert "m_hat" in idata.posterior, "'m_hat' is not in the posterior."
147+ # assert "r_hat" in idata.posterior, "'r_hat' is not in the posterior."
148+ # assert "w_hat" in idata.posterior, "'w_hat' is not in the posterior."
149+ # assert "K_hat" in idata.posterior, "'K_hat' is not in the posterior."
119150 assert "c_hat" in idata .posterior , "'c_hat' is not in the posterior."
120151
121152
122153 # Additional assertions to test the integrity of the returned posterior
123- assert len (idata .posterior ["tau_hat" ]) > 0 , "'tau_hat' has no samples."
124- assert len (idata .posterior ["m_hat" ]) > 0 , "'m_hat' has no samples."
125- assert len (idata .posterior ["r_hat" ]) > 0 , "'r_hat' has no samples."
126- assert len (idata .posterior ["w_hat" ]) > 0 , "'w_hat' has no samples."
127- assert len (idata .posterior ["K_hat" ]) > 0 , "'K_hat' has no samples."
154+ # assert len(idata.posterior["tau_hat"]) > 0, "'tau_hat' has no samples."
155+ # assert len(idata.posterior["m_hat"]) > 0, "'m_hat' has no samples."
156+ # assert len(idata.posterior["r_hat"]) > 0, "'r_hat' has no samples."
157+ # assert len(idata.posterior["w_hat"]) > 0, "'w_hat' has no samples."
158+ # assert len(idata.posterior["K_hat"]) > 0, "'K_hat' has no samples."
128159 assert len (idata .posterior ["c_hat" ]) > 0 , "'c_hat' has no samples."
129160
130161
@@ -139,4 +170,4 @@ def test_plot_posterior(bayes_CRM_instance):
139170 try :
140171 bayes_CRM_instance .plot_posterior (idata )
141172 except Exception as e :
142- pytest .fail (f"plot_posterior raised an exception: { e } " )
173+ pytest .fail (f"plot_posterior raised an exception: { e } " )
0 commit comments