Last active 4 days ago

wschrab revised this gist 4 days ago. Go to revision

1 file changed, 0 insertions, 0 deletions

SpringCleanAndAnalyze.exe(file created)

Binary file changes are not shown

wschrab revised this gist 4 days ago. Go to revision

1 file changed, 0 insertions, 0 deletions

SpringCleanAndAnalyze.exe (file deleted)

Binary file changes are not shown

wschrab revised this gist 4 days ago. Go to revision

No changes

wschrab revised this gist 4 days ago. Go to revision

1 file changed, 43 insertions

errors.txt(file created)

@@ -0,0 +1,43 @@
1 + ---------------------------------------------------------
2 + Checking spring: 1090_02
3 + ---------------------------------------------------------
4 + Found 3 file(s).
5 + Processing: Spring_1090_02_Test_01.csv
6 + Processing: Spring_1090_02_Test_02.csv
7 + Processing: Spring_1090_02_Test_03.csv
8 +
9 + Finished Cleaning in memory.
10 + Cleaned file was not saved because user selected no.
11 + Creating Above Threshold...
12 + Traceback (most recent call last):
13 + File "C:\Users\wdsch\AppData\Roaming\Python\Python314\site-packages\pandas\core\indexes\base.py", line 3641, in get_loc
14 + return self._engine.get_loc(casted_key)
15 + ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
16 + File "pandas/_libs/index.pyx", line 168, in pandas._libs.index.IndexEngine.get_loc
17 + File "pandas/_libs/index.pyx", line 197, in pandas._libs.index.IndexEngine.get_loc
18 + File "pandas/_libs/hashtable_class_helper.pxi", line 7668, in pandas._libs.hashtable.PyObjectHashTable.get_item
19 + File "pandas/_libs/hashtable_class_helper.pxi", line 7676, in pandas._libs.hashtable.PyObjectHashTable.get_item
20 + KeyError: 'Extension (mm)'
21 +
22 + The above exception was the direct cause of the following exception:
23 +
24 + Traceback (most recent call last):
25 + File "c:\Users\wdsch\OneDrive\Documents\Halozyme\Spring Python\SpringCleanAndAnalyze.py", line 369, in <module>
26 + process_spring(
27 + ~~~~~~~~~~~~~~^
28 + spring_name,
29 + ^^^^^^^^^^^^
30 + threshold,
31 + ^^^^^^^^^^
32 + save_intermediate_files
33 + ^^^^^^^^^^^^^^^^^^^^^^^
34 + )
35 + ^
36 + File "c:\Users\wdsch\OneDrive\Documents\Halozyme\Spring Python\SpringCleanAndAnalyze.py", line 167, in process_spring
37 + df[extension_column] >= threshold
38 + ~~^^^^^^^^^^^^^^^^^^
39 + File "C:\Users\wdsch\AppData\Roaming\Python\Python314\site-packages\pandas\core\frame.py", line 4378, in __getitem__
40 + indexer = self.columns.get_loc(key)
41 + File "C:\Users\wdsch\AppData\Roaming\Python\Python314\site-packages\pandas\core\indexes\base.py", line 3648, in get_loc
42 + raise KeyError(key) from err
43 + KeyError: 'Extension (mm)'

wschrab revised this gist 4 days ago. Go to revision

1 file changed, 383 insertions

SpringCleanAndAnalzye2.py(file created)

