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

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

from __future__ import print_function 

from builtins import str 

from builtins import zip 

import numpy as np 

import matplotlib 

import warnings 

import unittest 

import lsst.utils.tests 

import lsst.sims.maf.stackers as stackers 

from lsst.sims.utils import _galacticFromEquatorial, calcLmstLast, Site, _altAzPaFromRaDec, \ 

ObservationMetaData 

from lsst.sims.survey.fields import FieldsDatabase 

 

matplotlib.use("Agg") 

 

 

class TestStackerClasses(unittest.TestCase): 

 

def testAddCols(self): 

"""Test that we can add columns as expected. 

""" 

data = np.zeros(90, dtype=list(zip(['alt'], [float]))) 

data['alt'] = np.arange(0, 90) 

stacker = stackers.ZenithDistStacker(altCol='alt', degrees=True) 

newcol = stacker.colsAdded[0] 

# First - are the columns added if they are not there. 

data, cols_present = stacker._addStackerCols(data) 

self.assertEqual(cols_present, False) 

self.assertIn(newcol, data.dtype.names) 

# Next - if they are present, is that information passed back? 

with warnings.catch_warnings(): 

warnings.simplefilter('ignore') 

data, cols_present = stacker._addStackerCols(data) 

self.assertEqual(cols_present, True) 

 

def testEQ(self): 

""" 

Test that stackers can be compared 

""" 

s1 = stackers.ParallaxFactorStacker() 

s2 = stackers.ParallaxFactorStacker() 

assert(s1 == s2) 

 

s1 = stackers.RandomDitherFieldPerVisitStacker() 

s2 = stackers.RandomDitherFieldPerVisitStacker() 

assert(s1 == s2) 

 

# Test if they have numpy array atributes 

s1.ack = np.arange(10) 

s2.ack = np.arange(10) 

assert(s1 == s2) 

 

# Change the array and test 

s1.ack += 1 

assert(s1 != s2) 

 

s2 = stackers.RandomDitherFieldPerVisitStacker(decCol='blah') 

assert(s1 != s2) 

 

def testNormAirmass(self): 

""" 

Test the normalized airmass stacker. 

""" 

rng = np.random.RandomState(232) 

data = np.zeros(600, dtype=list(zip( 

['airmass', 'fieldDec'], [float, float]))) 

data['airmass'] = rng.random_sample(600) 

data['fieldDec'] = rng.random_sample(600) * np.pi - np.pi / 2. 

data['fieldDec'] = np.degrees(data['fieldDec']) 

stacker = stackers.NormAirmassStacker(degrees=True) 

data = stacker.run(data) 

for i in np.arange(data.size): 

self.assertLessEqual(data['normairmass'][i], data['airmass'][i]) 

self.assertLess(np.min(data['normairmass'] - data['airmass']), 0) 

 

def testParallaxFactor(self): 

""" 

Test the parallax factor. 

""" 

 

data = np.zeros(600, dtype=list(zip(['fieldRA', 'fieldDec', 'observationStartMJD'], 

[float, float, float]))) 

data['fieldRA'] = data['fieldRA'] + .1 

data['fieldDec'] = data['fieldDec'] - .1 

data['observationStartMJD'] = np.arange(data.size) + 49000. 

stacker = stackers.ParallaxFactorStacker(degrees=True) 

data = stacker.run(data) 

self.assertLess(max(np.abs(data['ra_pi_amp'])), 1.1) 

self.assertLess(max(np.abs(data['dec_pi_amp'])), 1.1) 

self.assertLess( 

np.max(data['ra_pi_amp']**2 + data['dec_pi_amp']**2), 1.1) 

self.assertGreater(min(np.abs(data['ra_pi_amp'])), 0.) 

self.assertGreater(min(np.abs(data['dec_pi_amp'])), 0.) 

 

def _tDitherRange(self, diffsra, diffsdec, ra, dec, maxDither): 

self.assertLessEqual(np.abs(diffsra).max(), maxDither) 

self.assertLessEqual(np.abs(diffsdec).max(), maxDither) 

offsets = np.sqrt(diffsra**2 + diffsdec**2) 

self.assertLessEqual(offsets.max(), maxDither) 

self.assertGreater(diffsra.max(), 0) 

self.assertGreater(diffsdec.max(), 0) 

