-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDiffractionClassifier2.0.py
180 lines (131 loc) · 5.15 KB
/
DiffractionClassifier2.0.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
import ClientSide2 #custom package
import logging
import numpy as np
import argparse
import json
import os
import ClassifierFunctions2 as cf
from matplotlib import pyplot as plt
from builtins import input
# Initialize essential global variables
#URL = "" #you'll need me to send you the link
FAMILIES = ["triclinic","monoclinic","orthorhombic","tetragonal",
"trigonal","hexagonal","cubic"]
DEFAULT_SESSION = os.path.join ("Sessions","session.json")
DEFAULT_USER = "user_profile.json"
SERVER_INFO = "server_gen2.json"
# list of three, one per level
prediction_per_level = [2, 3, 3]
DEFAULT_FILTER_SETTINGS = { "max_numpeaks": 20,
"dspace_range" : [0.1,6],
"peak_threshold": 1,
"filter_size" : 15,
"passes" : 2
}
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
# print(session)
file_path = session["file_path"]
output_file = session["output_file"]
manual_peak_selection = session["manual_peak_selection"]
known_family = session["known_family"]
chemistry = session["chemistry"]
diffraction = session["diffraction"]
mode = ""
if diffraction:
if chemistry:
mode="DiffChem"
else:
mode="DiffOnly"
else:
if chemistry:
raise ValueError('Running chemistry only predictions is currently not implemented')
else:
raise ValueError('Invalid prediction type. Either diffraction or chemistry must be enabled')
if known_family and known_family=='yes':
print('known family')
crystal_family = session["crystal_family"]
prediction_per_level[0] = 1
else:
crystal_family = None
if session["filter_settings"]:
FILTER_SETTINGS = session["filter_settings"]
else:
FILTER_SETTINGS = {}
print(FILTER_SETTINGS)
# Load user from provided path, [IN PROGRESS]
if session["user_info"]:
with open(session["user_info"],'r') as f:
user_info = json.load(f)
else:
with open(DEFAULT_USER,'r') as f:
user_info = json.load(f)
with open(session["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')
chem_vec = cf.check_for_chemistry(session)
# 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:
if not path[0] == '.':
file_paths.append(os.path.join(dirpath,path))
print("found {} files to load.".format(len(file_paths)))
else:
file_paths = [file_path]
for f_path in file_paths:
# Load Data from specified file (DM3, TIFF, CSV etc....)
print("loading data from {}".format(f_path))
image_data,scale = ClientSide2.Load_Profile(f_path)
print("I successfully loaded the data")
# print(scale)
print("length",len(image_data))
print("max",np.max(image_data))
if diffraction:
peak_locs,peaks_h = ClientSide2.Find_Peaks(image_data,scale, **FILTER_SETTINGS)
# Choose which peaks to classify on
if manual_peak_selection:
peak_locs = cf.choose_peaks(peak_locs,peaks_h)
#raise NotImplementedError
else:
peak_locs = []
peaks_h = []
#
# print(peak_locs)
# print(chem_vec)
classificated = ClientSide2.Send_For_Classification(peak_locs, chem_vec, mode, crystal_family, user_info, url, prediction_per_level)
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, prediction_per_level)
if __name__ == "__main__":
main()