Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

import numpy as np 

import unittest 

import lsst.utils.tests 

 

from lsst.sims.catUtils.mixins import VariabilityAGN 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class AgnModelTestCase(unittest.TestCase): 

 

longMessage = True 

 

def test_agn_structure_function(self): 

""" 

Test that the structure_function of the delta_magnitudes 

resulting from our AGN variability model is consistent with 

equation 3 of MacLeod et al. 2010 (ApJ 721, 1014) 

""" 

 

agn_obj = VariabilityAGN() 

n_obj = 10 

d_mjd = 1.0 

rng = np.random.RandomState(88) 

mjd_grid = np.arange(61000.0, 237000.0, d_mjd) 

agn_params = {} 

agn_params['seed'] = rng.randint(10, high=1000, size=n_obj) 

agn_params['agn_tau'] = rng.random_sample(n_obj)*25.0+75.0 

for bp in ('u', 'g', 'r', 'i', 'z', 'y'): 

agn_params['agn_sf%s' % bp] = rng.random_sample(n_obj)*100.0+5.0 

 

redshift = np.zeros(n_obj, dtype=float) 

dmag_arr = agn_obj.applyAgn([range(n_obj)], agn_params, mjd_grid, 

redshift=redshift) 

 

self.assertEqual(dmag_arr.shape, (6, n_obj, len(mjd_grid))) 

 

max_dev = -1.0 

for i_obj in range(n_obj): 

tau = agn_params['agn_tau'][i_obj] 

seed = agn_params['seed'][i_obj] 

for i_bp, bp in enumerate(('u', 'g', 'r', 'i', 'z', 'y')): 

sf_inf = agn_params['agn_sf%s' % bp][i_obj] 

 

# loop over different time lags, calculating the structure 

# function of the light curves and comparing to the 

# expected value of the structure function 

