-
Notifications
You must be signed in to change notification settings - Fork 4
/
DiffractionClassifier.py
171 lines (123 loc) · 5.23 KB
/
DiffractionClassifier.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
import ClientSide #custom package
import numpy as np
import argparse
import json
import os
import ClassifierFunctions as cf
from matplotlib import pyplot as plt
from builtins import input
# Initialize essential global variables
USER_INFO = "user_profile.json"
#URL = ""#you'll need me to send you the link
SERVER_INFO = "server_gen1.json"
FAMILIES = ["triclinic","monoclinic","orthorhombic","tetragonal",
"trigonal","hexagonal","cubic"]
DEFAULT_SESSION = os.path.join ("Sessions","session.json")
def build_parser():
parser = argparse.ArgumentParser()
# This will be implemented as rollout broadens
parser.add_argument('--apikey', type=str,
dest='key', help='api key to securely access service',
metavar='KEY', required=False)
parser.add_argument('--session',
dest='session',help='Keep user preferences for multirun sessions',
metavar='SESSION',required=False, default=None)
return parser
def main():
parser = build_parser()
options = parser.parse_args()
print(options.session)
# opens the user specified session
if options.session:
with open(os.path.join("Sessions",options.session),'r') as f:
session = json.load(f)
# opens the default session
else:
with open(DEFAULT_SESSION,'r') as f:
session = json.load(f)
# set variables from loaded session data
fam = session["crystal_family"]
provide_family = session["known_family"]
display_type = session["display_type"]
auto_calibrate = session["auto_calibrate"]
file_path = session["file_path"]
output_file = session["output_file"]
is_profile = session["is_profile"]
manual_peak_selection = session["manual_peak_selection"]
scale_bar = session["scale_bar"]
# Load calibration from specified file (json)
try:
print("Loading calibration from {}".format(os.path.join("Calibrations",auto_calibrate)))
with open(os.path.join("Calibrations",auto_calibrate),'r') as f:
calibration = json.load(f)
except:
print("No calibration could be loaded from {}\nPlease check the file exists and is formatted properly".format(auto_calibrate))
calibration = cf.set_calibration(is_profile)
print(calibration)
with open(SERVER_INFO,'r') as f:
server_info = json.load(f)
if server_info['URL']:
URL = server_info['URL']
else:
raise ValueError('you need to have the server URL provided to you')
# Load user from provided path, [IN PROGRESS]
with open(USER_INFO) as f:
user_info = json.load(f)
# Determine if the path is a directory or a file
if os.path.isdir(file_path):
print("loading files from directory")
file_paths = []
for dirpath,dirnames,fpath in os.walk(file_path):
for path in fpath:
file_paths.append(os.path.join(dirpath,path))
print("found {} files to load.".format(len(file_paths)))
else:
file_paths = [file_path]
print(file_paths)
for f_path in file_paths:
# Load Data from specified file (DM3, TIFF, CSV etc....)
try:
print("loading data from {}".format(f_path))
image_data = ClientSide.Load_Image(f_path)
print("I successfully loaded the data")
except:
print("Invalid file path given ({}).\n Please enter filepath to your data".format(f_path))
options.fpath = input()
# Change the processing based on data type
if is_profile:
# Choose which profile if there are multiple
image_data,scale = cf.choose_profile(image_data)
else:
plt.imshow(np.log(image_data))
plt.show(block=False)
#plt.show()
# Change the Processing based on the type of data
if is_profile:
print("identifying from profile")
radial_profile = {"brightness":image_data,
"pixel_range":scale}
else:
radial_profile = ClientSide.Extract_Profile(image_data)
peak_locs = ClientSide.Find_Peaks(radial_profile,calibration,is_profile,display_type,scale_bar)
# Choose which peaks to classify on
if manual_peak_selection:
peak_locs = cf.choose_peaks(peak_locs,display_type)
if provide_family =="yes":
while fam is None:
temp_fam = input("What family does the Crystal belong to?\n")
if temp_fam in FAMILIES:
fam = temp_fam
else:
print("Invalid choice. choose from {}\n".format(str(FAMILIES)[1:-1]))
elif provide_family =="no":
fam = None
classificated = ClientSide.Send_For_Classification(peak_locs,user_info,URL,fam)
classificated["file_name"] = f_path
# update the user on the results before saving
print(classificated)
# write results out to the specified file
if not os.path.exists("Results"):
os.makedirs("Results")
cf.write_to_csv(os.path.join("Results", output_file), classificated)
if __name__ == "__main__":
main()