-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (103 loc) · 3.52 KB
/
main.py
File metadata and controls
133 lines (103 loc) · 3.52 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
import os
import re
os.environ['MPLCONFIGDIR'] = os.getcwd() + "/configs/"
import matplotlib.pyplot as plt
from matplotlib.ticker import (MultipleLocator, FixedLocator)
import numpy as np
def main() -> None:
noise = read_noise()
products = read_products()
draw_graph(noise, products)
def draw_graph(noise, products):
nrows = len(noise) + len(products)
# Init graph figure
figure, axes = plt.subplots(nrows=nrows, ncols=1, figsize=[8, 12], dpi=300)
figure.suptitle('XRD NAME', fontsize=20)
#figure.tight_layout()
plt.figtext(0.05, 0.65, "Intensity (a.u.)", fontsize=16,
rotation='vertical', horizontalalignment='left')
plt.figtext(0.5, 0.05, "2theta (degree)", fontsize=16,
horizontalalignment='center',
verticalalignment='center')
# Remove Y gaps in between subplots
plt.subplots_adjust(hspace=0)
# Plot counter
pc = 0
# draw noise plots
for data in noise:
x = data[0]
y = data[1]
axes[pc].plot(x, y, color='black', label=f"Label-{pc}")
axes[pc].legend()
# Set range of X and Y axis
axes[pc].set_xlim(xmin=10, xmax=60)
axes[pc].set_ylim(ymin=0, ymax=400)
# # Set tick labels to nothing
axes[pc].tick_params(labelbottom=False, labelleft=False)
# Set minor and major X axis ticks
axes[pc].xaxis.set_minor_locator(MultipleLocator(2.5))
axes[pc].xaxis.set_major_locator(MultipleLocator(5))
# Set minor and major Y axis ticks
axes[pc].yaxis.set_minor_locator(MultipleLocator(50))
axes[pc].yaxis.set_major_locator(MultipleLocator(100))
# Change direction of all ticks
axes[pc].tick_params(which='both', direction='in', bottom=True, right=True, left=True, top=(pc == 0))
pc += 1
# draw product plots
for data in products:
axes[pc].bar(data[0], data[1], width=0.2, label=f"Label-{pc}")
axes[pc].legend()
axes[pc].sharex(axes[1])
axes[pc].set_ylim(ymin=0, ymax=1100)
# Set minor and major Y axis ticks
axes[pc].yaxis.set_minor_locator(MultipleLocator(150))
axes[pc].yaxis.set_major_locator(MultipleLocator(300))
axes[pc].tick_params(which='both', direction='in', bottom=True, right=True, left=True, top=False, labelsize=12)
axes[pc].tick_params(labelbottom=(pc == nrows - 1), labelleft=False)
pc += 1
plt.savefig(os.getcwd() + "/output/xrd_graph")
def read_noise():
noise = []
noise_files = [filename for filename in os.listdir("./source/") if filename.endswith('.txt')]
for i, file in enumerate(noise_files):
with open("./source/" + file, 'r', newline='') as f:
reader = f.readlines()
x = []
y = []
is_data = False
for row in reader:
if is_data:
pair = [x[0] for x in re.finditer(r"(\d*\.?\d+)", row.strip())]
if pair:
try:
x.append(float(pair[0]))
y.append(float(pair[1]))
except:
raise SystemExit(f"One or both values in {pair} could not be converted to number.")
if "[Data]" in row:
is_data = True
noise.append([x, y])
return noise
def read_products():
products = []
product_files = [filename for filename in os.listdir("./source/") if '$A' in filename]
for i, file in enumerate(product_files):
with open("./source/" + file, 'r', newline='') as f:
reader = f.readlines()
data = {}
for row in reader:
pair = [x[0] for x in re.finditer(r"(\d*\.?\d+)", row.strip())]
if pair:
try:
x = float(pair[0])
y = float(pair[1])
except:
raise SystemExit(f"One or both values in {pair} could not be converted to number.")
if not x in data:
data[x] = y
x = list(data.keys())
y = list(data.values())
products.append([x, y])
return products
if __name__ == "__main__":
main()