for delta_i_t in range(5,len(mjd_grid)//2, len(mjd_grid)//20): 

 

delta_t = d_mjd*delta_i_t 

 

dmag_0 = dmag_arr[i_bp][i_obj][:-delta_i_t] 

dmag_1 = dmag_arr[i_bp][i_obj][delta_i_t:] 

self.assertEqual(len(dmag_0), len(dmag_1)) 

 

# expected structure funciton value taken from 

# equation 3 of MacLeod et al 

sf_th = sf_inf*np.sqrt(1.0-np.exp(-delta_t/tau)) 

 

# use definition of structure function from 

# section 2.1 of Hughes et al. 1992 

# (ApJ 396, 469) 

sf_test = np.sqrt(np.mean((dmag_1-dmag_0)**2)) 

 

# verify that the structure function is within 10% 

# of the expected value 

self.assertLess(np.abs(1.0-sf_test/sf_th), 0.1) 

 

def test_agn_mean(self): 

""" 

Test that the mean of time lagged AGN light curves approaches 

delta_magnitude = 0 as the time lag gets larger than 

the AGN variability time scale 

""" 

 

agn_obj = VariabilityAGN() 

n_obj = 10 

d_mjd = 1.0 

rng = np.random.RandomState(11273) 

mjd_grid = np.arange(61000.0, 237000.0, d_mjd) 

agn_params = {} 

agn_params['seed'] = rng.randint(10, high=1000, size=n_obj) 

agn_params['agn_tau'] = rng.random_sample(n_obj)*25.0+75.0 

for bp in ('u', 'g', 'r', 'i', 'z', 'y'): 

agn_params['agn_sf%s' % bp] = rng.random_sample(n_obj)*100.0+5.0 

 

redshift = np.zeros(n_obj, dtype=float) 

dmag_arr = agn_obj.applyAgn([range(n_obj)], agn_params, mjd_grid, 

redshift=redshift) 

 

self.assertEqual(dmag_arr.shape, (6, n_obj, len(mjd_grid))) 

 

max_dev = -1.0 

for i_obj in range(n_obj): 

tau = agn_params['agn_tau'][i_obj] 

seed = agn_params['seed'][i_obj] 

for i_bp, bp in enumerate(('u', 'g', 'r', 'i', 'z', 'y')): 

sf_inf = agn_params['agn_sf%s' % bp][i_obj] 

 

# loop over different time lags, calculating the mean 

# of the light curve taken at those time lags; make 

# sure delta_mag is within 1-sigma of zero 

# 

# only consider lags that are greater than 5*tau 

delta_i_t_min = int(np.round(tau/d_mjd)) 

self.assertLess(5*delta_i_t_min, len(mjd_grid)//20) 

 

ct_lags = 0 

for delta_i_t in range(5*delta_i_t_min, len(mjd_grid)//20, 100): 

ct_lags += 1 

t_dexes = range(delta_i_t+rng.randint(0,high=10), len(mjd_grid), delta_i_t) 

dmag_subset = dmag_arr[i_bp][i_obj][t_dexes] 

self.assertGreater(len(dmag_subset), 19) 

dmag_mean = np.mean(dmag_subset) 

dmag_stdev = np.std(dmag_subset) 

msg = 'failed with %d samples' % len(dmag_subset) 

self.assertLess(np.abs(dmag_mean)/dmag_stdev, 1.0, 

msg=msg) 

self.assertGreater(ct_lags, 10) 

 

def test_agn_structure_function_with_redshift(self): 

""" 

Test that the structure_function of the delta_magnitudes 

resulting from our AGN variability model is consistent with 

equation 3 of MacLeod et al. 2010 (ApJ 721, 1014) 

 

This test is done for the case of non-zero redshift 

""" 

 

agn_obj = VariabilityAGN() 

n_obj = 10 

d_mjd = 1.0 

rng = np.random.RandomState(443) 

mjd_grid = np.arange(61000.0, 237000.0, d_mjd) 

agn_params = {} 

agn_params['seed'] = rng.randint(10, high=1000, size=n_obj) 

agn_params['agn_tau'] = rng.random_sample(n_obj)*25.0+75.0 

for bp in ('u', 'g', 'r', 'i', 'z', 'y'): 

agn_params['agn_sf%s' % bp] = rng.random_sample(n_obj)*100.0+5.0 

 

redshift = rng.random_sample(n_obj)*2.0+0.1 

dmag_arr = agn_obj.applyAgn([range(n_obj)], agn_params, mjd_grid, 

redshift=redshift) 

 

self.assertEqual(dmag_arr.shape, (6, n_obj, len(mjd_grid))) 

 

max_dev = -1.0 

for i_obj in range(n_obj): 

tau = agn_params['agn_tau'][i_obj] 

seed = agn_params['seed'][i_obj] 

time_dilation = 1.0+redshift[i_obj] 

for i_bp, bp in enumerate(('u', 'g', 'r', 'i', 'z', 'y')): 

sf_inf = agn_params['agn_sf%s' % bp][i_obj] 

 

# loop over different time lags, calculating the structure 

# function of the light curves and comparing to the 

# expected value of the structure function 

for delta_i_t in range(5,len(mjd_grid)//2, len(mjd_grid)//20): 

 

delta_t = d_mjd*delta_i_t/time_dilation 

 

dmag_0 = dmag_arr[i_bp][i_obj][:-delta_i_t] 

dmag_1 = dmag_arr[i_bp][i_obj][delta_i_t:] 

self.assertEqual(len(dmag_0), len(dmag_1)) 

 

# expected structure funciton value taken from 

# equation 3 of MacLeod et al 

sf_th = sf_inf*np.sqrt(1.0-np.exp(-delta_t/tau)) 

 

# use definition of structure function from 

# section 2.1 of Hughes et al. 1992 

# (ApJ 396, 469) 

sf_test = np.sqrt(np.mean((dmag_1-dmag_0)**2)) 

 

# verify that the structure function is within 10% 

# of the expected value 

self.assertLess(np.abs(1.0-sf_test/sf_th), 0.1) 

 

def test_agn_mean_with_redshift(self): 

""" 

Test that the mean of time lagged AGN light curves approaches 

delta_magnitude = 0 as the time lag gets larger than 

the AGN variability time scale 

 

This test is done in the case of non-zero redshift 

""" 

 

agn_obj = VariabilityAGN() 

n_obj = 10 

d_mjd = 1.0 

rng = np.random.RandomState(2273) 

mjd_grid = np.arange(61000.0, 237000.0, d_mjd) 

agn_params = {} 

agn_params['seed'] = rng.randint(10, high=1000, size=n_obj) 

agn_params['agn_tau'] = rng.random_sample(n_obj)*25.0+75.0 

for bp in ('u', 'g', 'r', 'i', 'z', 'y'): 

agn_params['agn_sf%s' % bp] = rng.random_sample(n_obj)*100.0+5.0 

 

redshift = rng.random_sample(n_obj)*2.0+0.1 

dmag_arr = agn_obj.applyAgn([range(n_obj)], agn_params, mjd_grid, 

redshift=redshift) 

 

self.assertEqual(dmag_arr.shape, (6, n_obj, len(mjd_grid))) 

 

max_dev = -1.0 

for i_obj in range(n_obj): 

tau = agn_params['agn_tau'][i_obj] 

seed = agn_params['seed'][i_obj] 

time_dilation = 1.0+redshift[i_obj] 

for i_bp, bp in enumerate(('u', 'g', 'r', 'i', 'z', 'y')): 

sf_inf = agn_params['agn_sf%s' % bp][i_obj] 

 

# loop over different time lags, calculating the mean 

# of the light curve taken at those time lags; make 

# sure delta_mag is within 1-sigma of zero 

# 

# only consider lags that are greater than 5*tau 

delta_i_t_min = int(np.round(tau/(time_dilation*d_mjd))) 

self.assertLess(5*delta_i_t_min, len(mjd_grid)//20) 

 

ct_lags = 0 

for delta_i_t in range(5*delta_i_t_min, len(mjd_grid)//20, 100): 

ct_lags += 1 

t_dexes = range(delta_i_t+rng.randint(0,high=10), len(mjd_grid), delta_i_t) 

dmag_subset = dmag_arr[i_bp][i_obj][t_dexes] 

self.assertGreater(len(dmag_subset), 19) 

dmag_mean = np.mean(dmag_subset) 

dmag_stdev = np.std(dmag_subset) 

msg = 'failed with %d samples' % len(dmag_subset) 

self.assertLess(np.abs(dmag_mean)/dmag_stdev, 1.0, 

msg=msg) 

 

self.assertGreater(ct_lags, 10) 

 

def test_threading(self): 

""" 

Test that running applyAgn with multithreading does not change the answers 

""" 

agn_obj = VariabilityAGN() 

agn_obj_2 = VariabilityAGN() 

agn_obj_2._agn_threads = 4 

self.assertEqual(agn_obj._agn_threads, 1) 

self.assertNotEqual(agn_obj._agn_threads, agn_obj_2._agn_threads) 

 

rng = np.random.RandomState(61422) 

n_agn = 11 

redshift = rng.random_sample(n_agn)*2.0+1.1 

agn_params = {} 

agn_params['agn_tau'] = rng.random_sample(n_agn)*10.0+1.0 

for bp in 'ugrizy': 

agn_params['agn_sf%s' % bp] = rng.random_sample(n_agn)*2.0+0.1 

agn_params['seed'] = rng.randint(2, high=100, size=n_agn) 

mjd = 61923.5 

dmag_control = agn_obj.applyAgn([np.arange(n_agn, dtype=int)], 

agn_params, mjd, redshift=redshift) 

 

self.assertEqual(dmag_control.shape, (6,n_agn)) 

n_zero = np.where(np.abs(dmag_control.flatten())<1.0e-10) 

self.assertEqual(len(n_zero[0]), 0) 

 

dmag_threaded = agn_obj_2.applyAgn([np.arange(n_agn, dtype=int)], 

agn_params, mjd, redshift=redshift) 

 

np.testing.assert_array_equal(dmag_control, dmag_threaded) 

 

##### now test it on a numpy array of mjd 

 

mjd = 59580.0+rng.random_sample(13)*2000.0 

dmag_control = agn_obj.applyAgn([np.arange(n_agn, dtype=int)], 

agn_params, mjd, redshift=redshift) 

 

self.assertEqual(dmag_control.shape, (6,n_agn, len(mjd))) 

n_zero = np.where(np.abs(dmag_control.flatten())<1.0e-10) 

self.assertEqual(len(n_zero[0]), 0) 

 

dmag_threaded = agn_obj_2.applyAgn([np.arange(n_agn, dtype=int)], 

agn_params, mjd, redshift=redshift) 

 

np.testing.assert_array_equal(dmag_control, dmag_threaded) 

 

 

class MemoryTestClass(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

 

289 ↛ 290line 289 didn't jump to line 290, because the condition on line 289 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()