self.assertLess(diffsra.min(), 0) 

self.assertLess(diffsdec.min(), 0) 

 

def _tDitherPerNight(self, diffsra, diffsdec, ra, dec, nights): 

n = np.unique(nights) 

for ni in n: 

match = np.where(nights == ni)[0] 

dra_on_night = np.abs(np.diff(diffsra[match])) 

ddec_on_night = np.abs(np.diff(diffsdec[match])) 

111 ↛ 112line 111 didn't jump to line 112, because the condition on line 111 was never true if dra_on_night.max() > 0.0005: 

print(ni) 

m = np.where(dra_on_night > 0.0005)[0] 

print(diffsra[match][m]) 

print(ra[match][m]) 

print(dec[match][m]) 

print(dra_on_night[m]) 

self.assertAlmostEqual(dra_on_night.max(), 0) 

self.assertAlmostEqual(ddec_on_night.max(), 0) 

 

def testSetupDitherStackers(self): 

# Test that we get no stacker when using default columns. 

raCol = 'fieldRA' 

decCol = 'fieldDec' 

degrees = True 

stackerlist = stackers.setupDitherStackers(raCol, decCol, degrees) 

self.assertEqual(len(stackerlist), 0) 

# Test that we get one (and the right one) when using particular columns. 

raCol = 'hexDitherFieldPerNightRa' 

decCol = 'hexDitherFieldPerNightDec' 

stackerlist = stackers.setupDitherStackers(raCol, decCol, degrees) 

self.assertEqual(len(stackerlist), 1) 

self.assertEqual(stackerlist[0], stackers.HexDitherFieldPerNightStacker()) 

# Test that kwargs are passed along. 

stackerlist = stackers.setupDitherStackers(raCol, decCol, degrees, maxDither=0.5) 

self.assertEqual(stackerlist[0].maxDither, np.radians(0.5)) 

 

def testBaseDitherStacker(self): 

# Test that the base dither stacker matches the type of a stacker. 

s = stackers.HexDitherFieldPerNightStacker() 

self.assertTrue(isinstance(s, stackers.BaseDitherStacker)) 

s = stackers.ParallaxFactorStacker() 

self.assertFalse(isinstance(s, stackers.BaseDitherStacker)) 

 

def testRandomDither(self): 

""" 

Test the random dither pattern. 

""" 

maxDither = .5 

data = np.zeros(600, dtype=list(zip( 

['fieldRA', 'fieldDec'], [float, float]))) 

# Set seed so the test is stable 

rng = np.random.RandomState(42) 

# Restrict dithers to area where wraparound is not a problem for 

# comparisons. 

data['fieldRA'] = np.degrees(rng.random_sample(600) * (np.pi) + np.pi / 2.0) 

data['fieldDec'] = np.degrees(rng.random_sample(600) * np.pi / 2.0 - np.pi / 4.0) 

stacker = stackers.RandomDitherFieldPerVisitStacker(maxDither=maxDither) 

data = stacker.run(data) 

diffsra = (data['fieldRA'] - data['randomDitherFieldPerVisitRa']) \ 

* np.cos(np.radians(data['fieldDec'])) 

diffsdec = data['fieldDec'] - data['randomDitherFieldPerVisitDec'] 

# Check dithers within expected range. 

self._tDitherRange(diffsra, diffsdec, 

data['fieldRA'], data['fieldDec'], maxDither) 

 

def testRandomDitherPerNight(self): 

""" 

Test the per-night random dither pattern. 

""" 

maxDither = 0.5 

ndata = 600 

# Set seed so the test is stable 

rng = np.random.RandomState(42) 

 

data = np.zeros(ndata, dtype=list(zip(['fieldRA', 'fieldDec', 'fieldId', 'night'], 

[float, float, int, int]))) 

data['fieldRA'] = rng.rand(ndata) * (np.pi) + np.pi / 2.0 

data['fieldDec'] = rng.rand(ndata) * np.pi / 2.0 - np.pi / 4.0 

data['fieldId'] = np.floor(rng.rand(ndata) * ndata) 

data['night'] = np.floor(rng.rand(ndata) * 10).astype('int') 

stacker = stackers.RandomDitherPerNightStacker(maxDither=maxDither) 

data = stacker.run(data) 

