-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClassifierFunctions.py
More file actions
245 lines (173 loc) · 6.6 KB
/
ClassifierFunctions.py
File metadata and controls
245 lines (173 loc) · 6.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
import csv
import os
import numpy as np
from matplotlib import pyplot as plt
from future.builtins.misc import input
def validate_calibration(prompt,name):
"""
ensures proper options are chosen for calibration
"""
while True:
temp_value = input(prompt)
try:
float(temp_value)
if temp_value > 0:
return float(temp_value)
else:
print("invalid {}".format(name))
except:
print("invalid {}".format(name))
def validate_profile_choice(dims):
"""
ensure proper options are chosen for selecting a single profile
"""
if dims[0] > 1:
profile_choice = int(input("Multiple profiles detected.\nplease choose which profile to use.\n"))
while profile_choice not in range(dims[0]):
profile_choice = int(input("Incorrect selection.\nplease choose {}.\n".format(range(dims[0]))))
else:
profile_choice = 0
return profile_choice
def set_calibration(is_profile):
"""
prompt user to fill in proper calibration parameters
"""
print("No calibration could be extracted from the file metadata\n Please enter calibration paramters")
pixel_size = validate_calibration("Please enter a pixel size (in angstroms) e.g. 14.1\n","pixel_size")
wavelength = validate_calibration("Please enter beam wavelength (in microns) e.g. .02354\n","wavelength")
if not is_profile:
camera_dist = validate_calibration("Please enter camera distance from sample (in millimeters) e.g. 223.1\n","camera distance")
else:
camera_dist = 0
return { "pixel_size": pixel_size,
"wavelength": wavelength,
"camera_distance": camera_dist}
def choose_profile(image_data):
"""
prompts user to select a profile if multiple are detected
"""
print("The data is a profile.")
if len(image_data.shape) == 1:
plt.plot(image_data)
plt.show(block=False)
return image_data,np.array(range(image_data.shape[0]))
elif len(image_data.shape) == 2:
if image_data.shape[0] == 1:
plt.plot(image_data[0,:])
plt.show(block=False)
#plt.show()
return image_data[0,:],np.array(range(image_data.shape[1]))
elif image_data.shape[1] == 1:
plt.plot(image_data[1,:])
plt.show(block=False)
#plt.show()
return image_data[0,:],np.array(range(image_data.shape[1]))
else:
""
return image_data[1,:],image_data[0,:]
else:
# show the user which profiles are present
for i in range(image_data.shape[0]):
plt.plot(image_data[i,:], label="profile {}".format(i))
plt.legend()
plt.show(block=False)
# have the user select the profile
profile_choice = validate_profile_choice(image_data.shape)
plt.close()
image_data = image_data[profile_choice]
# show chosen profile
plt.plot(image_data)
plt.show(block=False)
return image_data
def choose_display():
"""
prompt user to choose scale bar display
"""
choices = ["d","theta","both"]
temp_choice = "false"
while temp_choice not in choices:
temp_choice = input("Please choose the scale to display.\nd, theta, both\n")
if temp_choice not in choices:
print("incorrect choice\n")
return temp_choice
def choose_peaks(peak_locs,display_type):
"""
prompt user to select which peaks to classify on
"""
d = peak_locs["d_spacing"]
theta = peak_locs["2theta"]
vec = peak_locs["vec"]
if display_type == "d" or display_type =="both":
print(d)
if display_type == "theta" or display_type =="both":
print(theta)
maximum = min(len(d),len(theta))
print(maximum,len(d),len(theta))
raw_choices = input("Choose which peaks you'd like to select separated by spaces.\n").split(" ")
temp_choices = []
for choice in raw_choices:
try:
temp_index = int(choice)
if temp_index > 0 and temp_index <= maximum and temp_index not in temp_choices:
temp_choices.append(temp_index)
else:
print("index {} outside of available peaks".format(temp_index))
except:
print("couldn't convert {} into an index".format(choice))
print(temp_choices)
temp_locs = {
"d_spacing":[d[i-1] for i in temp_choices],
"2theta":[theta[i-1] for i in temp_choices],
"vec":[vec[i-1] for i in temp_choices]
}
return temp_locs
def provide_family():
"""
prompt user and ensure proper selection of base Crystal family
"""
family = None
while family is None:
temp_choice = input("Would you like to suggest a crystal family? yes or no\n")
if temp_choice =="yes":
family = temp_choice
elif temp_choice =="no":
family = temp_choice
else:
print("Invalid choice. Please choose yes or no\n")
return family
def write_to_csv(path,data_dict):
"""
save new row of results to csv
"""
schema = ["file_name","family","genus","genus_confidence",
"species_1","confidence_1","hall_1",
"species_2","confidence_2","hall_2",
"species_3","confidence_3","hall_3",
"species_4","confidence_4","hall_4","peaks"]
# if no file exists create a one and inform the user
if not os.path.exists(path):
print("creating new output file {}".format(path))
with open(path, "w") as csv_file:
filewriter = csv.writer(csv_file, delimiter=",")
filewriter.writerow(schema)
row = []
row.append(data_dict["file_name"])
row.append(data_dict["family"])
row.append(data_dict["genus_1"])
row.append(data_dict["genus_confidence_1"][:5])
row.append(data_dict["species_1"])
row.append(data_dict["confidence_1"][:5])
row.append(data_dict["hall_1"])
row.append(data_dict["species_2"])
row.append(data_dict["confidence_2"][:5])
row.append(data_dict["hall_2"])
row.append(data_dict["species_3"])
row.append(data_dict["confidence_3"][:5])
row.append(data_dict["hall_3"])
row.append(data_dict["species_4"])
row.append(data_dict["confidence_4"][:5])
row.append(data_dict["hall_4"])
row.append(data_dict["peaks"])
with open(path, "a") as csv_file:
filewriter = csv.writer(csv_file, delimiter=",")
filewriter.writerow(row)