-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_Functions.py
More file actions
630 lines (535 loc) · 22.6 KB
/
Project_Functions.py
File metadata and controls
630 lines (535 loc) · 22.6 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import numpy as np
class Object:
def __init__():[]
def __init__(self, SDSS_NAME, R, D, Z_VI, CLASS_P, p, mjd, fid,PSFMAG):
#leaving out redshift for now
R = round(R,2)
D = round(D,2)
self.name = SDSS_NAME
self.RA = R
self.Dec = D
self.z = Z_VI
self.Class_p = CLASS_P
self.Plate = p
self.MJD = mjd
self.FiberID = fid
self.Mag = PSFMAG
class FluxStore:
def __init__():[]
def __init__(self,PF):
#leaving out redshift for now
self.PlateFlux = PF
def Rebin(Plate_hdu, Bin_Size ):
rebin = []
rebin_weight=[]
obj=0
while obj < len(Plate_hdu):
current_flux = Plate_hdu[obj][0]
current_mask = Plate_hdu[obj][2]
current_inv = Plate_hdu[obj][3]
rebin_flux=[]
weight_=[]
pix=0
while pix < len(current_flux):
bin_no=0
x_flux=0
W=0
while bin_no<Bin_Size:
pix = pix+bin_no
if pix< len(current_flux):
w_ = 0
m = current_mask[pix]
if m==0:
w_=0
else:
w_ = current_inv[pix]
x_flux = x_flux +(current_flux[pix]*w_)
W=W+w_
bin_no=bin_no+1
else:
bin_no=bin_no+1
if W == 0:
x_flux=0
else:
x_flux = x_flux/W
rebin_flux.append(x_flux)
weight_.append(W)
rebin.append(rebin_flux)
rebin_weight.append(weight_)
obj=obj+1
return rebin, rebin_weight
def StandardRebin(Plate_hdu,wavelength, Bin_Size ):
rebin = []
rebin_weight=[]
bin_wav = Bin_Size*0.829026074968624
wavelength=wavelength[:4600]
rebinwav=[]
obj=0
while obj < len(Plate_hdu):
last_wav=0
current_flux = Plate_hdu[obj][0]
current_mask = Plate_hdu[obj][2]
current_inv = Plate_hdu[obj][3]
rebin_flux=[]
weight_=[]
cwav=[]
pix=0
First = True
bin_max = 3600
while pix < len(current_flux):
bin_no=0
x_flux=0
W=0
wav=0
if First:
cwav.append(bin_max+(bin_wav*0.5))
bin_max = bin_max+bin_wav
First = False
else:
bin_max = bin_max+bin_wav
cwav.append(bin_max+(bin_wav*0.5))
while wav<bin_max:
if pix< len(current_flux):
w_ = 0
m = current_mask[pix]
wav = wavelength[pix]
if m >0:
w_=0
else:
w_ = current_inv[pix]
x_flux = x_flux +(current_flux[pix]*w_)
W=W+w_
pix=pix+1
else:
pix=pix+1
wav=bin_max
if W == 0:
x_flux=0
else:
x_flux = x_flux/W
rebin_flux.append(x_flux)
weight_.append(W)
rebinwav.append(cwav)
rebin.append(rebin_flux)
rebin_weight.append(weight_)
obj=obj+1
print("Percentage Completed: "+np.str(((obj*100))/len(Plate_hdu))+"%")
return rebin, rebin_weight,rebinwav
def MLADatabasic(Full_Data,BinInfos,Flux,log_wavs):
All_Y=[]
All_X = []
All_redshifts=[]
All_Mag=[]
All_Name=[]
wav_logs=[]
plate_no = 0
y=0
while plate_no < len(Full_Data):
#loading matched objects with sup, plate data
CurrentSup_data = Full_Data[plate_no]
CurrentBin = BinInfos[plate_no]
CurrentFlux = Flux[plate_no]
wav= log_wavs[plate_no]
Plate_Y = []
Plate_X = []
Plate_redshifts=[]
Plate_Mag=[]
Plate_Name=[]
#first object is zeroth element
Sup_obj =0
while Sup_obj < len(CurrentSup_data):
#to stop double counting
no_match = True
#looking at an object that has been matched in sup list
CurrentSup = CurrentSup_data[Sup_obj]
#going through each object in the bin
BinObj_No = 0
while BinObj_No<len(CurrentBin):
BinObj = CurrentBin[BinObj_No]
##checking if the two match
if BinObj['FIBERID'] == CurrentSup.FiberID:
y=y+1
if no_match:
if CurrentSup.Class_p == 0:
a=0
else:
no_match = False
if CurrentSup.Class_p == 3 or CurrentSup.Class_p==30:
if CurrentSup.z < 2.1:
Plate_Y.append(3)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
Plate_X.append(x_flux)
Plate_redshifts.append(CurrentSup.z)
Plate_Mag.append(CurrentSup.Mag)
Plate_Name.append(CurrentSup.name)
else:
Plate_Y.append(30)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
Plate_X.append(x_flux)
Plate_redshifts.append(CurrentSup.z)
Plate_Mag.append(CurrentSup.Mag)
Plate_Name.append(CurrentSup.name)
else:
Plate_Y.append(CurrentSup.Class_p)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
Plate_X.append(x_flux)
Plate_redshifts.append(CurrentSup.z)
Plate_Mag.append(CurrentSup.Mag)
Plate_Name.append(CurrentSup.name)
BinObj_No=BinObj_No+1
Sup_obj=Sup_obj+1
All_Y.append(Plate_Y)
All_X.append(Plate_X)
All_Name.append(Plate_Name)
plate_no = plate_no+1
wav_logs.append(wav)
All_redshifts.append(Plate_redshifts)
All_Mag.append(Plate_Mag)
return All_X,All_Y,All_redshifts,All_Mag,wav_logs,All_Name
def MLAData(Full_Data,BinInfos,Flux,log_wavs,ANDMASK, INV, MJDs, PlateIDs):
All_Y=[]
All_X = []
All_redshifts=[]
All_Mag=[]
All_AND=[]
All_Inv=[]
All_Name=[]
All_MJDs = []
MatchedPlates = []
wav_logs=[]
plate_no = 0
y=0
while plate_no < len(Full_Data):
#loading matched objects with sup, plate data
CurrentSup_data = Full_Data[plate_no]
CurrentBin = BinInfos[plate_no]
CurrentFlux = Flux[plate_no]
CurrentAndM= ANDMASK[plate_no]
CurrentInv = INV[plate_no]
wav= log_wavs[plate_no]
currentmjd = MJDs[plate_no]
Plate_Y = []
Plate_X = []
Plate_AND=[]
Plate_Inv=[]
Plate_redshifts=[]
Plate_Mag=[]
Plate_Name=[]
pid=PlateIDs[plate_no]
plate_match = False
#first object is zeroth element
Sup_obj =0
while Sup_obj < len(CurrentSup_data):
#to stop double counting
no_match = True
#looking at an object that has been matched in sup list
CurrentSup = CurrentSup_data[Sup_obj]
#going through each object in the bin
BinObj_No = 0
while BinObj_No<len(CurrentBin):
BinObj = CurrentBin[BinObj_No]
ObjAndM= CurrentAndM[BinObj_No]
ObjInv = CurrentInv[BinObj_No]
##checking if the two match
if BinObj['FIBERID'] == CurrentSup.FiberID:
if currentmjd==CurrentSup.MJD :
plate_match = True
if no_match:
if CurrentSup.Class_p == 0:
a=0
else:
no_match = False
if CurrentSup.Class_p == 3 or CurrentSup.Class_p==30:
if CurrentSup.z < 2.1:
Plate_Y.append(1)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
Plate_X.append(x_flux)
Plate_redshifts.append(CurrentSup.z)
Plate_AND.append(ObjAndM)
Plate_Inv.append(ObjInv)
Plate_Mag.append(CurrentSup.Mag)
Plate_Name.append(CurrentSup.name)
else:
Plate_Y.append(3)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
Plate_X.append(x_flux)
Plate_redshifts.append(CurrentSup.z)
Plate_AND.append(ObjAndM)
Plate_Inv.append(ObjInv)
Plate_Mag.append(CurrentSup.Mag)
Plate_Name.append(CurrentSup.name)
else:
if CurrentSup.Class_p ==1:
Plate_Y.append(0)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
Plate_X.append(x_flux)
Plate_redshifts.append(CurrentSup.z)
Plate_AND.append(ObjAndM)
Plate_Inv.append(ObjInv)
Plate_Mag.append(CurrentSup.Mag)
Plate_Name.append(CurrentSup.name)
else:
Plate_Y.append(2)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
Plate_X.append(x_flux)
Plate_redshifts.append(CurrentSup.z)
Plate_AND.append(ObjAndM)
Plate_Inv.append(ObjInv)
Plate_Mag.append(CurrentSup.Mag)
Plate_Name.append(CurrentSup.name)
BinObj_No=BinObj_No+1
Sup_obj=Sup_obj+1
if plate_match:
All_Y.append(Plate_Y)
All_X.append(Plate_X)
All_Name.append(Plate_Name)
plate_no = plate_no+1
wav_logs.append(wav)
All_redshifts.append(Plate_redshifts)
All_Mag.append(Plate_Mag)
All_AND.append(Plate_AND)
All_Inv.append(Plate_Inv)
All_MJDs.append(currentmjd)
MatchedPlates.append(pid)
plate_no=plate_no+1
else:
plate_no=plate_no+1
return All_X,All_Y,All_redshifts,All_Mag,All_AND,All_Inv,wav_logs,All_Name,All_MJDs,MatchedPlates
def classification(objectclass, Trainingclass, prediction):
## Star, Quasar, Galaxy, BAL
classi = []
##Location of object that is predicted to be a given classification
loc = [0,0,0,0]
star=0;
#quasar w/ redshift <2.1
qso=0;
gal=0;
#quasar w/ redshift >2.1
bal=0;
starloc=[];
qsoloc=[];
galloc=[];
balloc=[];
i=0
while i< len(Trainingclass):
currentobject = Trainingclass[i]
currentpred = prediction[i]
if objectclass==currentobject:
if currentpred==0:
star=star+1
starloc.append(i)
elif currentpred==1:
qso=qso+1
qsoloc.append(i)
elif currentpred==2:
gal=gal+1
galloc.append(i)
elif currentpred==3:
bal=bal+1
balloc.append(i)
i=i+1
classi.append(star/(star+qso+gal+bal))
classi.append(qso/(star+qso+gal+bal))
classi.append(gal/(star+qso+gal+bal))
classi.append(bal/(star+qso+gal+bal))
return classi,starloc,qsoloc,galloc,balloc
def storing(PLATEIDs,HDU):
Full_Data=[]
Plate_Count =0
supers = HDU
while Plate_Count < len(PLATEIDs):
Current_Plate = PLATEIDs[Plate_Count]
CurrentObject = 0
platename_data = []
currentSup_size =len(supers)
while CurrentObject < currentSup_size:
Current = supers[CurrentObject]
##Checking if object was surveyed by current plate of interest
if Current['PLATE'] == Current_Plate:
##Storing object data
Object_ = Object(Current['SDSS_NAME'], Current['RA'], Current['Dec'],
Current['Z_VI'], Current['CLASS_PERSON'],Current['PLATE'] ,
Current['MJD'], Current['FIBERID'],Current['PSFMAG'])
##Adding object data to array containing data from other objects of the same plate
platename_data.append(Object_)
CurrentObject=CurrentObject+1
Plate_Count = Plate_Count + 1
Full_Data.append(platename_data)
return Full_Data
def MLADataBin(Full_Data,BinInfos,Flux,log_wavs, ANDMASK, INV, BIN_Size):
All_Y=[]
All_X = []
All_redshifts=[]
All_Mag=[]
wav_logs=[]
All_AND=[]
All_Inv=[]
plate_no = 0
y=0
while plate_no < len(Full_Data):
#loading matched objects with sup, plate data
CurrentSup_data = Full_Data[plate_no]
CurrentBin = BinInfos[plate_no]
CurrentFlux = Flux[plate_no]
CurrentAndM= ANDMASK[plate_no]
CurrentInv = INV[plate_no]
wav= log_wavs[plate_no]
Plate_Y = []
Plate_X = []
Plate_AND=[]
Plate_Inv=[]
#first object is zeroth element
Sup_obj =0
while Sup_obj < len(CurrentSup_data):
#to stop double counting
no_match = True
#looking at an object that has been matched in sup list
CurrentSup = CurrentSup_data[Sup_obj]
#going through each object in the bin
BinObj_No = 0
while BinObj_No<len(CurrentBin):
X=[]
BinObj = CurrentBin[BinObj_No]
ObjAndM= CurrentAndM[plate_no]
ObjInv = CurrentInv[plate_no]
##checking if the two match
if BinObj['FIBERID'] == CurrentSup.FiberID:
y=y+1
if no_match:
if CurrentSup.Class_p == 0:
a=0
else:
no_match = False
if CurrentSup.Class_p == 3 or CurrentSup.Class_p==30:
if CurrentSup.z < 2.1:
Plate_Y.append(3)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
All_redshifts.append(CurrentSup.z)
All_Mag.append(CurrentSup.Mag)
wav_logs.append(wav)
pixno=0
while pixno<len(x_flux):
bin_count =0
x_= 0
w=0
while bin_count<BIN_Size:
x_ = x_+x_flux[pixno+bin_count]*(ObjInv[pixno+bin_count])
w=w+ObjInv[pixno+bin_count]
x_=x_/w
X.append(x_)
Plate_X.append(X)
else:
Plate_Y.append(30)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
All_redshifts.append(CurrentSup.z)
All_Mag.append(CurrentSup.Mag)
wav_logs.append(wav)
while pixno<len(x_flux):
bin_count =0
x_= 0
w=0
while bin_count<BIN_Size:
x_ = x_+x_flux[pixno+bin_count]*(ObjInv[pixno+bin_count])
w=w+ObjInv[pixno+bin_count]
x_=x_/w
X.append(x_)
Plate_X.append(X)
else:
Plate_Y.append(CurrentSup.Class_p)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
All_redshifts.append(CurrentSup.z)
All_Mag.append(CurrentSup.Mag)
wav_logs.append(wav)
while pixno<len(x_flux):
bin_count =0
x_= 0
w=0
while bin_count<BIN_Size:
x_ = x_+x_flux[pixno+bin_count]*(ObjInv[pixno+bin_count])
w=w+ObjInv[pixno+bin_count]
x_=x_/w
X.append(x_)
Plate_X.append(X)
BinObj_No=BinObj_No+1
Plate_X
Sup_obj=Sup_obj+1
All_Y.append(Plate_Y)
All_X.append(Plate_X)
plate_no = plate_no+1
return All_X,All_Y,All_redshifts,All_Mag,wav_logs
def MLADataTest(Full_Data,BinInfos,Flux,log_wavs):
Plate_Y = []
Plate_X = []
All_redshifts=[]
All_Mag=[]
wav_logs=[]
plate_no = 0
y=0
while plate_no < len(Full_Data):
Y = []
X = []
#loading matched objects with sup, plate data
CurrentSup_data = Full_Data[plate_no]
CurrentBin = BinInfos[plate_no]
CurrentFlux = Flux[plate_no]
wav= log_wavs[plate_no]
#first object is zeroth element
Sup_obj =0
while Sup_obj < len(CurrentSup_data):
#to stop double counting
no_match = True
#looking at an object that has been matched in sup list
CurrentSup = CurrentSup_data[Sup_obj]
#going through each object in the bin
BinObj_No = 0
while BinObj_No<len(CurrentBin):
BinObj = CurrentBin[BinObj_No]
##checking if the two match
if BinObj['FIBERID'] == CurrentSup.FiberID:
y=y+1
if no_match:
if CurrentSup.Class_p == 0:
a=0
else:
no_match = False
if CurrentSup.Class_p == 3 or CurrentSup.Class_p==30:
if CurrentSup.z < 2.1:
Y.append(3)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
X.append(x_flux)
All_redshifts.append(CurrentSup.z)
All_Mag.append(CurrentSup.Mag)
wav_logs.append(wav)
else:
Y.append(30)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
X.append(x_flux)
All_redshifts.append(CurrentSup.z)
All_Mag.append(CurrentSup.Mag)
wav_logs.append(wav)
else:
Y.append(CurrentSup.Class_p)
x_flux =(CurrentFlux[BinObj_No])
x_flux=x_flux[:4600]
X.append(x_flux)
All_redshifts.append(CurrentSup.z)
All_Mag.append(CurrentSup.Mag)
wav_logs.append(wav)
BinObj_No=BinObj_No+1
Sup_obj=Sup_obj+1
plate_no = plate_no+1
Plate_X.append(X)
Plate_Y.append(Y)
return Plate_X,Plate_Y,All_redshifts,All_Mag,wav_logs