Utility Functions
Standalone functions for working with tensile test data without class instantiation.
Input/Output Functions
load_csv()
load_csv(filepath, config=None)
Load tensile test data from CSV file.
Parameters:
- filepath : str
- Path to CSV file
- config : TensileTestConfig, optional
- Configuration for parsing. If None, uses defaults.
Returns: pd.DataFrame with columns: force, extension
from tensile.io import load_csv
df = load_csv("test_data.csv")
print(df.head())
load_batch()
load_batch(folder_path, pattern='*.csv', config=None)
Load multiple CSV files from a folder.
Parameters:
- folder_path : str
- Path to folder
- pattern : str, default='*.csv'
- Glob pattern for file matching
- config : TensileTestConfig, optional
- Configuration for parsing
Returns: dict - {filename: DataFrame}
from tensile.io import load_batch
data_dict = load_batch("data/316L_A/")
for name, df in data_dict.items():
print(f"{name}: {len(df)} rows")
Analysis Functions
calculate_youngs_modulus()
calculate_youngs_modulus(stress, strain, strain_range=(0.0005, 0.003))
Calculate Young's modulus from stress-strain data using linear regression.
Parameters:
- stress : array-like
- Stress values in MPa
- strain : array-like
- Strain values (dimensionless)
- strain_range : tuple, default=(0.0005, 0.003)
- Strain range for linear fitting
Returns: dict - {'E_GPa': float, 'R2': float,
'n_points': int}
from tensile.analysis import calculate_youngs_modulus
result = calculate_youngs_modulus(stress, strain)
print(f"E = {result['E_GPa']:.1f} GPa (R² = {result['R2']:.4f})")
find_yield_strength()
find_yield_strength(stress, strain, offset=0.002)
Find yield strength (Rp0.2) using offset method.
Parameters:
- stress : array-like
- Stress values in MPa
- strain : array-like
- Strain values
- offset : float, default=0.002
- Offset strain (0.2% = 0.002)
Returns: dict - {'Rp02_MPa': float, 'strain_at_rp02':
float}
from tensile.analysis import find_yield_strength
result = find_yield_strength(stress, strain)
print(f"Rp0.2 = {result['Rp02_MPa']:.1f} MPa")
detect_slippage()
detect_slippage(stress, strain, stress_threshold=50.0,
strain_limit=0.005)
Detect initial slippage in stress-strain curve.
Parameters:
- stress : array-like
- Stress values in MPa
- strain : array-like
- Strain values
- stress_threshold : float, default=50.0
- Stress threshold in MPa
- strain_limit : float, default=0.005
- Max strain to check (0.5%)
Returns: dict - {'detected': bool, 'cutoff_index':
int, 'cutoff_strain': float}
from tensile.segmentation import detect_slippage
result = detect_slippage(stress, strain)
if result['detected']:
print(f"Slippage detected at strain {result['cutoff_strain']:.4f}")
# Remove slippage region
stress_clean = stress[result['cutoff_index']:]
strain_clean = strain[result['cutoff_index']:]
Visualization Functions
plot_stress_strain()
plot_stress_strain(stress, strain, title="Stress-Strain Curve",
markers=None)
Create interactive stress-strain plot with Plotly.
Parameters:
- stress : array-like
- Stress values in MPa
- strain : array-like
- Strain values
- title : str, optional
- Plot title
- markers : dict, optional
- Key points to annotate: {'Rp02': (x, y), 'Rm': (x, y)}
Returns: plotly.graph_objects.Figure
from tensile.visualization import plot_stress_strain
fig = plot_stress_strain(
stress, strain,
title="316L Tensile Test",
markers={'Rp02': (0.01, 250), 'Rm': (0.15, 600)}
)
fig.show()
plot_batch_comparison()
plot_batch_comparison(tests_dict, overlay=True, title="Batch
Comparison")
Compare multiple stress-strain curves on one plot.
Parameters:
- tests_dict : dict
- Dictionary of {name: (stress, strain)}
- overlay : bool, default=True
- If False, create subplots
- title : str, optional
- Plot title
Returns: plotly.graph_objects.Figure
from tensile.visualization import plot_batch_comparison
tests = {
'Test_1': (stress1, strain1),
'Test_2': (stress2, strain2),
'Test_3': (stress3, strain3)
}
fig = plot_batch_comparison(tests, title="316L Batch A")
fig.show()
Quick Example Workflow
from tensile.io import load_csv
from tensile.cleaning import clean_data
from tensile.analysis import calculate_youngs_modulus, find_yield_strength
from tensile.visualization import plot_stress_strain
# Load data
df = load_csv("test.csv")
# Clean and calculate stress-strain
df_clean = clean_data(df, specimen_length=50.0, specimen_diameter=5.0)
stress = df_clean['stress'].values
strain = df_clean['strain'].values
# Analysis
E_result = calculate_youngs_modulus(stress, strain)
rp_result = find_yield_strength(stress, strain)
print(f"E = {E_result['E_GPa']:.1f} GPa")
print(f"Rp0.2 = {rp_result['Rp02_MPa']:.1f} MPa")
# Plot
markers = {'Rp02': (rp_result['strain_at_rp02'], rp_result['Rp02_MPa'])}
fig = plot_stress_strain(stress, strain, markers=markers)
fig.show()