-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhidden_transform.py
160 lines (126 loc) · 6.03 KB
/
hidden_transform.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
import logging
from cStringIO import StringIO
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from imageflow.s3_util import upload_to_s3
from imageflow.models import ImageAnalysisPair
from reduction.util import (average_instrumental_mags_by_desig,
find_star_by_designation,
find_point_by_id)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def run(lightcurve, reduction):
logger.info('Finding hidden transform for lightcurve %d' % lightcurve.id)
if lightcurve.ciband == 'NONE':
reduction.hidden_transform = 0
reduction.hidden_transform_intercept = 0
reduction.hidden_transform_std = 0
reduction.hidden_transform_rval = 0
reduction.hidden_transform_graph_url = None
reduction.save()
return
ci1 = lightcurve.get_ci_band1()
ci2 = lightcurve.get_ci_band2()
# Select analyses that match the lightcurve filter.
image_pairs = ImageAnalysisPair.objects.filter(lightcurve=lightcurve)
analyses_band1 = [pair.analysis1 for pair in image_pairs]
analyses_band2 = [pair.analysis2 for pair in image_pairs]
# TODO(ian): Assert that these analyses are equal to lightcurve ci1, ci2
# respectively.
# Take the average instrumental magnitudes for comparison stars across
# these analyses.
comparison_desigs = lightcurve.get_comparison_desigs()
desig_map_band1 = average_instrumental_mags_by_desig(analyses_band1,
comparison_desigs)
desig_map_band2 = average_instrumental_mags_by_desig(analyses_band2,
comparison_desigs)
stars_band1 = desig_map_band1.values()
stars_band2 = desig_map_band2.values()
star_pairs = zip(stars_band1, stars_band2)
ht, ht_intercept, ht_std, ht_r, ht_url = \
calculate_hidden_transform(lightcurve, star_pairs, save_graph=True)
reduction.hidden_transform = ht
reduction.hidden_transform_intercept = ht_intercept
reduction.hidden_transform_std = ht_std
reduction.hidden_transform_rval = ht_r
reduction.hidden_transform_graph_url = ht_url
calculate_color_index(lightcurve, reduction, image_pairs)
#annotate_color_index(analysis, reduction)
def calculate_hidden_transform(lightcurve, star_pairs, save_graph=False):
# Get the URAT1 keys for each filter and CI band. eg. 'Bmag', 'jmag'
ci1 = lightcurve.get_ci_band1()
ci2 = lightcurve.get_ci_band2()
ci1_key = ci1.urat1_key
ci2_key = ci2.urat1_key
standard_diffs = []
instrumental_diffs = []
for i in xrange(len(star_pairs)):
star1, star2 = star_pairs[i]
if star1['designation'] != star2['designation']:
logger.error('Star1 designation does not match Star2 designation')
logger.error('%s vs %s)' % (star1, star2))
raise RuntimeError('Mismatched star designations')
standard_diffs.append(star1[ci1_key] - star1[ci2_key])
instrumental_diffs.append(star1['mag_instrumental'] - star2['mag_instrumental'])
xs = np.array(instrumental_diffs)
ys = np.array(standard_diffs)
slope, intercept, r_value, p_value, std_err = stats.linregress(xs, ys)
graph_url = None
if save_graph:
band1 = ci1.band.upper()
band2 = ci2.band.upper()
# Clear any existing state
plt.clf()
plt.cla()
plt.close()
plt.title(r'$%s-%s = %f(%s-%s) + %f\ \ \ R=%f$' % \
(band1, band2, slope, band1.lower(), band2.lower(), intercept, r_value))
plt.plot(xs, ys, '+', label='Original data', markersize=10)
plt.plot(xs, slope * xs + intercept, 'r', label='Fitted line')
plt.xlabel('%s-%s (instrumental)' % (band1.lower(), band2.lower()))
plt.ylabel('%s-%s (catalog)' % (band1, band2))
img_graph = StringIO()
plt.savefig(img_graph)
graph_url = upload_graph(lightcurve, img_graph.getvalue())
logger.info(' -> Uploaded to %s' % graph_url)
return slope, intercept, std_err, r_value, graph_url
def upload_graph(lightcurve, img_graph):
logger.info('-> Uploading hidden transform graph for lightcurve %d' % (lightcurve.id))
upload_key_prefix = 'processed/lightcurve/%d' % (lightcurve.id)
name = 'hidden_transform_graph.jpg'
logger.info(' -> Uploading %s...' % name)
return upload_to_s3(img_graph, upload_key_prefix, name)
def calculate_color_index(lightcurve, reduction, image_pairs):
if reduction.color_index_manual is not None:
# User has chosen color index.
reduction.color_index = reduction.color_index_manual
reduction.save()
return
ci1 = lightcurve.get_ci_band1()
ci2 = lightcurve.get_ci_band2()
ci1_key = ci1.urat1_key
ci2_key = ci2.urat1_key
#cis = defaultdict(list)
target_cis = []
for image_pair in image_pairs:
analysis1 = image_pair.analysis1
analysis2 = image_pair.analysis2
'''
for comp_desig in lightcurve.comparison_star_designations:
star1 = find_star_by_designation(analysis1.annotated_point_sources)
star2 = find_star_by_designation(analysis2.annotated_point_sources)
ci = star1['mag_instrumental'] - star2['mag_instrumental']
ci_transformed = reduction.hidden_transform * ci + reduction.hidden_transform_intercept
cis[comp_desig].append(ci_transformed)
'''
target1 = find_point_by_id(analysis1.annotated_point_sources, analysis1.target_id)
target2 = find_point_by_id(analysis2.annotated_point_sources, analysis2.target_id)
ci = target1['mag_instrumental'] - target2['mag_instrumental']
ci_transformed = reduction.hidden_transform * ci + reduction.hidden_transform_intercept
target_cis.append(ci_transformed)
#color_index_by_desig = {}
# TODO(ian): Show comparison standard color index vs computed color index
# TODO(ian): Produce graph... (see pg 122)
reduction.color_index = np.mean(target_cis)
reduction.save()