@@ -0,0 +1,383 @@
1 + import pandas as pd
2 + import glob
3 + import os
4 +
5 +
6 + # ---------------------------------------------------------
7 + # STEP 1 - Ask user for settings that apply to all springs
8 + # ---------------------------------------------------------
9 +
10 + threshold = float(
11 + input(
12 + "Enter peak detection threshold (mm): "
13 + )
14 + )
15 +
16 + # This controls whether the larger intermediate files are saved.
17 + # The cleaned and analyzed DataFrames are still created in memory either way
18 + # because they are needed to calculate the peak summary.
19 + generate_cleaned_analyzed = input(
20 + "Generate cleaned and analyzed files? (y/n): "
21 + ).strip().lower()
22 +
23 + save_intermediate_files = generate_cleaned_analyzed == "y"
24 +
25 +
26 + # ---------------------------------------------------------
27 + # STEP 2 - Create list of springs to process
28 + #
29 + # The program will process springs in this order:
30 + #
31 + # 1040_00, 1040_01, ..., 1040_99
32 + # 1090_00, 1090_01, ..., 1090_99
33 + # ---------------------------------------------------------
34 +
35 + spring_list = []
36 +
37 + for spring_prefix in ["1040", "1090"]:
38 + for spring_number in range(100):
39 + spring_name = f"{spring_prefix}_{spring_number:02d}"
40 + spring_list.append(spring_name)
41 +
42 +
43 + # ---------------------------------------------------------
44 + # STEP 3 - Function to process one spring
45 + #
46 + # This function does everything your original program did,
47 + # but for one automatically-selected spring instead of one
48 + # manually-entered spring.
49 + # ---------------------------------------------------------
50 +
51 + def process_spring(spring_name, threshold, save_intermediate_files):
52 +
53 + print()
54 + print("---------------------------------------------------------")
55 + print(f"Checking spring: {spring_name}")
56 + print("---------------------------------------------------------")
57 +
58 + # ---------------------------------------------------------
59 + # Find all matching test files for this spring
60 + # ---------------------------------------------------------
61 +
62 + file_pattern = f"Spring_{spring_name}_Test_*.csv"
63 +
64 + file_list = glob.glob(file_pattern)
65 +
66 + # Make sure files are processed in order.
67 + # This is important because Test 1 should come before Test 2, etc.
68 + file_list.sort()
69 +
70 + # If this spring has no matching files, skip it.
71 + if len(file_list) == 0:
72 + print(f"No files found matching: {file_pattern}")
73 + return
74 +
75 + print(f"Found {len(file_list)} file(s).")
76 +
77 + # ---------------------------------------------------------
78 + # Variables used while combining files
79 + # ---------------------------------------------------------
80 +
81 + combined_data = []
82 +
83 + time_offset = 0
84 +
85 + # ---------------------------------------------------------
86 + # Process each test file for this spring
87 + # ---------------------------------------------------------
88 +
89 + for test_number, file in enumerate(file_list, start=1):
90 +
91 + print(f"Processing: {file}")
92 +
93 + # Read CSV
94 + df = pd.read_csv(file)
95 +
96 + # Remove rows containing empty data
97 + df = df.dropna()
98 +
99 + # Time column name
100 + time_column = "Time (s)"
101 +
102 + # Create Test column.
103 + # This assigns Test 1, Test 2, Test 3, etc. based on file order.
104 + df["Test"] = test_number
105 +
106 + # Create continuous time column.
107 + # This makes the time from multiple files act like one continuous test.
108 + df["Continuous Time (s)"] = df[time_column] + time_offset
109 +
110 + # Determine ending time for next test.
111 + # The next file's time starts where this one ended.
112 + last_time = df[time_column].iloc[-1]
113 +
114 + time_offset += last_time
115 +
116 + # Store cleaned data in memory.
117 + combined_data.append(df)
118 +
119 + # ---------------------------------------------------------
120 + # Combine all tests into one DataFrame
121 + # ---------------------------------------------------------
122 +
123 + final_df = pd.concat(combined_data, ignore_index=True)
124 +
125 + # ---------------------------------------------------------
126 + # Save cleaned data only if user selected yes
127 + # ---------------------------------------------------------
128 +
129 + cleaned_output_file = f"Spring_{spring_name}_Cleaned.csv"
130 +
131 + if save_intermediate_files:
132 + final_df.to_csv(cleaned_output_file, index=False)
133 +
134 + print()
135 + print("Finished Cleaning!")
136 + print(f"Cleaned output saved as: {cleaned_output_file}")
137 + else:
138 + print()
139 + print("Finished Cleaning in memory.")
140 + print("Cleaned file was not saved because user selected no.")
141 +
142 + # ---------------------------------------------------------------------
143 + # ANALYZING SECTION
144 + # ---------------------------------------------------------------------
145 +
146 + # Instead of opening the cleaned file from disk, use final_df directly.
147 + # This allows the analysis to work even when the user chooses not to save
148 + # the cleaned CSV file.
149 + df = final_df.copy()
150 +
151 + # ---------------------------------------------------------
152 + # Define important column names
153 + # ---------------------------------------------------------
154 +
155 + extension_column = "Extension (mm)"
156 +
157 + # ---------------------------------------------------------
158 + # Create Above Threshold flag
159 + #
160 + # 1 = Extension is above threshold
161 + # 0 = Extension is below threshold
162 + # ---------------------------------------------------------
163 +
164 + print("Creating Above Threshold...")
165 +
166 + df["Above Threshold"] = (
167 + df[extension_column] >= threshold
168 + ).astype(int)
169 +
170 + # ---------------------------------------------------------
171 + # Create Previous Above Threshold column
172 + #
173 + # Shift the Above Threshold column down by one row.
174 + # This lets each row compare itself to the row before it.
175 + # ---------------------------------------------------------
176 +
177 + print("Creating Previous Above Threshold...")
178 +
179 + df["Previous Above Threshold"] = (
180 + df["Above Threshold"]
181 + .shift(1)
182 + .fillna(0)
183 + .astype(int)
184 + )
185 +
186 + # ---------------------------------------------------------
187 + # Create Rising Edge column
188 + #
189 + # Rising Edge occurs when:
190 + #
191 + # Previous = 0
192 + # Current = 1
193 + #
194 + # This marks the start of a compression cycle.
195 + # ---------------------------------------------------------
196 +
197 + print("Creating Rising Edge...")
198 +
199 + df["Rising Edge"] = (
200 + (df["Above Threshold"] == 1)
201 + &
202 + (df["Previous Above Threshold"] == 0)
203 + ).astype(int)
204 +
205 + # ---------------------------------------------------------
206 + # Create Falling Edge column
207 + #
208 + # Falling Edge occurs when:
209 + #
210 + # Previous = 1
211 + # Current = 0
212 + #
213 + # This marks where the signal drops back below the threshold.
214 + # ---------------------------------------------------------
215 +
216 + print("Creating Falling Edge...")
217 +
218 + df["Falling Edge"] = (
219 + (df["Above Threshold"] == 0)
220 + &
221 + (df["Previous Above Threshold"] == 1)
222 + ).astype(int)
223 +
224 + # ---------------------------------------------------------
225 + # Create Cycle Number
226 + #
227 + # Every Rising Edge starts a new cycle.
228 + #
229 + # Example:
230 + #
231 + # Rising Edge:
232 + # 0 0 1 0 0 1 0
233 + #
234 + # Cycle Number:
235 + # 0 0 1 1 1 2 2
236 + #
237 + # The cumulative sum increases by 1 every time a rising edge occurs.
238 + # ---------------------------------------------------------
239 +
240 + print("Creating Cycle Number...")
241 +
242 + df["Cycle Number"] = (
243 + df["Rising Edge"]
244 + .cumsum()
245 + )
246 +
247 + # ---------------------------------------------------------
248 + # Save analyzed file only if user selected yes
249 + # ---------------------------------------------------------
250 +
251 + analyzed_output_file = f"Spring_{spring_name}_Analyzed.csv"
252 +
253 + if save_intermediate_files:
254 + print("Saving Analyzed File...")
255 +
256 + df.to_csv(analyzed_output_file, index=False)
257 +
258 + print(f"Analyzed output saved as: {analyzed_output_file}")
259 + else:
260 + print("Analyzed file was not saved because user selected no.")
261 +
262 + # ---------------------------------------------------------
263 + # Create Peak Summary
264 + # ---------------------------------------------------------
265 +
266 + peak_summary = []
267 +
268 + print("Creating peak summary...")
269 +
270 + # Get all valid cycle numbers.
271 + # Cycle 0 is ignored because it is before the first rising edge.
272 + cycle_numbers = sorted(
273 + df[df["Cycle Number"] > 0]["Cycle Number"].unique()
274 + )
275 +
276 + for cycle in cycle_numbers:
277 +
278 + # Get only rows for this cycle that are above threshold.
279 + # This searches the active compression portion of the cycle.
280 + cycle_data = df[
281 + (df["Cycle Number"] == cycle)
282 + &
283 + (df["Above Threshold"] == 1)
284 + ]
285 +
286 + # Skip empty cycles just in case.
287 + if len(cycle_data) == 0:
288 + continue
289 +
290 + # Find row with maximum load inside this cycle's above-threshold area.
291 + peak_index = cycle_data["Load (N)"].idxmax()
292 +
293 + peak_row = df.loc[peak_index]
294 +
295 + peak_summary.append({
296 + "Cycle Number": cycle,
297 + "Test": peak_row["Test"],
298 + "Peak Load (N)": peak_row["Load (N)"],
299 + "Peak Extension (mm)": peak_row[extension_column],
300 + "Peak Time (s)": peak_row["Continuous Time (s)"],
301 + "Peak Row": peak_index
302 + })
303 +
304 + # ---------------------------------------------------------
305 + # Create peak summary DataFrame
306 + # ---------------------------------------------------------
307 +
308 + peak_df = pd.DataFrame(peak_summary)
309 +
310 + # ---------------------------------------------------------
311 + # Save peak summary
312 + #
313 + # This file is always saved because it is the main output.
314 + # ---------------------------------------------------------
315 +
316 + print("Saving peak summary...")
317 +
318 + peak_output_file = f"Spring_{spring_name}_Peaks.csv"
319 +
320 + peak_df.to_csv(
321 + peak_output_file,
322 + index=False
323 + )
324 +
325 + # ---------------------------------------------------------
326 + # Report cycles per test
327 + # ---------------------------------------------------------
328 +
329 + if len(peak_df) > 0:
330 +
331 + cycles_per_test = (
332 + peak_df.groupby("Test").size()
333 + )
334 +
335 + print("Cycles Per Test:")
336 +
337 + for test, count in cycles_per_test.items():
338 + print(f"Test {int(test)}: {count}")
339 +
340 + else:
341 + print("No peaks found for this spring.")
342 +
343 + # ---------------------------------------------------------
344 + # Report results
345 + # ---------------------------------------------------------
346 +
347 + total_cycles = int(
348 + df["Cycle Number"].max()
349 + )
350 +
351 + print()
352 + print("Analysis Complete")
353 + print(f"Spring: {spring_name}")
354 + print(f"Total Cycles Found: {total_cycles}")
355 + print(f"Total Peaks Found: {len(peak_summary)}")
356 +
357 + if save_intermediate_files:
358 + print(f"Cleaned File: {cleaned_output_file}")
359 + print(f"Analyzed File: {analyzed_output_file}")
360 +
361 + print(f"Peak Summary File: {peak_output_file}")
362 +
363 +
364 + # ---------------------------------------------------------
365 + # STEP 4 - Run the process for every spring in the list
366 + # ---------------------------------------------------------
367 +
368 + for spring_name in spring_list:
369 + process_spring(
370 + spring_name,
371 + threshold,
372 + save_intermediate_files
373 + )
374 +
375 +
376 + # ---------------------------------------------------------
377 + # STEP 5 - Keep command window open
378 + # ---------------------------------------------------------
379 +
380 + print()
381 + print("Finished processing all springs.")
382 +
383 + input("\nPress Enter to exit")

