This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fom.py
194 lines (147 loc) · 7.43 KB
/
fom.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
#!/usr/bin/env python3.7
# -*- coding: UTF-8 -*-
"""The ``fom`` module calculates the Figure of Merit (FOM) parameter
defined as:
.. math::
FOM = \\frac{N_{true}}{N_{total}} \\times \\frac{N_{true}}{N_{true} + N_{false}}
where :math:`N_{true}` is the number of objects correctly classified as a given
type, :math:`N_{false}` is the number of objects falsely classified as the
given type, and :math:`N_{total}` is the total number of objects with that
type.
This module specifically focuses on classification techniques that classify
targets based on their (x, y) position in some two dimensional phase space.
Functions are provided for calculating the FOM according to multiple
classification schemes:
+----------------------+------------------------------------------------------+
| Function | Classification Scheme |
+======================+======================================================+
| ``rectangular`` | Uses a pair of rectangular axes to classify targets |
| | in the upper right quadrant as a given type. |
+----------------------+------------------------------------------------------+
| ``vertical`` | Classifies targets with an x coordinate greater than |
| | some value as a given type. |
+----------------------+------------------------------------------------------+
| ``horizontal`` | Classifies targets with a y coordinate greater than |
| | some value as a given type. |
+----------------------+------------------------------------------------------+
| ``linear`` | Classifies targets above a linear boundary |
| | y = mx + b as a given type. |
+----------------------+------------------------------------------------------+
| ``diagonal`` | Classifies targets above a diagonal boundary |
| | y = x + b as a given type. Note the difference in |
| | slope between this option and the ``linear`` option. |
+----------------------+------------------------------------------------------+
Alternatively, the ``fom`` method can be used to calculate the FOM for a set
of predetermined classifications, independent of the chosen classification
technique (i.e. using the results of an already run classification).
Usage Example
-------------
>>> from phot_class.fom import fom
>>>
>>> # The true "spectroscopic" classification of an object
>>> truth = ['normal', 'normal', 'normal', '91bg', '91bg']
>>>
>>> # The result of some classification piepline
>>> classifications = ['normal', 'normal', '91bg', 'normal', '91bg']
>>>
>>> # Classify the FOM for the "91bg" type
>>> bg_fom = fom(truth, classifications, '91bg')
>>>
>>> # Classify the FOM for the "normal" type
>>> normal_fom = fom(truth, classifications, 'normal')
Function Documentation
----------------------
"""
import numpy as np
def fom(truth, classification, check_type):
"""Calculate the figure of merit for a given set of classifications
Args:
truth (ndarray): The true classifications
classification (ndarray): The assigned classifications
check_type (str): The classification to calculate the FOM for
Returns:
(n_true / n_total_type) * (n_true / (n_true + n_false))
"""
truth = np.asarray(truth)
classification = np.asarray(classification)
# The total number of objects of the given type
ntotal = sum(truth == check_type)
# The number of objects correctly classified as the given type
ntrue = sum((truth == classification) & (classification == check_type))
# The number of objects incorrectly classified as the given type
nfalse = sum((truth != classification) & (classification == check_type))
return (ntrue / ntotal) * (ntrue / (ntrue + nfalse))
def rectangular(truth, x, y, x_cutoff, y_cutoff, check_type):
"""Calculate the figure of merit using a pair of rectangular lower bounds
Args:
truth (ndarray): Array of classifications (str) to take as truth
x (ndarray): The classification x coordinate
y (ndarray): The classification y coordinate
x_cutoff (float): The x boundary to use for separating 91bg / normal
y_cutoff (float): The y boundary to use for separating 91bg / normal
check_type (str): The classification to calculate the FOM for
Returns:
The figure of merit value
"""
classification = np.full_like(truth, f'not_{check_type}')
classification[(x > x_cutoff) & (y > y_cutoff)] = check_type
return fom(truth, classification, check_type)
def horizontal(truth, x, x_cutoff, check_type):
"""Calculate the figure of merit using a vertical lower boundary
(i.e. using only the x coordinate)
Args:
truth (ndarray): Array of classifications to take as truth
x (ndarray): The classification x coordinate
x_cutoff (float): The x boundary to use for separating 91bg / normal
check_type (str): The classification to calculate the FOM for
Returns:
The figure of merit value
"""
classification = np.full_like(truth, f'not_{check_type}')
classification[x > x_cutoff] = check_type
return fom(truth, classification, check_type)
def vertical(truth, y, y_cutoff, check_type):
"""Calculate the figure of merit using a horizontal lower boundary
(i.e. using only the y coordinate)
Expected classifications are "normal" or "91bg" (case insensitive).
All other classifications are ignored.
Args:
truth (ndarray): Array of classifications to take as truth
y (ndarray): The classification y coordinate
y_cutoff (float): The y boundary to use for separating 91bg / normal
check_type (str): The classification to calculate the FOM for
Returns:
The figure of merit value
"""
classification = np.full_like(truth, f'not_{check_type}')
classification[y > y_cutoff] = check_type
return fom(truth, classification, check_type)
def linear(truth, x, y, m, b, check_type):
"""Calculate the figure of merit using a diagonal lower bound: y = mx + b
Args:
truth (ndarray): Array of classifications to take as truth
x (ndarray): The classification x coordinate
y (ndarray): The classification y coordinate
m (float): The slope of the boundary
b (float): The y-intercept of the boundary
check_type (str): The classification to calculate the FOM for
Returns:
The figure of merit value
"""
classification = np.full_like(truth, f'not_{check_type}')
classification[y > (m * x + b)] = check_type
return fom(truth, classification, check_type)
def diagonal(truth, x, y, b, check_type):
"""Calculate the figure of merit using a diagonal lower bound: y = -x + b
Expected classifications are "normal" or "91bg" (case insensitive).
All other classifications are ignored.
Args:
truth (ndarray): Array of classifications to take as truth
x (ndarray): The classification x coordinate
y (ndarray): The classification y coordinate
b (float): The y-intercept of the boundary
check_type (str): The classification to calculate the FOM for
Returns:
The figure of merit value
"""
return linear(truth, x, y, m=-1, b=b, check_type=check_type)