diffsra = (np.radians(data['fieldRA']) - np.radians(data['randomDitherPerNightRa'])) \ 

* np.cos(np.radians(data['fieldDec'])) 

diffsdec = np.radians(data['fieldDec']) - np.radians(data['randomDitherPerNightDec']) 

self._tDitherRange(diffsra, diffsdec, 

data['fieldRA'], data['fieldDec'], maxDither) 

# Check that dithers on the same night are the same. 

self._tDitherPerNight(diffsra, diffsdec, data['fieldRA'], 

data['fieldDec'], data['night']) 

 

def testSpiralDitherPerNight(self): 

""" 

Test the per-night spiral dither pattern. 

""" 

maxDither = 0.5 

ndata = 2000 

# Set seed so the test is stable 

rng = np.random.RandomState(42) 

 

data = np.zeros(ndata, dtype=list(zip(['fieldRA', 'fieldDec', 'fieldId', 'night'], 

[float, float, int, int]))) 

data['fieldRA'] = rng.rand(ndata) * (np.pi) + np.pi / 2.0 

data['fieldRA'] = np.zeros(ndata) + np.pi / 2.0 

data['fieldDec'] = rng.rand(ndata) * np.pi / 2.0 - np.pi / 4.0 

data['fieldDec'] = np.zeros(ndata) 

data['fieldId'] = np.floor(rng.rand(ndata) * ndata) 

data['night'] = np.floor(rng.rand(ndata) * 20).astype('int') 

stacker = stackers.SpiralDitherPerNightStacker(maxDither=maxDither) 

data = stacker.run(data) 

diffsra = (data['fieldRA'] - data['spiralDitherPerNightRa']) \ 

* np.cos(np.radians(data['fieldDec'])) 

diffsdec = data['fieldDec'] - data['spiralDitherPerNightDec'] 

self._tDitherRange(diffsra, diffsdec, 

data['fieldRA'], data['fieldDec'], maxDither) 

# Check that dithers on the same night are the same. 

self._tDitherPerNight(diffsra, diffsdec, data['fieldRA'], data[ 

'fieldDec'], data['night']) 

 

def testHexDitherPerNight(self): 

""" 

Test the per-night hex dither pattern. 

""" 

maxDither = 0.5 

ndata = 2000 

# Set seed so the test is stable 

rng = np.random.RandomState(42) 

 

data = np.zeros(ndata, dtype=list(zip(['fieldRA', 'fieldDec', 'fieldId', 'night'], 

[float, float, int, int]))) 

data['fieldRA'] = rng.rand(ndata) * (np.pi) + np.pi / 2.0 

data['fieldDec'] = rng.rand(ndata) * np.pi / 2.0 - np.pi / 4.0 

data['fieldId'] = np.floor(rng.rand(ndata) * ndata) 

data['night'] = np.floor(rng.rand(ndata) * 217).astype('int') 

stacker = stackers.HexDitherPerNightStacker(maxDither=maxDither) 

data = stacker.run(data) 

diffsra = (data['fieldRA'] - data['hexDitherPerNightRa']) \ 

* np.cos(np.radians(data['fieldDec'])) 

diffsdec = data['fieldDec'] - data['hexDitherPerNightDec'] 

self._tDitherRange(diffsra, diffsdec, 

data['fieldRA'], data['fieldDec'], maxDither) 

# Check that dithers on the same night are the same. 

self._tDitherPerNight(diffsra, diffsdec, data['fieldRA'], 

data['fieldDec'], data['night']) 

 

def testRandomRotDitherPerFilterChangeStacker(self): 

""" 

Test the rotational dither stacker. 

""" 

maxDither = 90 

filt = np.array(['r', 'r', 'r', 'g', 'g', 'g', 'r', 'r']) 

rotTelPos = np.array([0, 0, 1, 0, .5, 1, 0, 180], float) 

odata = np.zeros(len(filt), dtype=list(zip(['filter', 'rotTelPos'], [(np.str_, 1), float]))) 

odata['filter'] = filt 

odata['rotTelPos'] = rotTelPos 

stacker = stackers.RandomRotDitherPerFilterChangeStacker(maxDither=maxDither, degrees=True, 

randomSeed=99) 

data = stacker.run(odata) 

randomDithers = data['randomDitherPerFilterChangeRotTelPos'] 

rotOffsets = rotTelPos - randomDithers 

