generated from KemingHe/agentic-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrain_analysis_data.py
225 lines (178 loc) · 6.19 KB
/
strain_analysis_data.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
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
"""Data analysis and visualization module for strain analysis.
This module provides functions for analyzing strain data, performing regression,
and creating visualizations. It is designed to be easily extensible for researchers
to add their own analysis methods.
"""
from typing import Dict, List, TypedDict
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
import json
class StrainData(TypedDict):
"""Type definition for strain data."""
filename: str
deformation_distance: float
strain_tensor: np.ndarray
class RegressionResult(TypedDict):
"""Type definition for regression results."""
slope: float
intercept: float
r_squared: float
p_value: float
class AnalysisResults(TypedDict):
"""Type definition for complete analysis results."""
strain_data: List[StrainData]
scale_length: float
regression_results: Dict[str, RegressionResult]
def format_scientific(value: float, unit: str = "") -> str:
"""Format numbers in scientific notation for readability.
Args:
value: The number to format (can be float or numpy array)
unit: Optional unit to append to the formatted number
Returns:
str: Formatted number in scientific notation with optional unit
"""
value_float = float(value.item() if isinstance(value, np.ndarray) else value)
return f"{value_float:.2e} {unit}".strip()
def calculate_regression(
x_values: List[float], y_values: List[float]
) -> RegressionResult:
"""Calculate linear regression for a set of x and y values.
Args:
x_values: List of x values (e.g., deformation distances)
y_values: List of y values (e.g., strain components)
Returns:
RegressionResult containing slope, intercept, R², and p-value
"""
slope, intercept, r_value, p_value, _ = stats.linregress(x_values, y_values)
return {
"slope": float(slope),
"intercept": float(intercept),
"r_squared": float(r_value**2),
"p_value": float(p_value),
}
def analyze_strain_data(
strain_data: List[StrainData], scale_length: float
) -> AnalysisResults:
"""Analyze strain data and calculate regression results.
Args:
strain_data: List of strain data points
scale_length: Physical length of the scale in real-world units
Returns:
AnalysisResults containing strain data and regression results
"""
x_values = [d["deformation_distance"] for d in strain_data]
strain_components = {"x_axis": (0, 0), "y_axis": (1, 1), "shear": (0, 1)}
regression_results = {
name: calculate_regression(
x_values, [d["strain_tensor"][i, j] for d in strain_data]
)
for name, (i, j) in strain_components.items()
}
return {
"strain_data": strain_data,
"scale_length": scale_length,
"regression_results": regression_results,
}
def create_strain_plot(
x_values: List[float],
y_values: List[float],
results: RegressionResult,
title: str,
ylabel: str,
) -> plt.Figure:
"""Create a plot for strain data with regression line.
Args:
x_values: List of x values (deformation distances)
y_values: List of y values (strain components)
results: Regression results
title: Plot title
ylabel: Y-axis label
Returns:
matplotlib Figure object
"""
fig, ax = plt.subplots()
ax.scatter(x_values, y_values, color="blue", label="Data points")
ax.plot(
x_values,
[results["slope"] * x + results["intercept"] for x in x_values],
color="red",
label="Regression line",
)
ax.set_xlabel("Deformation Distance (mm)")
ax.set_ylabel(ylabel)
ax.legend()
return fig
def create_strain_dataframe(
x_values: List[float], y_values: List[float], strain_type: str
) -> pd.DataFrame:
"""Create a DataFrame for strain data.
Args:
x_values: List of x values (deformation distances)
y_values: List of y values (strain components)
strain_type: Type of strain (e.g., "εxx", "εyy", "εxy")
Returns:
pandas DataFrame with formatted data
"""
return pd.DataFrame(
{
"Distance (mm)": [f"{x:.2f}" for x in x_values],
strain_type: [format_scientific(y, "") for y in y_values],
}
)
def export_data(data: Dict, format: str = "json") -> str:
"""Export data in specified format."""
if format == "json":
return json.dumps(data, indent=2)
elif format == "csv":
return pd.DataFrame(data).to_csv(index=False)
else:
raise ValueError(f"Unsupported format: {format}")
def export_raw_data(
files_data: Dict, scale_length: float, strain_data: List[StrainData]
) -> tuple[str, str]:
"""Export raw data in JSON and CSV formats.
Args:
files_data: Dictionary containing file data
scale_length: Physical length of the scale
strain_data: List of strain data points
Returns:
Tuple of (json_str, csv_str) containing formatted data
"""
# JSON format
raw_data = {
"files_data": files_data,
"scale_length": scale_length,
}
json_str = export_data(raw_data, "json")
# CSV format
csv_data = [
{
"filename": data["filename"],
"deformation_distance": data["deformation_distance"],
"strain_xx": data["strain_tensor"][0, 0],
"strain_yy": data["strain_tensor"][1, 1],
"strain_xy": data["strain_tensor"][0, 1],
}
for data in strain_data
]
csv_str = export_data(csv_data, "csv")
return json_str, csv_str
def export_analysis_results(
regression_results: Dict[str, RegressionResult],
) -> tuple[str, str]:
"""Export analysis results in JSON and CSV formats.
Args:
regression_results: Dictionary containing regression results
Returns:
Tuple of (json_str, csv_str) containing formatted results
"""
# JSON format
json_str = export_data(regression_results, "json")
# CSV format
analysis_data = [
{"component": name, **results} for name, results in regression_results.items()
]
csv_str = export_data(analysis_data, "csv")
return json_str, csv_str