wschrab revised this gist 4 days ago. Go to revision

No changes

wschrab revised this gist 4 days ago. Go to revision

3 files changed, 300 insertions, 589 deletions

SpringAnalyze.py (file deleted)

@@ -1,204 +0,0 @@
1 - import pandas as pd
2 -
3 -
4 - # ---------------------------------------------------------
5 - # STEP 1 - Get user inputs
6 - # ---------------------------------------------------------
7 -
8 - spring_name = input(
9 - "Enter spring number (example: 1040_02): "
10 - ).strip()
11 -
12 - threshold = float(
13 - input(
14 - "Enter peak detection threshold (mm): "
15 - )
16 - )
17 -
18 -
19 - # ---------------------------------------------------------
20 - # STEP 2 - Open cleaned file
21 - # ---------------------------------------------------------
22 -
23 - input_file = f"Spring_{spring_name}_Cleaned.csv"
24 -
25 - print(f"\nLoading {input_file}...")
26 -
27 - df = pd.read_csv(input_file)
28 -
29 -
30 - # ---------------------------------------------------------
31 - # STEP 3 - Define important column names
32 - # ---------------------------------------------------------
33 -
34 - extension_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 - # ---------------------------------------------------------
43 - print("Creating Above Threshold...")
44 - df["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 - # ---------------------------------------------------------
54 - print("Creating Previous Above Threshold...")
55 - df["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 - # ---------------------------------------------------------
71 - print("Creating Rising Edge...")
72 - df["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 - # ---------------------------------------------------------
87 - print("Creating Falling Edge...")
88 - df["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 - # ---------------------------------------------------------
108 - print("Creating Cycle Number...")
109 - df["Cycle Number"] = (
110 - df["Rising Edge"]
111 - .cumsum()
112 - )
113 -
114 -
115 - # ---------------------------------------------------------
116 - # STEP 9 - Save analyzed file
117 - # ---------------------------------------------------------
118 - print("Saving Analyzed File...")
119 - output_file = f"Spring_{spring_name}_Analyzed.csv"
120 -
121 - df.to_csv(output_file, index=False)
122 -
123 -
124 - # ---------------------------------------------------------
125 - # STEP 11 - Create Peak Summary
126 - # ---------------------------------------------------------
127 -
128 - peak_summary = []
129 -
130 - print("Creating Cycle Numbers...")
131 - # Get all valid cycle numbers
132 - cycle_numbers = sorted(
133 - df[df["Cycle Number"] > 0]["Cycle Number"].unique()
134 - )
135 -
136 - for 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 -
167 - peak_df = pd.DataFrame(peak_summary)
168 -
169 - # ---------------------------------------------------------
170 - # STEP 13 - Save peak summary
171 - # ---------------------------------------------------------
172 - print("Saving peak summary...")
173 - peak_output_file = (
174 - f"Spring_{spring_name}_Peaks.csv"
175 - )
176 -
177 - peak_df.to_csv(
178 - peak_output_file,
179 - index=False
180 - )
181 -
182 - # ---------------------------------------------------------
183 - # STEP 14 - Report results
184 - # ---------------------------------------------------------
185 -
186 - total_cycles = int(
187 - df["Cycle Number"].max()
188 - )
189 -
190 - print("\nAnalysis Complete")
191 - print(
192 - f"Total Cycles Found: "
193 - f"{total_cycles}"
194 - )
195 -
196 - print(
197 - f"Analyzed File: "
198 - f"{output_file}"
199 - )
200 -
201 - print(
202 - f"Peak Summary File: "
203 - f"{peak_output_file}"
204 - )

SpringClean.py (file deleted)

@@ -1,85 +0,0 @@
1 - import pandas as pd
2 - import glob
3 - import os
4 -
5 -
6 - # ---------------------------------------------------------
7 - # STEP 1 - Ask user which spring to process
8 - # ---------------------------------------------------------
9 - spring_name = input("Enter spring number (example: 1040_01): ").strip()
10 -
11 -
12 - # ---------------------------------------------------------
13 - # STEP 2 - Find all matching test files
14 - # ---------------------------------------------------------
15 - file_pattern = f"Spring_{spring_name}_Test_*.csv"
16 -
17 - file_list = glob.glob(file_pattern)
18 -
19 - # Make sure files are processed in order
20 - file_list.sort()
21 -
22 - if 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 - # ---------------------------------------------------------
30 - combined_data = []
31 -
32 - time_offset = 0
33 -
34 -
35 - # ---------------------------------------------------------
36 - # STEP 4 - Process each test file
37 - # ---------------------------------------------------------
38 - for 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 - # ---------------------------------------------------------
69 - final_df = pd.concat(combined_data, ignore_index=True)
70 -
71 -
72 - # ---------------------------------------------------------
73 - # STEP 6 - Create output filename
74 - # ---------------------------------------------------------
75 - output_file = f"Spring_{spring_name}_Cleaned.csv"
76 -
77 -
78 - # ---------------------------------------------------------
79 - # STEP 7 - Save cleaned data
80 - # ---------------------------------------------------------
81 - final_df.to_csv(output_file, index=False)
82 -
83 - print()
84 - print(f"Finished!")
85 - print(f"Output saved as: {output_file}")

SpringCleanAndAnalyze.py

@@ -1,301 +1,301 @@
1 - import pandas as pd
2 - import glob
3 - import os
4 -
5 -
6 - # ---------------------------------------------------------
7 - # STEP 1 - Ask user which spring to process
8 - # ---------------------------------------------------------
9 - spring_name = input("Enter spring number (example: 1040_01): ").strip()
10 -
11 - threshold = float(
12 - input(
13 - "Enter peak detection threshold (mm): "
14 - )
15 - )
16 -
17 -
18 - # ---------------------------------------------------------
19 - # STEP 2 - Find all matching test files
20 - # ---------------------------------------------------------
21 - file_pattern = f"Spring_{spring_name}_Test_*.csv"
22 -
23 - file_list = glob.glob(file_pattern)
24 -
25 - # Make sure files are processed in order
26 - file_list.sort()
27 -
28 - if len(file_list) == 0:
29 - print(f"No files found matching: {file_pattern}")
30 - exit()
31 -
32 -
33 - # ---------------------------------------------------------
34 - # STEP 3 - Variables used while combining files
35 - # ---------------------------------------------------------
36 - combined_data = []
37 -
38 - time_offset = 0
39 -
40 -
41 - # ---------------------------------------------------------
42 - # STEP 4 - Process each test file
43 - # ---------------------------------------------------------
44 - for test_number, file in enumerate(file_list, start=1):
45 -
46 - print(f"Processing: {file}")
47 -
48 - # Read CSV
49 - df = pd.read_csv(file)
50 -
51 - # Remove rows containing empty data
52 - df = df.dropna()
53 -
54 - # Time column name
55 - time_column = "Time (s)"
56 -
57 - # Create Test column
58 - df["Test"] = test_number
59 -
60 - # Create continuous time column
61 - df["Continuous Time (s)"] = df[time_column] + time_offset
62 -
63 - # Determine ending time for next test
64 - last_time = df[time_column].iloc[-1]
65 -
66 - time_offset += last_time
67 -
68 - # Store cleaned data
69 - combined_data.append(df)
70 -
71 -
72 - # ---------------------------------------------------------
73 - # STEP 5 - Combine all tests into one dataframe
74 - # ---------------------------------------------------------
75 - final_df = pd.concat(combined_data, ignore_index=True)
76 -
77 -
78 - # ---------------------------------------------------------
79 - # STEP 6 - Create output filename
80 - # ---------------------------------------------------------
81 - output_file = f"Spring_{spring_name}_Cleaned.csv"
82 -
83 -
84 - # ---------------------------------------------------------
85 - # STEP 7 - Save cleaned data
86 - # ---------------------------------------------------------
87 - final_df.to_csv(output_file, index=False)
88 -
89 - print()
90 - print(f"Finished Cleaning!")
91 - print(f"Output saved as: {output_file}")
92 -
93 -
94 - # ---------------------------------------------------------------------
95 - # ANALYZING SECTION
96 - # ---------------------------------------------------------------------
97 -
98 -
99 - # ---------------------------------------------------------
100 - # STEP 2 - Open cleaned file
101 - # ---------------------------------------------------------
102 -
103 - input_file = f"Spring_{spring_name}_Cleaned.csv"
104 -
105 - print(f"\nLoading {input_file}...")
106 -
107 - df = pd.read_csv(input_file)
108 -
109 -
110 - # ---------------------------------------------------------
111 - # STEP 3 - Define important column names
112 - # ---------------------------------------------------------
113 -
114 - extension_column = "Extension (mm)"
115 -
116 -
117 - # ---------------------------------------------------------
118 - # STEP 4 - Create Above Threshold flag
119 - #
120 - # 1 = Extension is above threshold
121 - # 0 = Extension is below threshold
122 - # ---------------------------------------------------------
123 - print("Creating Above Threshold...")
124 - df["Above Threshold"] = (
125 - df[extension_column] >= threshold
126 - ).astype(int)
127 -
128 -
129 - # ---------------------------------------------------------
130 - # STEP 5 - Create Previous Above Threshold column
131 - #
132 - # Shift the Above Threshold column down by one row
133 - # ---------------------------------------------------------
134 - print("Creating Previous Above Threshold...")
135 - df["Previous Above Threshold"] = (
136 - df["Above Threshold"]
137 - .shift(1)
138 - .fillna(0)
139 - .astype(int)
140 - )
141 -
142 -
143 - # ---------------------------------------------------------
144 - # STEP 6 - Create Rising Edge column
145 - #
146 - # Rising Edge occurs when:
147 - #
148 - # Previous = 0
149 - # Current = 1
150 - # ---------------------------------------------------------
151 - print("Creating Rising Edge...")
152 - df["Rising Edge"] = (
153 - (df["Above Threshold"] == 1)
154 - &
155 - (df["Previous Above Threshold"] == 0)
156 - ).astype(int)
157 -
158 -
159 - # ---------------------------------------------------------
160 - # STEP 7 - Create Falling Edge column
161 - #
162 - # Falling Edge occurs when:
163 - #
164 - # Previous = 1
165 - # Current = 0
166 - # ---------------------------------------------------------
167 - print("Creating Falling Edge...")
168 - df["Falling Edge"] = (
169 - (df["Above Threshold"] == 0)
170 - &
171 - (df["Previous Above Threshold"] == 1)
172 - ).astype(int)
173 -
174 -
175 - # ---------------------------------------------------------
176 - # STEP 8 - Create Cycle Number
177 - #
178 - # Every Rising Edge starts a new cycle.
179 - #
180 - # Example:
181 - #
182 - # Rising Edge:
183 - # 0 0 1 0 0 1 0
184 - #
185 - # Cycle Number:
186 - # 0 0 1 1 1 2 2
187 - # ---------------------------------------------------------
188 - print("Creating Cycle Number...")
189 - df["Cycle Number"] = (
190 - df["Rising Edge"]
191 - .cumsum()
192 - )
193 -
194 -
195 - # ---------------------------------------------------------
196 - # STEP 9 - Save analyzed file
197 - # ---------------------------------------------------------
198 - print("Saving Analyzed File...")
199 - output_file = f"Spring_{spring_name}_Analyzed.csv"
200 -
201 - df.to_csv(output_file, index=False)
202 -
203 -
204 - # ---------------------------------------------------------
205 - # STEP 11 - Create Peak Summary
206 - # ---------------------------------------------------------
207 -
208 - peak_summary = []
209 -
210 - print("Creating Cycle Numbers...")
211 - # Get all valid cycle numbers
212 - cycle_numbers = sorted(
213 - df[df["Cycle Number"] > 0]["Cycle Number"].unique()
214 - )
215 -
216 - for cycle in cycle_numbers:
217 -
218 - # Get only rows for this cycle that are above threshold
219 - cycle_data = df[
220 - (df["Cycle Number"] == cycle)
221 - &
222 - (df["Above Threshold"] == 1)
223 - ]
224 -
225 - # Skip empty cycles just in case
226 - if len(cycle_data) == 0:
227 - continue
228 -
229 - # Find row with maximum load
230 - peak_index = cycle_data["Load (N)"].idxmax()
231 -
232 - peak_row = df.loc[peak_index]
233 -
234 - peak_summary.append({
235 - "Cycle Number": cycle,
236 - "Test": peak_row["Test"],
237 - "Peak Load (N)": peak_row["Load (N)"],
238 - "Peak Extension (mm)": peak_row[extension_column],
239 - "Peak Time (s)": peak_row["Continuous Time (s)"],
240 - "Peak Row": peak_index
241 - })
242 -
243 - # ---------------------------------------------------------
244 - # STEP 12 - Create dataframe
245 - # ---------------------------------------------------------
246 -
247 - peak_df = pd.DataFrame(peak_summary)
248 -
249 - # ---------------------------------------------------------
250 - # STEP 13 - Save peak summary
251 - # ---------------------------------------------------------
252 - print("Saving peak summary...")
253 - peak_output_file = (
254 - f"Spring_{spring_name}_Peaks.csv"
255 - )
256 -
257 - peak_df.to_csv(
258 - peak_output_file,
259 - index=False
260 - )
261 -
262 - # Cycles per test count
263 - cycles_per_test = (
264 - peak_df.groupby("Test").size()
265 - )
266 -
267 - print("Cycles Per Test:")
268 -
269 - for test, count in cycles_per_test.items():
270 - print(f"Test {int(test)}: {count}")
271 -
272 - # ---------------------------------------------------------
273 - # STEP 14 - Report results
274 - # ---------------------------------------------------------
275 -
276 - total_cycles = int(
277 - df["Cycle Number"].max()
278 - )
279 -
280 - print("\nAnalysis Complete")
281 - print(
282 - f"Total Cycles Found: "
283 - f"{total_cycles}"
284 - )
285 -
286 - print(
287 - f"Total Peaks Found: "
288 - f"{len(peak_summary)}"
289 - )
290 -
291 - print(
292 - f"Analyzed File: "
293 - f"{output_file}"
294 - )
295 -
296 - print(
297 - f"Peak Summary File: "
298 - f"{peak_output_file}"
299 - )
300 -
1 + import pandas as pd
2 + import glob
3 + import os
4 +
5 +
6 + # ---------------------------------------------------------
7 + # STEP 1 - Ask user which spring to process
8 + # ---------------------------------------------------------
9 + spring_name = input("Enter spring number (example: 1040_01): ").strip()
10 +
11 + threshold = float(
12 + input(
13 + "Enter peak detection threshold (mm): "
14 + )
15 + )
16 +
17 +
18 + # ---------------------------------------------------------
19 + # STEP 2 - Find all matching test files
20 + # ---------------------------------------------------------
21 + file_pattern = f"Spring_{spring_name}_Test_*.csv"
22 +
23 + file_list = glob.glob(file_pattern)
24 +
25 + # Make sure files are processed in order
26 + file_list.sort()
27 +
28 + if len(file_list) == 0:
29 + print(f"No files found matching: {file_pattern}")
30 + exit()
31 +
32 +
33 + # ---------------------------------------------------------
34 + # STEP 3 - Variables used while combining files
35 + # ---------------------------------------------------------
36 + combined_data = []
37 +
38 + time_offset = 0
39 +
40 +
41 + # ---------------------------------------------------------
42 + # STEP 4 - Process each test file
43 + # ---------------------------------------------------------
44 + for test_number, file in enumerate(file_list, start=1):
45 +
46 + print(f"Processing: {file}")
47 +
48 + # Read CSV
49 + df = pd.read_csv(file)
50 +
51 + # Remove rows containing empty data
52 + df = df.dropna()
53 +
54 + # Time column name
55 + time_column = "Time (s)"
56 +
57 + # Create Test column
58 + df["Test"] = test_number
59 +
60 + # Create continuous time column
61 + df["Continuous Time (s)"] = df[time_column] + time_offset
62 +
63 + # Determine ending time for next test
64 + last_time = df[time_column].iloc[-1]
65 +
66 + time_offset += last_time
67 +
68 + # Store cleaned data
69 + combined_data.append(df)
70 +
71 +
72 + # ---------------------------------------------------------
73 + # STEP 5 - Combine all tests into one dataframe
74 + # ---------------------------------------------------------
75 + final_df = pd.concat(combined_data, ignore_index=True)
76 +
77 +
78 + # ---------------------------------------------------------
79 + # STEP 6 - Create output filename
80 + # ---------------------------------------------------------
81 + output_file = f"Spring_{spring_name}_Cleaned.csv"
82 +
83 +
84 + # ---------------------------------------------------------
85 + # STEP 7 - Save cleaned data
86 + # ---------------------------------------------------------
87 + final_df.to_csv(output_file, index=False)
88 +
89 + print()
90 + print(f"Finished Cleaning!")
91 + print(f"Output saved as: {output_file}")
92 +
93 +
94 + # ---------------------------------------------------------------------
95 + # ANALYZING SECTION
96 + # ---------------------------------------------------------------------
97 +
98 +
99 + # ---------------------------------------------------------
100 + # STEP 2 - Open cleaned file
101 + # ---------------------------------------------------------
102 +
103 + input_file = f"Spring_{spring_name}_Cleaned.csv"
104 +
105 + print(f"\nLoading {input_file}...")
106 +
107 + df = pd.read_csv(input_file)
108 +
109 +
110 + # ---------------------------------------------------------
111 + # STEP 3 - Define important column names
112 + # ---------------------------------------------------------
113 +
114 + extension_column = "Extension (mm)"
115 +
116 +
117 + # ---------------------------------------------------------
118 + # STEP 4 - Create Above Threshold flag
119 + #
120 + # 1 = Extension is above threshold
121 + # 0 = Extension is below threshold
122 + # ---------------------------------------------------------
123 + print("Creating Above Threshold...")
124 + df["Above Threshold"] = (
125 + df[extension_column] >= threshold
126 + ).astype(int)
127 +
128 +
129 + # ---------------------------------------------------------
130 + # STEP 5 - Create Previous Above Threshold column
131 + #
132 + # Shift the Above Threshold column down by one row
133 + # ---------------------------------------------------------
134 + print("Creating Previous Above Threshold...")
135 + df["Previous Above Threshold"] = (
136 + df["Above Threshold"]
137 + .shift(1)
138 + .fillna(0)
139 + .astype(int)
140 + )
141 +
142 +
143 + # ---------------------------------------------------------
144 + # STEP 6 - Create Rising Edge column
145 + #
146 + # Rising Edge occurs when:
147 + #
148 + # Previous = 0
149 + # Current = 1
150 + # ---------------------------------------------------------
151 + print("Creating Rising Edge...")
152 + df["Rising Edge"] = (
153 + (df["Above Threshold"] == 1)
154 + &
155 + (df["Previous Above Threshold"] == 0)
156 + ).astype(int)
157 +
158 +
159 + # ---------------------------------------------------------
160 + # STEP 7 - Create Falling Edge column
161 + #
162 + # Falling Edge occurs when:
163 + #
164 + # Previous = 1
165 + # Current = 0
166 + # ---------------------------------------------------------
167 + print("Creating Falling Edge...")
168 + df["Falling Edge"] = (
169 + (df["Above Threshold"] == 0)
170 + &
171 + (df["Previous Above Threshold"] == 1)
172 + ).astype(int)
173 +
174 +
175 + # ---------------------------------------------------------
176 + # STEP 8 - Create Cycle Number
177 + #
178 + # Every Rising Edge starts a new cycle.
179 + #
180 + # Example:
181 + #
182 + # Rising Edge:
183 + # 0 0 1 0 0 1 0
184 + #
185 + # Cycle Number:
186 + # 0 0 1 1 1 2 2
187 + # ---------------------------------------------------------
188 + print("Creating Cycle Number...")
189 + df["Cycle Number"] = (
190 + df["Rising Edge"]
191 + .cumsum()
192 + )
193 +
194 +
195 + # ---------------------------------------------------------
196 + # STEP 9 - Save analyzed file
197 + # ---------------------------------------------------------
198 + print("Saving Analyzed File...")
199 + output_file = f"Spring_{spring_name}_Analyzed.csv"
200 +
201 + df.to_csv(output_file, index=False)
202 +
203 +
204 + # ---------------------------------------------------------
205 + # STEP 11 - Create Peak Summary
206 + # ---------------------------------------------------------
207 +
208 + peak_summary = []
209 +
210 + print("Creating Cycle Numbers...")
211 + # Get all valid cycle numbers
212 + cycle_numbers = sorted(
213 + df[df["Cycle Number"] > 0]["Cycle Number"].unique()
214 + )
215 +
216 + for cycle in cycle_numbers:
217 +
218 + # Get only rows for this cycle that are above threshold
219 + cycle_data = df[
220 + (df["Cycle Number"] == cycle)
221 + &
222 + (df["Above Threshold"] == 1)
223 + ]
224 +
225 + # Skip empty cycles just in case
226 + if len(cycle_data) == 0:
227 + continue
228 +
229 + # Find row with maximum load
230 + peak_index = cycle_data["Load (N)"].idxmax()
231 +
232 + peak_row = df.loc[peak_index]
233 +
234 + peak_summary.append({
235 + "Cycle Number": cycle,
236 + "Test": peak_row["Test"],
237 + "Peak Load (N)": peak_row["Load (N)"],
238 + "Peak Extension (mm)": peak_row[extension_column],
239 + "Peak Time (s)": peak_row["Continuous Time (s)"],
240 + "Peak Row": peak_index
241 + })
242 +
243 + # ---------------------------------------------------------
244 + # STEP 12 - Create dataframe
245 + # ---------------------------------------------------------
246 +
247 + peak_df = pd.DataFrame(peak_summary)
248 +
249 + # ---------------------------------------------------------
250 + # STEP 13 - Save peak summary
251 + # ---------------------------------------------------------
252 + print("Saving peak summary...")
253 + peak_output_file = (
254 + f"Spring_{spring_name}_Peaks.csv"
255 + )
256 +
257 + peak_df.to_csv(
258 + peak_output_file,
259 + index=False
260 + )
261 +
262 + # Cycles per test count
263 + cycles_per_test = (
264 + peak_df.groupby("Test").size()
265 + )
266 +
267 + print("Cycles Per Test:")
268 +
269 + for test, count in cycles_per_test.items():
270 + print(f"Test {int(test)}: {count}")
271 +
272 + # ---------------------------------------------------------
273 + # STEP 14 - Report results
274 + # ---------------------------------------------------------
275 +
276 + total_cycles = int(
277 + df["Cycle Number"].max()
278 + )
279 +
280 + print("\nAnalysis Complete")
281 + print(
282 + f"Total Cycles Found: "
283 + f"{total_cycles}"
284 + )
285 +
286 + print(
287 + f"Total Peaks Found: "
288 + f"{len(peak_summary)}"
289 + )
290 +
291 + print(
292 + f"Analyzed File: "
293 + f"{output_file}"
294 + )
295 +
296 + print(
297 + f"Peak Summary File: "
298 + f"{peak_output_file}"
299 + )
300 +
301 301 input("\nPress Enter to exit")

wschrab revised this gist 1 week ago. Go to revision

2 files changed, 301 insertions

SpringCleanAndAnalyze.exe

Binary file changes are not shown

SpringCleanAndAnalyze.py(file created)

@@ -0,0 +1,301 @@
1 + import pandas as pd
2 + import glob
3 + import os
4 +
5 +
6 + # ---------------------------------------------------------
7 + # STEP 1 - Ask user which spring to process
8 + # ---------------------------------------------------------
9 + spring_name = input("Enter spring number (example: 1040_01): ").strip()
10 +
11 + threshold = float(
12 + input(
13 + "Enter peak detection threshold (mm): "
14 + )
15 + )
16 +
17 +
18 + # ---------------------------------------------------------
19 + # STEP 2 - Find all matching test files
20 + # ---------------------------------------------------------
21 + file_pattern = f"Spring_{spring_name}_Test_*.csv"
22 +
23 + file_list = glob.glob(file_pattern)
24 +
25 + # Make sure files are processed in order
26 + file_list.sort()
27 +
28 + if len(file_list) == 0:
29 + print(f"No files found matching: {file_pattern}")
30 + exit()
31 +
32 +
33 + # ---------------------------------------------------------
34 + # STEP 3 - Variables used while combining files
35 + # ---------------------------------------------------------
36 + combined_data = []
37 +
38 + time_offset = 0
39 +
40 +
41 + # ---------------------------------------------------------
42 + # STEP 4 - Process each test file
43 + # ---------------------------------------------------------
44 + for test_number, file in enumerate(file_list, start=1):
45 +
46 + print(f"Processing: {file}")
47 +
48 + # Read CSV
49 + df = pd.read_csv(file)
50 +
51 + # Remove rows containing empty data
52 + df = df.dropna()
53 +
54 + # Time column name
55 + time_column = "Time (s)"
56 +
57 + # Create Test column
58 + df["Test"] = test_number
59 +
60 + # Create continuous time column
61 + df["Continuous Time (s)"] = df[time_column] + time_offset
62 +
63 + # Determine ending time for next test
64 + last_time = df[time_column].iloc[-1]
65 +
66 + time_offset += last_time
67 +
68 + # Store cleaned data
69 + combined_data.append(df)
70 +
71 +
72 + # ---------------------------------------------------------
73 + # STEP 5 - Combine all tests into one dataframe
74 + # ---------------------------------------------------------
75 + final_df = pd.concat(combined_data, ignore_index=True)
76 +
77 +
78 + # ---------------------------------------------------------
79 + # STEP 6 - Create output filename
80 + # ---------------------------------------------------------
81 + output_file = f"Spring_{spring_name}_Cleaned.csv"
82 +
83 +
84 + # ---------------------------------------------------------
85 + # STEP 7 - Save cleaned data
86 + # ---------------------------------------------------------
87 + final_df.to_csv(output_file, index=False)
88 +
89 + print()
90 + print(f"Finished Cleaning!")
91 + print(f"Output saved as: {output_file}")
92 +
93 +
94 + # ---------------------------------------------------------------------
95 + # ANALYZING SECTION
96 + # ---------------------------------------------------------------------
97 +
98 +
99 + # ---------------------------------------------------------
100 + # STEP 2 - Open cleaned file
101 + # ---------------------------------------------------------
102 +
103 + input_file = f"Spring_{spring_name}_Cleaned.csv"
104 +
105 + print(f"\nLoading {input_file}...")
106 +
107 + df = pd.read_csv(input_file)
108 +
109 +
110 + # ---------------------------------------------------------
111 + # STEP 3 - Define important column names
112 + # ---------------------------------------------------------
113 +
114 + extension_column = "Extension (mm)"
115 +
116 +
117 + # ---------------------------------------------------------
118 + # STEP 4 - Create Above Threshold flag
119 + #
120 + # 1 = Extension is above threshold
121 + # 0 = Extension is below threshold
122 + # ---------------------------------------------------------
123 + print("Creating Above Threshold...")
124 + df["Above Threshold"] = (
125 + df[extension_column] >= threshold
126 + ).astype(int)
127 +
128 +
129 + # ---------------------------------------------------------
130 + # STEP 5 - Create Previous Above Threshold column
131 + #
132 + # Shift the Above Threshold column down by one row
133 + # ---------------------------------------------------------
134 + print("Creating Previous Above Threshold...")
135 + df["Previous Above Threshold"] = (
136 + df["Above Threshold"]
137 + .shift(1)
138 + .fillna(0)
139 + .astype(int)
140 + )
141 +
142 +
143 + # ---------------------------------------------------------
144 + # STEP 6 - Create Rising Edge column
145 + #
146 + # Rising Edge occurs when:
147 + #
148 + # Previous = 0
149 + # Current = 1
150 + # ---------------------------------------------------------
151 + print("Creating Rising Edge...")
152 + df["Rising Edge"] = (
153 + (df["Above Threshold"] == 1)
154 + &
155 + (df["Previous Above Threshold"] == 0)
156 + ).astype(int)
157 +
158 +
159 + # ---------------------------------------------------------
160 + # STEP 7 - Create Falling Edge column
161 + #
162 + # Falling Edge occurs when:
163 + #
164 + # Previous = 1
165 + # Current = 0
166 + # ---------------------------------------------------------
167 + print("Creating Falling Edge...")
168 + df["Falling Edge"] = (
169 + (df["Above Threshold"] == 0)
170 + &
171 + (df["Previous Above Threshold"] == 1)
172 + ).astype(int)
173 +
174 +
175 + # ---------------------------------------------------------
176 + # STEP 8 - Create Cycle Number
177 + #
178 + # Every Rising Edge starts a new cycle.
179 + #
180 + # Example:
181 + #
182 + # Rising Edge:
183 + # 0 0 1 0 0 1 0
184 + #
185 + # Cycle Number:
186 + # 0 0 1 1 1 2 2
187 + # ---------------------------------------------------------
188 + print("Creating Cycle Number...")
189 + df["Cycle Number"] = (
190 + df["Rising Edge"]
191 + .cumsum()
192 + )
193 +
194 +
195 + # ---------------------------------------------------------
196 + # STEP 9 - Save analyzed file
197 + # ---------------------------------------------------------
198 + print("Saving Analyzed File...")
199 + output_file = f"Spring_{spring_name}_Analyzed.csv"
200 +
201 + df.to_csv(output_file, index=False)
202 +
203 +
204 + # ---------------------------------------------------------
205 + # STEP 11 - Create Peak Summary
206 + # ---------------------------------------------------------
207 +
208 + peak_summary = []
209 +
210 + print("Creating Cycle Numbers...")
211 + # Get all valid cycle numbers
212 + cycle_numbers = sorted(
213 + df[df["Cycle Number"] > 0]["Cycle Number"].unique()
214 + )
215 +
216 + for cycle in cycle_numbers:
217 +
218 + # Get only rows for this cycle that are above threshold
219 + cycle_data = df[
220 + (df["Cycle Number"] == cycle)
221 + &
222 + (df["Above Threshold"] == 1)
223 + ]
224 +
225 + # Skip empty cycles just in case
226 + if len(cycle_data) == 0:
227 + continue
228 +
229 + # Find row with maximum load
230 + peak_index = cycle_data["Load (N)"].idxmax()
231 +
232 + peak_row = df.loc[peak_index]
233 +
234 + peak_summary.append({
235 + "Cycle Number": cycle,
236 + "Test": peak_row["Test"],
237 + "Peak Load (N)": peak_row["Load (N)"],
238 + "Peak Extension (mm)": peak_row[extension_column],
239 + "Peak Time (s)": peak_row["Continuous Time (s)"],
240 + "Peak Row": peak_index
241 + })
242 +
243 + # ---------------------------------------------------------
244 + # STEP 12 - Create dataframe
245 + # ---------------------------------------------------------
246 +
247 + peak_df = pd.DataFrame(peak_summary)
248 +
249 + # ---------------------------------------------------------
250 + # STEP 13 - Save peak summary
251 + # ---------------------------------------------------------
252 + print("Saving peak summary...")
253 + peak_output_file = (
254 + f"Spring_{spring_name}_Peaks.csv"
255 + )
256 +
257 + peak_df.to_csv(
258 + peak_output_file,
259 + index=False
260 + )
261 +
262 + # Cycles per test count
263 + cycles_per_test = (
264 + peak_df.groupby("Test").size()
265 + )
266 +
267 + print("Cycles Per Test:")
268 +
269 + for test, count in cycles_per_test.items():
270 + print(f"Test {int(test)}: {count}")
271 +
272 + # ---------------------------------------------------------
273 + # STEP 14 - Report results
274 + # ---------------------------------------------------------
275 +
276 + total_cycles = int(
277 + df["Cycle Number"].max()
278 + )
279 +
280 + print("\nAnalysis Complete")
281 + print(
282 + f"Total Cycles Found: "
283 + f"{total_cycles}"
284 + )
285 +
286 + print(
287 + f"Total Peaks Found: "
288 + f"{len(peak_summary)}"
289 + )
290 +
291 + print(
292 + f"Analyzed File: "
293 + f"{output_file}"
294 + )
295 +
296 + print(
297 + f"Peak Summary File: "
298 + f"{peak_output_file}"
299 + )
300 +
301 + input("\nPress Enter to exit")

wschrab revised this gist 1 week ago. Go to revision

1 file changed, 0 insertions, 0 deletions

SpringCleanAndAnalyze.exe(file created)

Binary file changes are not shown

wschrab revised this gist 1 week ago. Go to revision

1 file changed, 10 insertions, 19 deletions

SpringAnalyze.py

@@ -40,7 +40,7 @@ extension_column = "Extension from Preload (mm)"
40 40 # 1 = Extension is above threshold
41 41 # 0 = Extension is below threshold
42 42 # ---------------------------------------------------------
43 -
43 + print("Creating Above Threshold...")
44 44 df["Above Threshold"] = (
45 45 df[extension_column] >= threshold
46 46 ).astype(int)
@@ -51,7 +51,7 @@ df["Above Threshold"] = (
51 51 #
52 52 # Shift the Above Threshold column down by one row
53 53 # ---------------------------------------------------------
54 -
54 + print("Creating Previous Above Threshold...")
55 55 df["Previous Above Threshold"] = (
56 56 df["Above Threshold"]
57 57 .shift(1)
@@ -68,7 +68,7 @@ df["Previous Above Threshold"] = (
68 68 # Previous = 0
69 69 # Current = 1
70 70 # ---------------------------------------------------------
71 -
71 + print("Creating Rising Edge...")
72 72 df["Rising Edge"] = (
73 73 (df["Above Threshold"] == 1)
74 74 &
@@ -84,7 +84,7 @@ df["Rising Edge"] = (
84 84 # Previous = 1
85 85 # Current = 0
86 86 # ---------------------------------------------------------
87 -
87 + print("Creating Falling Edge...")
88 88 df["Falling Edge"] = (
89 89 (df["Above Threshold"] == 0)
90 90 &
@@ -105,7 +105,7 @@ df["Falling Edge"] = (
105 105 # Cycle Number:
106 106 # 0 0 1 1 1 2 2
107 107 # ---------------------------------------------------------
108 -
108 + print("Creating Cycle Number...")
109 109 df["Cycle Number"] = (
110 110 df["Rising Edge"]
111 111 .cumsum()
@@ -115,28 +115,19 @@ df["Cycle Number"] = (
115 115 # ---------------------------------------------------------
116 116 # STEP 9 - Save analyzed file
117 117 # ---------------------------------------------------------
118 -
118 + print("Saving Analyzed File...")
119 119 output_file = f"Spring_{spring_name}_Analyzed.csv"
120 120
121 121 df.to_csv(output_file, index=False)
122 122
123 123
124 - # ---------------------------------------------------------
125 - # STEP 10 - Report results
126 - # ---------------------------------------------------------
127 -
128 - total_cycles = int(df["Cycle Number"].max())
129 -
130 - print("\nAnalysis Complete")
131 - print(f"Total Cycles Found: {total_cycles}")
132 - print(f"Output File: {output_file}")
133 -
134 124 # ---------------------------------------------------------
135 125 # STEP 11 - Create Peak Summary
136 126 # ---------------------------------------------------------
137 127
138 128 peak_summary = []
139 129
130 + print("Creating Cycle Numbers...")
140 131 # Get all valid cycle numbers
141 132 cycle_numbers = sorted(
142 133 df[df["Cycle Number"] > 0]["Cycle Number"].unique()
@@ -156,14 +147,14 @@ for cycle in cycle_numbers:
156 147 continue
157 148
158 149 # Find row with maximum load
159 - peak_index = cycle_data["Load (lb)"].idxmax()
150 + peak_index = cycle_data["Load (N)"].idxmax()
160 151
161 152 peak_row = df.loc[peak_index]
162 153
163 154 peak_summary.append({
164 155 "Cycle Number": cycle,
165 156 "Test": peak_row["Test"],
166 - "Peak Load (lb)": peak_row["Load (lb)"],
157 + "Peak Load (N)": peak_row["Load (N)"],
167 158 "Peak Extension (mm)": peak_row["Extension from Preload (mm)"],
168 159 "Peak Time (s)": peak_row["Continuous Time (s)"],
169 160 "Peak Row": peak_index
@@ -178,7 +169,7 @@ peak_df = pd.DataFrame(peak_summary)
178 169 # ---------------------------------------------------------
179 170 # STEP 13 - Save peak summary
180 171 # ---------------------------------------------------------
181 -
172 + print("Saving peak summary...")
182 173 peak_output_file = (
183 174 f"Spring_{spring_name}_Peaks.csv"
184 175 )
Newer Older