self.assertEqual(rotOffsets[0], 0) 

offsetChanges = np.where(rotOffsets[1:] != rotOffsets[:-1])[0] 

filtChanges = np.where(filt[1:] != filt[:-1])[0] 

# Don't count last offset change because this was just value to force min/max limit. 

np.testing.assert_array_equal(offsetChanges[:-1], filtChanges) 

self.assertLessEqual(randomDithers.max(), 90.0) 

stacker = stackers.RandomRotDitherPerFilterChangeStacker(maxDither=maxDither, 

degrees=True, maxRotAngle = 30, 

randomSeed=19231) 

data = stacker.run(odata) 

randomDithers = data['randomDitherPerFilterChangeRotTelPos'] 

self.assertEqual(randomDithers.max(), 30.0) 

 

def testHAStacker(self): 

"""Test the Hour Angle stacker""" 

data = np.zeros(100, dtype=list(zip(['observationStartLST', 'fieldRA'], [float, float]))) 

data['observationStartLST'] = np.arange(100) / 99. * np.pi * 2 

stacker = stackers.HourAngleStacker(degrees=True) 

data = stacker.run(data) 

# Check that data is always wrapped 

self.assertLess(np.max(data['HA']), 12.) 

self.assertGreater(np.min(data['HA']), -12.) 

# Check that HA is zero if lst == RA 

data = np.zeros(1, dtype=list(zip(['observationStartLST', 'fieldRA'], [float, float]))) 

data = stacker.run(data) 

self.assertEqual(data['HA'], 0.) 

data = np.zeros(1, dtype=list(zip(['observationStartLST', 'fieldRA'], [float, float]))) 

data['observationStartLST'] = 20. 

data['fieldRA'] = 20. 

data = stacker.run(data) 

self.assertEqual(data['HA'], 0.) 

# Check a value 

data = np.zeros(1, dtype=list(zip(['observationStartLST', 'fieldRA'], [float, float]))) 

data['observationStartLST'] = 0. 

data['fieldRA'] = np.degrees(np.pi / 2.) 

data = stacker.run(data) 

np.testing.assert_almost_equal(data['HA'], -6.) 

 

def testPAStacker(self): 

""" Test the parallacticAngleStacker""" 

data = np.zeros(100, dtype=list(zip( 

['observationStartMJD', 'fieldDec', 'fieldRA', 'observationStartLST'], [float] * 4))) 

data['observationStartMJD'] = np.arange(100) * .2 + 50000 

site = Site(name='LSST') 

data['observationStartLST'], last = calcLmstLast(data['observationStartMJD'], site.longitude_rad) 

data['observationStartLST'] = data['observationStartLST']*180./12. 

stacker = stackers.ParallacticAngleStacker(degrees=True) 

data = stacker.run(data) 

# Check values are in good range 

assert(data['PA'].max() <= 180) 

assert(data['PA'].min() >= -180) 

 

# Check compared to the util 

check_pa = [] 

ras = np.radians(data['fieldRA']) 

decs = np.radians(data['fieldDec']) 

for ra, dec, mjd in zip(ras, decs, data['observationStartMJD']): 

alt, az, pa = _altAzPaFromRaDec(ra, dec, 

ObservationMetaData(mjd=mjd, site=site)) 

 

check_pa.append(pa) 

check_pa = np.degrees(check_pa) 

np.testing.assert_array_almost_equal(data['PA'], check_pa, decimal=0) 

 

def testFilterColorStacker(self): 

"""Test the filter color stacker.""" 

data = np.zeros(60, dtype=list(zip(['filter'], ['<U1']))) 

data['filter'][0:10] = 'u' 

data['filter'][10:20] = 'g' 

data['filter'][20:30] = 'r' 

data['filter'][30:40] = 'i' 

data['filter'][40:50] = 'z' 

data['filter'][50:60] = 'y' 

stacker = stackers.FilterColorStacker() 

data = stacker.run(data) 

# Check if re-run stacker raises warning (adding column twice). 

with warnings.catch_warnings(record=True) as w: 

warnings.simplefilter("always") 

data = stacker.run(data) 

assert len(w) > 1 

assert "already present in simData" in str(w[-1].message) 

# Check if use non-recognized filter raises exception. 

data = np.zeros(600, dtype=list(zip(['filter'], ['<U1']))) 

