-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcrete_reports.py
More file actions
218 lines (184 loc) · 7.49 KB
/
concrete_reports.py
File metadata and controls
218 lines (184 loc) · 7.49 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
"""
A demonstration of how to create an analysis tool for PDF concrete
test reports using the basics taught in Python for Structural Engineers
"""
__version__ = "0.1.0"
import pathlib
import tabula
import pandas as pd
import json
from typing import Optional
from rich.progress import track
import plotly.graph_objects as go
# def load_template_areas(filename: list[str | pathlib.Path]) -> list[list[float]]:
# """
# Reads the area extents in each of the template files in filenames.
# """
# areas = []
# with open(filename, 'r') as file:
# template_data = json.load(file)
# for template in template_data:
# areas.append([template['y1'], template['x1'], template['y2'], template['x2']])
# return areas
def read_pdf_data(filename: str | pathlib.Path, template_file: str | pathlib.Path) -> list[pd.DataFrame]:
"""
Returns multiple dataframes representing multiple area regions of table data
extracted from the concrete test report in 'filename'.
"""
df_list = tabula.read_pdf_with_template(
input_path = filename,
template_path = template_file,
pandas_options = {"header": None},
)
return df_list
def compile_reports(
reports_dir: str | pathlib.Path,
company: str,
templates_dir: Optional[pathlib.Path],
) -> pd.DataFrame:
"""
Returns a pd.DataFrame that represents the extracted data from the
PDFs contained in the directory, 'reports_dir'.
"""
reports_dir_path = pathlib.Path(reports_dir)
report_paths = list(reports_dir_path.glob("*.pdf"))
companies = {"abc_company": abc_reports}
df = pd.DataFrame()
for report_path in track(report_paths):
series = companies[company](report_path, templates_dir)
if series is None: continue
df = pd.concat([df, series], axis=1)
df = df.T
# df["Date Cast"] = pd.to_datetime(df['Date Cast'], format="%Y-%m-%d")
df = df.set_index("Date Cast")
df = df.sort_index()
for column_name in df.columns:
if "Sample" in column_name or "Expected" in column_name:
df[column_name] = df[column_name].astype(float)
return df
def plot(concrete_df: pd.DataFrame, plot_name: str = "") -> None:
"""
Plots the DataFrame, 'concrete_df' as a bar chart with dates on the
x-axis and strength on the y-axis as a stacked chart.
"""
expected = go.Bar(name="Expected (MPa)",
x=concrete_df.index,
y=concrete_df["Expected (MPa)"],
marker_color = 'rgba(255,0,0,1)',
hovertext = concrete_df["Expected (MPa)"].map(str) + "<br>" + concrete_df["Location"],
)
sample_a = go.Bar(name="Sample A",
x=concrete_df.index,
y=concrete_df["Sample A (MPa)"],
marker_color = 'rgba(0,0,0, 0.3)',
hovertext = concrete_df["Sample A (MPa)"].map(str) + " @ " + concrete_df["Sample A (Age)"].map(str) + " days",
hoverinfo = "text"
)
sample_b = go.Bar(name="Sample B",
x=concrete_df.index,
y=concrete_df["Sample B (MPa)"],
marker_color = 'rgba(0,0,0, 0.35)',
hovertext = concrete_df["Sample B (MPa)"].map(str) + " @ " + concrete_df["Sample B (Age)"].map(str)+ " days",
hoverinfo = "text"
)
sample_c = go.Bar(name="Sample C",
x=concrete_df.index,
y=concrete_df["Sample C (MPa)"],
marker_color = 'rgba(0, 0, 0, 0.40)',
hovertext = concrete_df["Sample C (MPa)"].map(str) + " @ " + concrete_df["Sample C (Age)"].map(str)+ " days",
hoverinfo = "text"
)
sample_d = go.Bar(name="Sample D",
x=concrete_df.index,
y=concrete_df["Sample D (MPa)"],
marker_color = 'rgba(0,0,0, 0.45)',
hovertext = concrete_df["Sample D (MPa)"].map(str) + " @ " + concrete_df["Sample D (Age)"].map(str)+ " days",
hoverinfo = "text"
)
plot_data = [expected, sample_a, sample_b, sample_c, sample_d]
fig = go.Figure(data=plot_data)
fig.update_layout(
barmode='overlay',
bargroupgap = 0.3,
height = 700,
#width = len(concrete_df.index) * 30 if len(concrete_df.index) > 800 else 1000,
title = f"{plot_name}",
xaxis_tickangle=-45,
xaxis = dict(
title = "Date Cast",
ticktext = concrete_df.index,
tickvals = concrete_df.index,),
yaxis = dict(
title = "Concrete Strength (MPa)"
))
fig.show()
def abc_reports(
report: pathlib.Path,
templates_dir: pathlib.Path
) -> pd.Series:
"""
Returns a pandas series representing the data captured from the
conrete test report file, 'report'.
"""
samples_df, spec_strength_df, supplier_df, cast_info_df = read_pdf_data(
report, templates_dir / "ABC_Company.json"
)
# Clean and extract the sample data
header_first_row = any(samples_df.iloc[0].str.contains("SAMPLE").fillna(False))
header_second_row = any(samples_df.iloc[1].str.contains("SAMPLE").fillna(False))
header_third_row = any(samples_df.iloc[2].str.contains("SAMPLE").fillna(False))
if header_first_row == True:
header_row_idx = 0
elif header_second_row == True:
header_row_idx = 1
elif header_third_row == True:
header_row_idx = 3
header_row = pd.Index(samples_df.iloc[header_row_idx])
sample_col_idx = header_row.get_loc("SAMPLE")
try:
age_col_idx = header_row.get_loc("AGE(DAYS)")
except KeyError:
age_col_idx = header_row.get_loc("AGE (DAYS)")
strength_col_idx = header_row.get_loc("COMPRESSIVE STRENGTH (MPa)")
clean_samples = samples_df.loc[
header_row_idx + 1:, [sample_col_idx, age_col_idx, strength_col_idx]
].reset_index(drop=True)
clean_samples.columns = ["Sample", "Age", "Strength"]
# Extract spec_strength
spec_strength = spec_strength_df.iloc[:,1].item()
spec_age = spec_strength_df.iloc[:,4].item()
# Extract supplier
if len(supplier_df) == 3:
supplier_name = supplier_df.iloc[0, 1]
mix_id = supplier_df.loc[2, 1]
elif len(supplier_df) == 2:
supplier_name = supplier_df.iloc[0, 1]
mix_id = supplier_df.loc[1, 1]
# Extract casting info
air_content = cast_info_df.iloc[0, 1]
if air_content == "%":
air_content = cast_info_df.iloc[0, 0].split(" ")[-1]
if isinstance(air_content, str):
air_content = air_content.replace(" ","").replace("%","")
if cast_info_df.iloc[2, 1] != cast_info_df.iloc[2, 1]:
cast_date = cast_info_df.iloc[2, 0].split()[-1]
else:
cast_date = cast_info_df.iloc[2, 1]
pour_location = cast_info_df.iloc[4,0]
return pd.Series(data={
"Date Cast": cast_date,
"Expected (MPa)": int(spec_strength),
"Expected (Days)": int(spec_age),
"Supplier": supplier_name,
"Mix ID": mix_id,
"Sample A (MPa)": clean_samples.loc[0, "Strength"],
"Sample A (Age)": clean_samples.loc[0, "Age"],
"Sample B (MPa)": clean_samples.loc[1, "Strength"],
"Sample B (Age)": clean_samples.loc[1, "Age"],
"Sample C (MPa)": clean_samples.loc[2, "Strength"],
"Sample C (Age)": clean_samples.loc[2, "Age"],
"Sample D (MPa)": clean_samples.loc[3, "Strength"],
"Sample D (Age)": clean_samples.loc[3, "Age"],
"Entrained Air, %": float(air_content),
"Location": pour_location,
"File": f"{report.name}"})