-
Notifications
You must be signed in to change notification settings - Fork 2
/
optimal_spline_basepoints.py
executable file
·1317 lines (1075 loc) · 48.7 KB
/
optimal_spline_basepoints.py
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import os, sys, numpy, time
import scipy, scipy.interpolate, scipy.spatial, scipy.ndimage
from astropy.io import fits
import pysalt.mp_logging
import logging
import bottleneck
import wlcal
import skyline_intensity
import logging
import find_edges_of_skylines
import fastedge
import skytrace
import wlmodel
import localnoise
import plot_high_res_sky_spec
import math
import quickwlmodel
lots_of_debug = True
import spline_pickle_test
use_fast_edges = True
lots_of_debug = True#False
def satisfy_schoenberg_whitney(data, basepoints, k=3):
logger = logging.getLogger("SchoenbergWhitney")
logger.debug("Starting with %d basepoints for %d datapoints" % (
basepoints.shape[0], data.shape[0]))
delete = numpy.isnan(basepoints)
count,bins = numpy.histogram(
a=data,
bins=basepoints,
)
delete[count == 0] = True
# logger.debug("done with histogram method, continuing old-fashioned way")
# for idx in range(basepoints.shape[0]-1):
# # count how many data points are between this and the next basepoint
# in_range = (data > basepoints[idx]) & (data < basepoints[idx+1])
# count = numpy.sum(in_range)
# if (count <= k):
# # delete this basepoint
# delete[idx] = True
# logger.debug("BP % 5d: Deleting basepoint @ %.5f (idx, %d data points, < %d)" % (
# idx, basepoints[idx], count, k))
n_delete = numpy.sum(delete)
logger.debug("Deleting %d basepoints, left with %d" % (
n_delete, basepoints.shape[0]-n_delete))
#logger.debug("%s %s" % (str(delete.shape), str(delete2.shape)))
#logger.debug("old vs new: %s" % ((delete[:-1] == delete2)))
return basepoints[~delete]
def find_source_mask(img_data, debug=False):
logger = logging.getLogger("FindSrcMask")
#
# Flatten image in wavelength direction
#
flat = bottleneck.nanmedian(img_data.astype(numpy.float32), axis=1)
# print img_data.shape, flat.shape
if (debug):
numpy.savetxt("obj_mask.flat", flat)
median_level = numpy.median(flat)
logger.debug("found median flux level: %f" % (median_level))
# do running median filter
filter_size = int(0.1*img_data.shape[0])
med_filt = scipy.ndimage.filters.median_filter(flat.reshape((-1,1)),
size=filter_size,
mode='constant')[:,0]
if (debug):
numpy.savetxt("obj_mask.medfilt", med_filt)
excess = flat - med_filt
good = numpy.isfinite(excess, dtype=numpy.bool)
#print good
#print numpy.sum(good)
combined = numpy.append(numpy.arange(excess.shape[0]).reshape((-1,1)),
excess.reshape((-1,1)), axis=1)
if (debug):
numpy.savetxt("obj_mask.excess", combined)
# compute noise
for i in range(3):
stats = numpy.percentile(excess[good], [16,50,84])
_med = stats[1] #numpy.median(excess[good])
_std = 0.5*(stats[2]-stats[0]) #numpy.std(excess[good])
logger.debug("Iteration %d: median=%f, std=%f" % (
i+1, _med, _std
))
# print _med, _std
good = (excess > _med-3*_std) & (excess < _med+3*_std)
# print numpy.sum(good)
if (debug):
numpy.savetxt("obj_mask.filter%d" % (i+1), combined[good])
source = excess > (_med+5*_std) #~good
# print source
source_mask = scipy.ndimage.filters.convolve(
input=source,
weights=numpy.ones((11)),
output=None,
mode='reflect', cval=0.0)
# print source_mask
if (debug):
numpy.savetxt("obj_mask.src", combined[source_mask])
return source_mask
def find_center_row(data):
# Create interpolator for the median profile
interp = scipy.interpolate.interp1d(
x=data[:,0], y=data[:,1], kind='linear',
bounds_error=False, fill_value=numpy.NaN)
#
# Optimization routine
#
def fold_profile(p, interp, maxy, count):
dx = numpy.arange(maxy, dtype=numpy.float)
x_left = p[0] - dx
x_right = p[0] + dx
profile_left = interp(x_left)
profile_right = interp(x_right)
diff = profile_left - profile_right
count[0] += 1
# print "iteration %d --> %e" % (count[0], p[0])
# with open("opt_%d.del" % (count[0]), "w") as f:
# numpy.savetxt(f, profile_left)
# print >>f, "\n"*5,
# numpy.savetxt(f, profile_right)
# print >>f, "\n"*5,
# numpy.savetxt(f, diff)
return diff[numpy.isfinite(diff)]
#
# Get rid of all points that are too noisy
#
w=5
noise = numpy.array([bottleneck.nanvar(data[i-w:i+w,1]) for i in range(w,data.shape[0]-w+1)])
# numpy.savetxt("median_noise", noise)
noise[:w] = numpy.NaN
noise[-w:] = numpy.NaN
for iteration in range(3):
valid = numpy.isfinite(noise)
_perc = numpy.percentile(noise[valid], [16,50,84])
_med = _perc[1]
_sigma = 0.5*(_perc[2]-_perc[0])
outlier = (noise > _med+3*_sigma) | (noise < _med - 3*_sigma)
noise[outlier] = numpy.NaN
#numpy.savetxt("median_noise2", noise)
valid = numpy.isfinite(noise)
data[:,1][~valid] = numpy.NaN
#numpy.savetxt("median_noise3", data)
count=[0]
fit_all = scipy.optimize.leastsq(
func=fold_profile,
x0=[data.shape[0]/5.],
args=(interp, data.shape[0]/2,count),
full_output=True,
epsfcn=1e-1,
)
#print fit_all[0]
return fit_all[0][0]
def optimal_sky_subtraction(obj_hdulist,
image_data=None,
sky_regions=None,
slitprofile=None,
N_points = 6000,
wlmode='arc',
compare=False,
iterate=False,
return_2d = True,
skiplength=1,
mask_objects=True,
add_edges=True,
skyline_flat=None,
select_region=None,
debug_prefix="",
obj_wl=None,
noise_mode='global',
debug=False):
logger = logging.getLogger("OptSplineKs")
skiplength = 1
lots_of_debug = False #debug
#wl_map = wlmap_model
logger.info("Preparing optimal sky-subtraction")
import time
time.sleep(1)
#print '\n'*5, 'img_data\n\n\n', image_data.shape, "\n", image_data, '\n'*5
if (image_data is None):
obj_data = obj_hdulist['SCI.RAW'].data #/ fm.reshape((-1,1))
#print "obj_data:", obj_data.shape
logger.debug("Using obj_data from SCI.RAW extension")
else:
obj_data = image_data.copy()
logger.debug("Using obj_data from passed image data")
#obj_wl = wlmap_model #wl_map #obj_hdulist['WAVELENGTH'].data
x_eff, wl_map, medians, p_scale, p_skew, fm = \
None, None, None, None, None, numpy.ones(obj_hdulist['SCI'].data.shape[0])
wlmap_model = None
y_center = 2070 #obj_hdulist['SCI'].data.shape[0]/2.
if (obj_wl is not None):
logger.info("Using existing WL model")
pass
elif (wlmode == 'arc'):
obj_wl = obj_hdulist['WAVELENGTH'].data
logger.info("Using wavelength solution from ARC")
elif (wlmode == 'sky'):
#
# Prepare a new refined wavelength map by using sky-lines
#
(x_eff, wl_map, medians, p_scale, p_skew, fm) = skytrace.create_wlmap_from_skylines(obj_hdulist)
obj_wl = wl_map
logger.info("Using wavelength solution constructed from SKY lines")
y_center = find_center_row(medians)
logger.info("Using row %.1f as center line of focal plane" % (y_center))
elif (wlmode == 'model'):
wlmap_model = wlmodel.rssmodelwave(
header=obj_hdulist[0].header,
img=obj_hdulist['SCI'].data,
xbin=4, ybin=4,
y_center=y_center)
obj_wl = wlmap_model
logger.info("Using synthetic model wavelength map")
else:
logger.error("Unknown WL mode, using ARC instead!")
obj_wl = obj_hdulist['WAVELENGTH'].data
# obj_wl = wl_map #wl_map #obj_hdulist['WAVELENGTH'].data
obj_rms = obj_hdulist['VAR'].data / fm.reshape((-1,1))
# store the wavelength map we end up using to return it to the main process
wl_map = obj_wl
if (lots_of_debug):
pysalt.clobberfile(debug_prefix+"XXX.fits")
obj_hdulist.writeto(debug_prefix+"XXX.fits", clobber=True)
try:
obj_spatial = obj_hdulist['SPATIAL'].data
except:
logger.warning("Could not find spatial map, using plain x/y coordinates instead")
obj_spatial, _ = numpy.indices(obj_data.shape)
# now merge all data frames into a single 3-d numpy array
logger.info("Combining all data required for 2-D sky estimation")
obj_cube = numpy.empty((obj_data.shape[0], obj_data.shape[1], 4))
obj_cube[:,:,0] = obj_wl[:,:]
obj_cube[:,:,1] = (obj_data*1.0)[:,:]
obj_cube[:,:,2] = obj_rms[:,:]
obj_cube[:,:,3] = obj_spatial[:,:]
good_sky_data = numpy.isfinite(obj_data)
#print good_sky_data
#print "\n\n #1: --\n", good_sky_data.shape, obj_cube.shape, "\n\n"
if (lots_of_debug):
pysalt.clobberfile(debug_prefix+"data_preflat.fits")
fits.PrimaryHDU(data=obj_cube[:,:,1]).writeto(debug_prefix+"data_preflat.fits", clobber=True)
if (skyline_flat is not None):
# We also received a skyline flatfield for field flattening
obj_cube[:,:,1] /= skyline_flat.reshape((-1,1))
logger.info("Applying skyline flatfield to data before sky-subtraction")
#return 1,2
pass
if (lots_of_debug):
pysalt.clobberfile(debug_prefix+"data_postflat.fits")
fits.PrimaryHDU(data=obj_cube[:,:,1]).writeto(debug_prefix+"data_postflat.fits", clobber=True)
# mask_objects = False
if (not mask_objects and select_region is None):
obj_bpm = numpy.array(obj_hdulist['BPM'].data).flatten()
good_sky_data &= (obj_hdulist['BPM'].data == 0)
logger.info("Using full-frame (no masking) for sky estimation")
else:
use4sky = numpy.ones((obj_cube.shape[0]), dtype=numpy.bool)
# by default, use entire frame for sky
if (mask_objects):
logger.debug("mask_object was selected, so looking for source_mask next")
source_mask = find_source_mask(obj_data)
use4sky = use4sky & (~source_mask)
logger.debug("Excluding %d rows contaminated by sources from "
"sky-estimation" % (numpy.sum(source_mask)))
# trim down sky by regions not contaminated with (strong) sources
if (select_region is not None):
logger.debug("limiting sky to selected regions")
sky = numpy.zeros((obj_cube.shape[0]), dtype=numpy.bool)
for y12 in select_region:
#print "@@@@@@@@@@",y12, numpy.sum(use4sky), use4sky.shape
logger.debug("Selecting sky-region: y =%d--%d (total: %d)" % (
y12[0], y12[1], numpy.sum(use4sky)))
sky[y12[0]:y12[1]] = True
use4sky = use4sky & sky
# also only select regions explicitely chosen as sky
#print "selecting:", use4sky.shape, numpy.sum(use4sky)
logger.info("Computing sky-spectrum from total of %d pixel-lines after masking" % (numpy.sum(use4sky)))
#
# mark all excluded regions as such and exclude them from the sky
# dataset
#
good_sky_data[~use4sky] = False
# obj_cube = obj_cube[use4sky]
if (lots_of_debug):
_x = numpy.array(obj_data)
_x[source_mask] = numpy.NaN
pysalt.clobberfile(debug_prefix+"obj_mask.fits")
fits.HDUList([
fits.PrimaryHDU(),
fits.ImageHDU(data=obj_data),
fits.ImageHDU(data=_x)]).writeto(debug_prefix+"obj_mask.fits")
obj_bpm = numpy.array(obj_hdulist['BPM'].data)[use4sky].flatten()
#print obj_bpm.shape, obj_cube.shape
#print "\n\n #2: --\n", good_sky_data.shape, obj_cube.shape, "\n\n"
# convert dataset from 3-d to 2-d
# obj_cube = obj_cube.reshape((-1, obj_cube.shape[2]))
fits.PrimaryHDU(data=good_sky_data.astype(numpy.int)).writeto(
"good_sky_data_x0.fits", clobber=True)
# Now exclude all pixels marked as bad
#valid_pixels = (obj_bpm == 0) & numpy.isfinite(obj_cube[:, 1])
#obj_cube = obj_cube[valid_pixels]
if (lots_of_debug):
fits.PrimaryHDU(data=good_sky_data.astype(numpy.int)).writeto(
"goodskydata.0.fits", clobber=True)
n_good_pixels = numpy.sum(good_sky_data)
logger.info("%d pixels (of %d, ~%.1f%%) left after eliminating bad "
"pixels!" % (
n_good_pixels,good_sky_data.size,
100.*n_good_pixels/good_sky_data.size))
#
# Now also exclude all points that are marked as non-sky regions
# (e.g. including source regions)
#
if (sky_regions is not None and
type(sky_regions) == numpy.ndarray):
logger.info("Selecting sky-pixels from user-defined regions")
logger.debug("Sky-regions: %s" % (str(sky_regions)))
is_sky = numpy.zeros((obj_cube.shape[0]), dtype=numpy.bool)
for idx, sky_region in enumerate(sky_regions):
logger.debug("Good region: %d ... %d" % (sky_region[0], sky_region[1]))
in_region = (obj_cube[:,3] > sky_region[0]) & \
(obj_cube[:,3] < sky_region[1]) & \
(numpy.isfinite(obj_cube[:,1]))
is_sky[in_region] = True
# obj_cube = obj_cube[is_sky]
else:
logger.info("No user-selected sky-regions, using full available frame")
#print "\n\n #3: --\n", good_sky_data.shape, obj_cube.shape, "\n\n"
allskies = obj_cube #[::skiplength]
if (lots_of_debug):
# numpy.savetxt(debug_prefix+"xxx1", allskies)
# print obj_cube.shape
# print good_sky_data.shape
_x = obj_cube[good_sky_data]
# print _x.shape
logger.debug("Saving debug output")
numpy.savetxt(debug_prefix+"xxx1",
obj_cube[good_sky_data].reshape((-1, obj_cube.shape[2])))
logger.debug("done with debug output")
# _x = fits.ImageHDU(data=obj_hdulist['SCI.RAW'].data,
# header=obj_hdulist['SCI.RAW'].header)
# _x.name = "STEP1"
# obj_hdulist.append(_x)
# #
# # Load and prepare data
# #
# allskies = numpy.loadtxt(allskies_filename)
# just to be on the safe side, sort allskies by wavelength
logger.debug("Sorting input data by wavelength")
allskies = obj_cube[good_sky_data].reshape((-1, obj_cube.shape[2]))
sky_sort_wl = numpy.argsort(allskies[:,0])
allskies = allskies[sky_sort_wl]
if (lots_of_debug):
logger.debug("writing debug output")
numpy.savetxt(debug_prefix+"xxx2", allskies[::skiplength])
logger.debug("done writing debug output")
logger.debug("Working on %7d data points to estimate sky" % (allskies.shape[0]))
#
# Compute cumulative distribution
#
logger.debug("Computing cumulative distribution")
allskies_cumulative = numpy.cumsum(allskies[:,1], axis=0)
# print allskies.shape, allskies_cumulative.shape, wl_sorted.shape
if (lots_of_debug):
numpy.savetxt(debug_prefix+"cumulative.asc",
numpy.append(allskies[::skiplength][:,0].reshape((-1,1)),
allskies_cumulative[::skiplength].reshape((-1,1)),
axis=1)
)
logger.debug("Cumulative flux range: %f ... %f" % (
allskies_cumulative[0], allskies_cumulative[-1]))
# os._exit(0)
#############################################################################
#
# Now create the basepoints by equally distributing them across the
# cumulative distribution. This naturally puts more basepoints into regions
# with more signal where more precision is needed
#
#############################################################################
# Create a simple interpolator to make life a bit easier
interp = scipy.interpolate.interp1d(
x=allskies_cumulative,
y=allskies[:,0],
kind='nearest',
bounds_error=False,
fill_value=-9999,
#assume_sorted=True,
)
# now create the raw basepoints in cumulative flux space
k_cumflux = numpy.linspace(allskies_cumulative[0],
allskies_cumulative[-1],
N_points+2)[1:-1]
# and using the interpolator, convert flux space into wavelength
k_wl = interp(k_cumflux)
logger.debug("Average basepoint spacing: %f A" % ((k_wl[-1]-k_wl[0])/k_wl.shape[0]))
# eliminate all negative-wavelength basepoints -
# these represent interpolation errors
k_wl = k_wl[k_wl>0]
if (lots_of_debug):
numpy.savetxt(debug_prefix+"opt_basepoints",
numpy.append(k_wl.reshape((-1,1)),
k_cumflux.reshape((-1,1)),
axis=1)
)
logger.debug("Done selecting %d spline base points" % (k_wl.shape[0]))
#############################################################################
#
# Add additional wavelength sampling points along the line-edges if
# this was requested.
#
#############################################################################
if (add_edges):
logger.info("Adding sky-samples for line edges")
dl = 3.
dn = 50
if (use_fast_edges):
logger.info("Using fast-edge method")
edges = fastedge.find_line_edges(allskies, line_sigma=2.75)
# distribute additional basepoints across 2. (+/- dl) angstroem
# for each edge
all_edge_points = numpy.empty((edges.shape[0], dn))
for ie, edge in enumerate(edges):
bp = numpy.linspace(edge-dl, edge+dl, dn)
all_edge_points[ie,:] = bp[:]
else:
pysalt.clobberfile(debug_prefix+"edges.cheat")
if (not os.path.isfile(debug_prefix+"edges.cheat")):
edges = find_edges_of_skylines.find_edges_of_skylines(allskies, fn="XXX")
numpy.savetxt(debug_prefix+"edges.cheat", edges)
else:
edges = numpy.loadtxt(debug_prefix+"edges.cheat")
# distribute additional basepoints across 2. (+/- dl) angstroem
# for each edge
all_edge_points = numpy.empty((edges.shape[0], dn))
for ie, edge in enumerate(edges[:,0]):
bp = numpy.linspace(edge-dl, edge+dl, dn)
all_edge_points[ie,:] = bp[:]
#
# Now merge the list of new basepoints with the existing list.
# sort this list ot make it a suitable input for spline fitting
#
if (lots_of_debug):
numpy.savetxt(debug_prefix+"k_wl.in", k_wl)
k_wl_new = numpy.append(k_wl, all_edge_points.flatten())
k_wl = numpy.sort(k_wl_new)
if (lots_of_debug):
numpy.savetxt(debug_prefix+"k_wl.out", k_wl)
#############################################################################
#
# Now we have the new optimal set of base points, let's compare it to the
# original with the same number of basepoints, sampling the available data
# with points equi-distant in wavelength space.
#
#############################################################################
wl_min, wl_max = numpy.min(allskies[:,0]), numpy.max(allskies[:,0])
logger.info("Found Min/Max WL-range: %.3f / %.3f" % (wl_min, wl_max))
if (compare):
logger.info("Computing spline using original/simple sampling")
wl_range = wl_max - wl_min
k_orig_ = numpy.linspace(wl_min, wl_max, N_points+2)[1:-1]
k_orig = satisfy_schoenberg_whitney(allskies[:,0], k_orig_, k=3)
spline_orig = scipy.interpolate.LSQUnivariateSpline(
x=allskies[:,0],
y=allskies[:,1],
t=k_orig,
w=None, # no weights (for now)
#bbox=None, #[wl_min, wl_max],
k=3, # use a cubic spline fit
)
if (lots_of_debug):
numpy.savetxt(debug_prefix+"spline_orig", numpy.append(k_orig.reshape((-1,1)),
spline_orig(k_orig).reshape((-1,1)),
axis=1)
)
logger.info("Computing spline using optimized sampling")
logger.debug("#datapoints: %d, #basepoints: %d" % (
allskies.shape[0], k_wl.shape[0]))
k_opt_good = satisfy_schoenberg_whitney(allskies[:,0], k_wl, k=3)
if (lots_of_debug):
logger.debug("Saving debug output")
numpy.savetxt(debug_prefix+"allskies", allskies)
fits.PrimaryHDU(data=allskies).writeto("allskies.fits", clobber=True)
numpy.savetxt(debug_prefix+"bp_in", k_wl)
numpy.savetxt(debug_prefix+"bp_out", k_opt_good)
logger.debug("done with debug output")
logger.info("Computing optimized sky-spectrum spline interpolator (%d data, %d base-points)" % (
allskies.shape[0], k_opt_good.shape[0]
))
try:
spline_opt = scipy.interpolate.LSQUnivariateSpline(
x=allskies[:,0],
y=allskies[:,1],
t=k_opt_good[::10], #k_wl,
w=None, # no weights (for now)
bbox=[wl_min, wl_max],
k=3, # use a cubic spline fit
)
except ValueError:
logger.error("ERROR: Unable to compute LSQUnivariateSpline (data: %d, bp=%d/10)" % (
allskies.shape[0], k_opt_good.shape[0]))
spline_opt = None
if (lots_of_debug and spline_opt is not None):
spec_simple = numpy.append(k_wl.reshape((-1,1)),
spline_opt(k_wl).reshape((-1,1)),
axis=1)
#numpy.savetxt(debug_prefix+"spline_opt", spec_simple)
fits.PrimaryHDU(data=good_sky_data.astype(numpy.int)).writeto(
"good_sky_data_x1.fits", clobber=True)
#
#
# Now we have a pretty good guess on what the entire sky spectrum looks like
# This means we can use known sky-lines to find and compensate for intensity
# variations
#
#
# if (not iterate):
# if (return_2d):
# pass
# else:
# # only return a 1-d spectrum, centered on the middle row
# line = obj_wl.shape[0]/2
# wl = obj_wl[line,:]
# spec = spline_opt(wl)
#
# return spec, None, None
# _x = fits.ImageHDU(data=obj_hdulist['SCI.RAW'].data,
# header=obj_hdulist['SCI.RAW'].header)
# _x.name = "STEP2"
# obj_hdulist.append(_x)
logger.info("Computing spline using optimized sampling and outlier rejection")
good_point = (allskies[:,0] > 0)
logger.info("Using a total of %d pixels for sky estimation" % (good_point.shape[0]))
#print good_point.shape
#print good_point
avg_sample_width = (numpy.max(k_wl) - numpy.min(k_wl)) / k_wl.shape[0]
# good_data = allskies[good_point]
spline_iter = None
n_iterations = 3
logger.info("Starting iteratively (%dx) computing best sky-spectrum, "
"using noise-mode %s" % (n_iterations, noise_mode))
strong_gradient_basepoints_added = False
wl_sort = numpy.argsort(obj_wl.flatten())
wl_unsort = numpy.argsort(wl_sort)
# print "wort/unsort:", wl_sort.shape, wl_unsort.shape
obj_cube_sorted = obj_cube.reshape((-1, obj_cube.shape[2]))[wl_sort]
good_sky_data_sorted = good_sky_data.reshape((-1,1))[wl_sort]
# print "obj-cube-sorted:", obj_cube_sorted.shape, good_sky_data_sorted.shape
for iteration in range(n_iterations):
logger.info("Starting sky-spectrum iteration %d of %d" % (
iteration+1, n_iterations)
)
# compute spline
# k_iter_good = satisfy_schoenberg_whitney(good_data[:,0], k_wl, k=3)
good_data = obj_cube_sorted[good_sky_data_sorted]
logger.info("good data: %s" % (str(good_data.shape)))
# print "***\n"*5,good_data.shape,"\n***"*5
# we now need to sort the data by wavelength
# logger.debug("Sorting input data for iteration %d" % (iteration+1))
#si = numpy.argsort(good_data[:,0])
#good_data = good_data[si]
#
# Search for regions of large scatter - these indicate something is
# not yet well described by the fit and thus could benefit from
# another spline basepoint in that region.
#
logger.info("Searching for additional spline basepoints")
noiseblocksize = 250
# print good_data.shape
# os._exit(-2)
obj_cube_1d = obj_cube_sorted
# obj_cube.copy().reshape((-1, obj_cube.shape[2]))[wl_sort]
good_sky_data_1d = good_sky_data_sorted # good_sky_data.reshape((-1,1))[wl_sort]
# print
n_noise_blocks = math.ceil(obj_cube_1d.shape[0] / float(noiseblocksize))
n_to_add = int(n_noise_blocks*noiseblocksize-obj_cube_1d.shape[0])
# print n_to_add
n_add_front = int(n_to_add/2)
n_add_back = int(n_to_add - n_add_front)
pad_width = ((int(n_add_front),int(n_add_back)),(0,0))
# print pad_width, obj_cube_1d.shape
prep4noise = numpy.pad(
obj_cube_1d,
pad_width=pad_width,
mode='constant',
constant_values=(numpy.NaN,),
)
prep4noise_flag = numpy.pad(
good_sky_data_1d,
pad_width=pad_width,
mode='constant', constant_values=(False,)
)
# print "prep4noise:", prep4noise.shape, prep4noise_flag.shape
prep4noise_reshape = numpy.reshape(prep4noise,
(-1, noiseblocksize, obj_cube_1d.shape[1]))
# print prep4noise.shape, prep4noise_reshape.shape
prep4noise_reshape_flag = numpy.reshape(prep4noise_flag,
(-1, noiseblocksize, prep4noise_flag.shape[1]))
# print "prepnoise flags/flux:", prep4noise_reshape_flag.shape, \
# prep4noise_reshape.shape
for i in range(3):
logger.debug("starting iteration %d on binned data" % (i+1))
noise_stats = numpy.nanpercentile(
prep4noise_reshape[:,:,1], [16,50,84], axis=1
)
binned_wl = numpy.median(prep4noise_reshape[:,:,0], axis=1)
# print noise_stats.shape
binned_one_sigma = 0.5*(noise_stats[2, :] - noise_stats[0, :])
binned_median = noise_stats[1, :]
# print binned_median.shape, binned_one_sigma.shape
shape_1d = (binned_one_sigma.shape[0], 1)
_good_max = (binned_median + 3 * binned_one_sigma)
_good_min = (binned_median - 3 * binned_one_sigma)
bad_data = (prep4noise_reshape[:,:,1] >
_good_max.reshape(shape_1d)) | \
(prep4noise_reshape[:,:,1]
< _good_min.reshape(shape_1d))
# print "bad data:", i, bad_data.shape
prep4noise_reshape[bad_data,1] = numpy.NaN
prep4noise_reshape_flag[bad_data] = False
# print noise_stats.shape
if (not debug):
logger.debug("done computing noise spectrum")
else:
logger.debug("done computing noise spectrum, saving debug")
debug_fn = "noisespec_%d.%d" % (iteration+1,i+1)
numpy.savetxt(debug_fn,
numpy.array([binned_wl,
binned_one_sigma,
binned_median,
]).T)
# numpy.savetxt("noisedata_%d.%d" % (iteration+1,i+1),
# prep4noise_reshape.reshape(
# (-1, prep4noise_reshape.shape[2]))
# )
logger.info("done writing debug %s" % (debug_fn))
#
# Re-convert the 3-d array to 2-d
#
unprep = prep4noise_reshape.reshape(
(-1, prep4noise_reshape.shape[2]))[
n_add_front:-n_add_back] #[wl_unsort].reshape(obj_cube.shape)
unprep_mask = prep4noise_reshape_flag.reshape((-1,1))[n_add_front:-n_add_back] #[wl_unsort].reshape(
#good_sky_data.shape)
# print unprep.shape, unprep_mask.shape, obj_cube.shape, good_sky_data.shape
obj_cube_1d = unprep
good_sky_data_sorted = unprep_mask
fits.PrimaryHDU(
data=good_sky_data_sorted[wl_unsort].reshape(obj_data.shape).astype(numpy.int)).writeto(
"unprepped_mask.fits", clobber=True
)
# print "Un-prepped:", obj_cube_1d.shape, good_sky_data_sorted.shape
#fits.PrimaryHDU(data=good_sky_data.astype(numpy.int)).writeto(
# "mask_%d.fits" % (iteration), clobber=True)
if (not strong_gradient_basepoints_added):
logger.info("Searching for the edges of sky-lines")
data = numpy.array([binned_wl,
binned_one_sigma,
binned_median,
]).T
basepoints_to_add = quickwlmodel.find_additional_basepoints(
data=data,
)
logger.info("Adding %d spline basepoints in places of strong "
"flux-gradients" % (basepoints_to_add.shape[0]))
k_wl = numpy.sort(numpy.append(k_wl, basepoints_to_add))
strong_gradient_basepoints_added = True
#
# Select only data points not previously masked out
#
good_data = obj_cube_sorted[good_sky_data_sorted[:,0]]
logger.debug("Dumping debug output")
if (debug):
numpy.savetxt("xxx.allskies", obj_cube_sorted)
numpy.savetxt("xxx.allskies.good", good_data)
# print good_data.shape
logger.debug("Ensuring Schoenberg/Whitney is satisfied")
k_iter_good = satisfy_schoenberg_whitney(
good_data[:,0],
k_wl, k=3)
logger.info("Computing spline, measuring noise and rejecting "
"outliers ...")
logger.info("Computing spline ...")
try:
# spline_iter = scipy.interpolate.LSQUnivariateSpline(
# x=good_data[:,0], #allskies[:,0],#[good_point],
# y=good_data[:,1], #allskies[:,1],#[good_point],
# t=k_iter_good, #k_wl,
# w=None, # no weights (for now)
# bbox=[wl_min, wl_max],
# k=3, # use a cubic spline fit
# )
# print obj_cube_sorted.shape
spline_iter = scipy.interpolate.LSQUnivariateSpline(
x=good_data[:,0], #allskies[:,0],# #[good_point],
y=good_data[:,1], #allskies[:,1],
# #[good_point],
t=k_iter_good, #k_wl,
w=None, # no weights (for now)
bbox=[wl_min, wl_max],
k=3, # use a cubic spline fit
)
# spline_pickle_test.write_pickle(debug_prefix+"splinein",
# fct=scipy.interpolate.LSQUnivariateSpline,
# x=good_data[:,0], #allskies[:,0],#[good_point],
# y=good_data[:,1], #allskies[:,1],#[good_point],
# t=k_iter_good, #k_wl,
# w=None, # no weights (for now)
# bbox=[wl_min, wl_max],
# k=3,
# )
except ValueError as e:
# this is most likely
# ValueError: Interior knots t must satisfy Schoenberg-Whitney conditions
# print e
if (iteration > 100):
break
else:
logger.warning("unable to compute LSQ spline, skipping 80% of basepoints")
spline_iter = scipy.interpolate.LSQUnivariateSpline(
x=good_data[:,0], #allskies[:,0],#[good_point],
y=good_data[:,1], #allskies[:,1],#[good_point],
t=k_iter_good[5:-5][::5], #k_wl,
w=None, # no weights (for now)
bbox=[wl_min, wl_max],
k=3, # use a cubic spline fit
)
#print("Critical error!")
#os._exit(0)
if (lots_of_debug):
logger.debug("Saving debug data for this iteration")
numpy.savetxt(debug_prefix+"spline_opt.iter%d" % (iteration+1),
numpy.append(k_wl.reshape((-1,1)),
spline_iter(k_wl).reshape((-1,1)),
axis=1)
)
# compute spline fit for each wavelength data point
# dflux = good_data[:,1] - spline_iter(good_data[:,0])
logger.debug("computing residuals for outlier rejection")
modelflux = spline_iter(obj_cube_sorted[:,0])
dflux = obj_cube_sorted[:,1] - modelflux
if (lots_of_debug):
logger.debug("writing dflux.fits")
fits.PrimaryHDU(data=dflux).writeto(
"dflux_%d.fits" % (iteration+1),
clobber=True
)
logger.debug("dflux shape: %s" % (str(dflux.shape)))
# there's only 1 iteration, no need to prep for #2
# break
# print dflux
#
# Add here: work out the scatter of the distribution of pixels in the
# vicinity of this basepoint. This is what determined outlier
# or not, and NOT the uncertainty in a given pixel
#
keep_iterating = True
if (noise_mode == 'local1'):
local_noise = localnoise.calculate_local_noise(
data=dflux[good_sky_data_sorted],
basepoints=k_iter_good,
select=good_data[:,0],
dumpdebug=True
)
var = local_noise[:, 0]
# print var.shape
# subset_selector = good_data.shape[0] / (100*k_wl.shape[0])
# subset = good_data[::subset_selector]
# logger.debug("Searching for points close to each basepoint")
#
# # Create a KD-tree with all data points
# wl_tree = scipy.spatial.cKDTree(subset[:,0].reshape((-1,1)))
#
# # Now search this tree for points near each of the spline base points
# d, i = wl_tree.query(k_wl.reshape((-1,1)),
# k=100, # use 100 neighbors
# distance_upper_bound=avg_sample_width)
#
# # make sure to flag outliers
# bad = (i >= dflux.shape[0])
# i[bad] = 0
#
# # Now we have all indices of a bunch of nearby datapoints, so we can
# # extract how far off each of the data points is
# delta_flux_2d = dflux[i]
# delta_flux_2d[bad] = numpy.NaN
# #print "dflux_2d = ", delta_flux_2d.shape
#
# # With this we can estimate the scatter around each spline fit basepoint
# logger.debug("Estimating local scatter")
# var = bottleneck.nanstd(delta_flux_2d, axis=1)
#print "variance:", var.shape
if (lots_of_debug):
numpy.savetxt(debug_prefix+"fit_variance.iter_%d" % (iteration+1),
numpy.append(k_iter_good.reshape((-1,1)),
var.reshape((-1,1)), axis=1))
#
# Now interpolate this scatter linearly to the position of each
# datapoint in the original dataset. That way we can easily decide,
# for each individual pixel, if that pixel is to be considered an
# outlier or not.
#
# Note: Need to consider ALL pixels here, not just the good ones
# selected above
#
std_interpol = scipy.interpolate.interp1d(
x = k_iter_good,
y = var,
kind = 'linear',
fill_value=1e3,
bounds_error=False,
#assume_sorted=True
)
# var_at_pixel = std_interpol(good_data[:,0])
var_at_pixel = std_interpol(obj_cube[:,:,0])
logger.debug("Marking outliers for rejection in next iteration")
if (lots_of_debug):
numpy.savetxt(debug_prefix+"pixelvar.%d" % (iteration+1),
numpy.append(obj_cube[:,:,0].reshape((-1,1)),
var_at_pixel.reshape((-1,1)), axis=1))
# Now mark all pixels exceeding the noise threshold as outliers
not_outlier = numpy.fabs(dflux) < 3*var_at_pixel
outlier = numpy.fabs(dflux) > 3*var_at_pixel
elif (noise_mode == 'global'):
#
# Assume a global noise level everywhere