Last active 4 days ago

Revision 021addfc71a8343c93ee13db548e06df4713b973

SpringAnalyze.py Raw
1import pandas as pd
2
3
4# ---------------------------------------------------------
5# STEP 1 - Get user inputs
6# ---------------------------------------------------------
7
8spring_name = input(
9 "Enter spring number (example: 1040_02): "
10).strip()
11
12threshold = float(
13 input(
14 "Enter peak detection threshold (mm): "
15 )
16)
17
18
19# ---------------------------------------------------------
20# STEP 2 - Open cleaned file
21# ---------------------------------------------------------
22
23input_file = f"Spring_{spring_name}_Cleaned.csv"
24
25print(f"\nLoading {input_file}...")
26
27df = pd.read_csv(input_file)
28
29
30# ---------------------------------------------------------
31# STEP 3 - Define important column names
32# ---------------------------------------------------------
33
34extension_column = "Extension from Preload (mm)"
35
36
37# ---------------------------------------------------------
38# STEP 4 - Create Above Threshold flag
39#
40# 1 = Extension is above threshold
41# 0 = Extension is below threshold
42# ---------------------------------------------------------
43print("Creating Above Threshold...")
44df["Above Threshold"] = (
45 df[extension_column] >= threshold
46).astype(int)
47
48
49# ---------------------------------------------------------
50# STEP 5 - Create Previous Above Threshold column
51#
52# Shift the Above Threshold column down by one row
53# ---------------------------------------------------------
54print("Creating Previous Above Threshold...")
55df["Previous Above Threshold"] = (
56 df["Above Threshold"]
57 .shift(1)
58 .fillna(0)
59 .astype(int)
60)
61
62
63# ---------------------------------------------------------
64# STEP 6 - Create Rising Edge column
65#
66# Rising Edge occurs when:
67#
68# Previous = 0
69# Current = 1
70# ---------------------------------------------------------
71print("Creating Rising Edge...")
72df["Rising Edge"] = (
73 (df["Above Threshold"] == 1)
74 &
75 (df["Previous Above Threshold"] == 0)
76).astype(int)
77
78
79# ---------------------------------------------------------
80# STEP 7 - Create Falling Edge column
81#
82# Falling Edge occurs when:
83#
84# Previous = 1
85# Current = 0
86# ---------------------------------------------------------
87print("Creating Falling Edge...")
88df["Falling Edge"] = (
89 (df["Above Threshold"] == 0)
90 &
91 (df["Previous Above Threshold"] == 1)
92).astype(int)
93
94
95# ---------------------------------------------------------
96# STEP 8 - Create Cycle Number
97#
98# Every Rising Edge starts a new cycle.
99#
100# Example:
101#
102# Rising Edge:
103# 0 0 1 0 0 1 0
104#
105# Cycle Number:
106# 0 0 1 1 1 2 2
107# ---------------------------------------------------------
108print("Creating Cycle Number...")
109df["Cycle Number"] = (
110 df["Rising Edge"]
111 .cumsum()
112)
113
114
115# ---------------------------------------------------------
116# STEP 9 - Save analyzed file
117# ---------------------------------------------------------
118print("Saving Analyzed File...")
119output_file = f"Spring_{spring_name}_Analyzed.csv"
120
121df.to_csv(output_file, index=False)
122
123
124# ---------------------------------------------------------
125# STEP 11 - Create Peak Summary
126# ---------------------------------------------------------
127
128peak_summary = []
129
130print("Creating Cycle Numbers...")
131# Get all valid cycle numbers
132cycle_numbers = sorted(
133 df[df["Cycle Number"] > 0]["Cycle Number"].unique()
134)
135
136for cycle in cycle_numbers:
137
138 # Get only rows for this cycle that are above threshold
139 cycle_data = df[
140 (df["Cycle Number"] == cycle)
141 &
142 (df["Above Threshold"] == 1)
143 ]
144
145 # Skip empty cycles just in case
146 if len(cycle_data) == 0:
147 continue
148
149 # Find row with maximum load
150 peak_index = cycle_data["Load (N)"].idxmax()
151
152 peak_row = df.loc[peak_index]
153
154 peak_summary.append({
155 "Cycle Number": cycle,
156 "Test": peak_row["Test"],
157 "Peak Load (N)": peak_row["Load (N)"],
158 "Peak Extension (mm)": peak_row["Extension from Preload (mm)"],
159 "Peak Time (s)": peak_row["Continuous Time (s)"],
160 "Peak Row": peak_index
161 })
162
163# ---------------------------------------------------------
164# STEP 12 - Create dataframe
165# ---------------------------------------------------------
166
167peak_df = pd.DataFrame(peak_summary)
168
169# ---------------------------------------------------------
170# STEP 13 - Save peak summary
171# ---------------------------------------------------------
172print("Saving peak summary...")
173peak_output_file = (
174 f"Spring_{spring_name}_Peaks.csv"
175)
176
177peak_df.to_csv(
178 peak_output_file,
179 index=False
180)
181
182# ---------------------------------------------------------
183# STEP 14 - Report results
184# ---------------------------------------------------------
185
186total_cycles = int(
187 df["Cycle Number"].max()
188)
189
190print("\nAnalysis Complete")
191print(
192 f"Total Cycles Found: "
193 f"{total_cycles}"
194)
195
196print(
197 f"Analyzed File: "
198 f"{output_file}"
199)
200
201print(
202 f"Peak Summary File: "
203 f"{peak_output_file}"
204)
SpringClean.py Raw
1import pandas as pd
2import glob
3import os
4
5
6# ---------------------------------------------------------
7# STEP 1 - Ask user which spring to process
8# ---------------------------------------------------------
9spring_name = input("Enter spring number (example: 1040_01): ").strip()
10
11
12# ---------------------------------------------------------
13# STEP 2 - Find all matching test files
14# ---------------------------------------------------------
15file_pattern = f"Spring_{spring_name}_Test_*.csv"
16
17file_list = glob.glob(file_pattern)
18
19# Make sure files are processed in order
20file_list.sort()
21
22if len(file_list) == 0:
23 print(f"No files found matching: {file_pattern}")
24 exit()
25
26
27# ---------------------------------------------------------
28# STEP 3 - Variables used while combining files
29# ---------------------------------------------------------
30combined_data = []
31
32time_offset = 0
33
34
35# ---------------------------------------------------------
36# STEP 4 - Process each test file
37# ---------------------------------------------------------
38for test_number, file in enumerate(file_list, start=1):
39
40 print(f"Processing: {file}")
41
42 # Read CSV
43 df = pd.read_csv(file)
44
45 # Remove rows containing empty data
46 df = df.dropna()
47
48 # Time column name
49 time_column = "Time (s)"
50
51 # Create Test column
52 df["Test"] = test_number
53
54 # Create continuous time column
55 df["Continuous Time (s)"] = df[time_column] + time_offset
56
57 # Determine ending time for next test
58 last_time = df[time_column].iloc[-1]
59
60 time_offset += last_time
61
62 # Store cleaned data
63 combined_data.append(df)
64
65
66# ---------------------------------------------------------
67# STEP 5 - Combine all tests into one dataframe
68# ---------------------------------------------------------
69final_df = pd.concat(combined_data, ignore_index=True)
70
71
72# ---------------------------------------------------------
73# STEP 6 - Create output filename
74# ---------------------------------------------------------
75output_file = f"Spring_{spring_name}_Cleaned.csv"
76
77
78# ---------------------------------------------------------
79# STEP 7 - Save cleaned data
80# ---------------------------------------------------------
81final_df.to_csv(output_file, index=False)
82
83print()
84print(f"Finished!")
85print(f"Output saved as: {output_file}")
This file can't be rendered. View the full file.