data['filter'] = 'q' 

self.assertRaises(IndexError, stacker.run, data) 

 

def testGalacticStacker(self): 

""" 

Test the galactic coordinate stacker 

""" 

ra, dec = np.degrees(np.meshgrid(np.arange(0, 2. * np.pi, 0.1), 

np.arange(-np.pi, np.pi, 0.1))) 

ra = np.ravel(ra) 

dec = np.ravel(dec) 

data = np.zeros(ra.size, dtype=list(zip(['ra', 'dec'], [float] * 2))) 

data['ra'] += ra 

data['dec'] += dec 

s = stackers.GalacticStacker(raCol='ra', decCol='dec') 

newData = s.run(data) 

expectedL, expectedB = _galacticFromEquatorial(np.radians(ra), np.radians(dec)) 

np.testing.assert_array_equal(newData['gall'], expectedL) 

np.testing.assert_array_equal(newData['galb'], expectedB) 

 

# Check that we have all the quadrants populated 

q1 = np.where((newData['gall'] < np.pi) & (newData['galb'] < 0.))[0] 

q2 = np.where((newData['gall'] < np.pi) & (newData['galb'] > 0.))[0] 

q3 = np.where((newData['gall'] > np.pi) & (newData['galb'] < 0.))[0] 

q4 = np.where((newData['gall'] > np.pi) & (newData['galb'] > 0.))[0] 

assert(q1.size > 0) 

assert(q2.size > 0) 

assert(q3.size > 0) 

assert(q4.size > 0) 

 

def testOpSimFieldStacker(self): 

""" 

Test the OpSimFieldStacker 

""" 

rng = np.random.RandomState(812351) 

s = stackers.OpSimFieldStacker(raCol='ra', decCol='dec', degrees=False) 

 

# First sanity check. Make sure the center of the fields returns the right field id 

opsim_fields_db = FieldsDatabase() 

 

# Returned RA/Dec coordinates in degrees 

field_id, ra, dec = opsim_fields_db.get_id_ra_dec_arrays("select * from Field;") 

 

data = np.array(list(zip(np.radians(ra), 

np.radians(dec))), 

dtype=list(zip(['ra', 'dec'], [float, float]))) 

new_data = s.run(data) 

 

np.testing.assert_array_equal(field_id, new_data['opsimFieldId']) 

 

# Cherry picked a set of coordinates that should belong to a certain list of fields. 

# These coordinates are not exactly at the center of fields, but close enough that 

# they should be classified as belonging to them. 

ra_inside_2548 = (10. + 1. / 60 + 6.59 / 60. / 60.) * np.pi / 12. # 10:01:06.59 

dec_inside_2548 = np.radians(-1. * (2. + 8. / 60. + 27.6 / 60. / 60.)) # -02:08:27.6 

 

ra_inside_8 = (8. + 49. / 60 + 19.83 / 60. / 60.) * np.pi / 12. # 08:49:19.83 

dec_inside_8 = np.radians(-1. * (85. + 19. / 60. + 04.7 / 60. / 60.)) # -85:19:04.7 

 

ra_inside_1253 = (9. + 16. / 60 + 13.67 / 60. / 60.) * np.pi / 12. # 09:16:13.67 

dec_inside_1253 = np.radians(-1. * (30. + 23. / 60. + 41.4 / 60. / 60.)) # -30:23:41.4 

 

data = np.zeros(3, dtype=list(zip(['ra', 'dec'], [float, float]))) 

field_id = np.array([2548, 8, 1253], dtype=int) 

data['ra'] = np.array([ra_inside_2548, ra_inside_8, ra_inside_1253]) 

data['dec'] = np.array([dec_inside_2548, dec_inside_8, dec_inside_1253]) 

 

new_data = s.run(data) 

 

np.testing.assert_array_equal(field_id, new_data['opsimFieldId']) 

 

# Now let's generate a set of random coordinates and make sure they are all assigned a fieldID. 

data = np.array(list(zip(rng.rand(600) * 2. * np.pi, 

rng.rand(600) * np.pi - np.pi / 2.)), 

dtype=list(zip(['ra', 'dec'], [float, float]))) 

 

new_data = s.run(data) 

 

self.assertGreater(new_data['opsimFieldId'].max(), 0) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()