Last active 4 days ago

wschrab revised this gist 1 week ago. Go to revision

1 file changed, 44 insertions, 1 deletion

SpringAnalyze.py

@@ -167,4 +167,47 @@ for cycle in cycle_numbers:
167 167 "Peak Extension (mm)": peak_row["Extension from Preload (mm)"],
168 168 "Peak Time (s)": peak_row["Continuous Time (s)"],
169 169 "Peak Row": peak_index
170 - })
170 + })
171 +
172 + # ---------------------------------------------------------
173 + # STEP 12 - Create dataframe
174 + # ---------------------------------------------------------
175 +
176 + peak_df = pd.DataFrame(peak_summary)
177 +
178 + # ---------------------------------------------------------
179 + # STEP 13 - Save peak summary
180 + # ---------------------------------------------------------
181 +
182 + peak_output_file = (
183 + f"Spring_{spring_name}_Peaks.csv"
184 + )
185 +
186 + peak_df.to_csv(
187 + peak_output_file,
188 + index=False
189 + )
190 +
191 + # ---------------------------------------------------------
192 + # STEP 14 - Report results
193 + # ---------------------------------------------------------
194 +
195 + total_cycles = int(
196 + df["Cycle Number"].max()
197 + )
198 +
199 + print("\nAnalysis Complete")
200 + print(
201 + f"Total Cycles Found: "
202 + f"{total_cycles}"
203 + )
204 +
205 + print(
206 + f"Analyzed File: "
207 + f"{output_file}"
208 + )
209 +
210 + print(
211 + f"Peak Summary File: "
212 + f"{peak_output_file}"
213 + )

wschrab revised this gist 1 week ago. Go to revision

1 file changed, 39 insertions, 1 deletion

SpringAnalyze.py

@@ -129,4 +129,42 @@ total_cycles = int(df["Cycle Number"].max())
129 129
130 130 print("\nAnalysis Complete")
131 131 print(f"Total Cycles Found: {total_cycles}")
132 - print(f"Output File: {output_file}")
132 + print(f"Output File: {output_file}")
133 +
134 + # ---------------------------------------------------------
135 + # STEP 11 - Create Peak Summary
136 + # ---------------------------------------------------------
137 +
138 + peak_summary = []
139 +
140 + # Get all valid cycle numbers
141 + cycle_numbers = sorted(
142 + df[df["Cycle Number"] > 0]["Cycle Number"].unique()
143 + )
144 +
145 + for cycle in cycle_numbers:
146 +
147 + # Get only rows for this cycle that are above threshold
148 + cycle_data = df[
149 + (df["Cycle Number"] == cycle)
150 + &
151 + (df["Above Threshold"] == 1)
152 + ]
153 +
154 + # Skip empty cycles just in case
155 + if len(cycle_data) == 0:
156 + continue
157 +
158 + # Find row with maximum load
159 + peak_index = cycle_data["Load (lb)"].idxmax()
160 +
161 + peak_row = df.loc[peak_index]
162 +
163 + peak_summary.append({
164 + "Cycle Number": cycle,
165 + "Test": peak_row["Test"],
166 + "Peak Load (lb)": peak_row["Load (lb)"],
167 + "Peak Extension (mm)": peak_row["Extension from Preload (mm)"],
168 + "Peak Time (s)": peak_row["Continuous Time (s)"],
169 + "Peak Row": peak_index
170 + })

wschrab revised this gist 1 week ago. Go to revision

1 file changed, 132 insertions

SpringAnalyze.py(file created)

@@ -0,0 +1,132 @@
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 +
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 +
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 +
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 +
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 +
109 + df["Cycle Number"] = (
110 + df["Rising Edge"]
111 + .cumsum()
112 + )
113 +
114 +
115 + # ---------------------------------------------------------
116 + # STEP 9 - Save analyzed file
117 + # ---------------------------------------------------------
118 +
119 + output_file = f"Spring_{spring_name}_Analyzed.csv"
120 +
121 + df.to_csv(output_file, index=False)
122 +
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}")

wschrab revised this gist 1 week ago. Go to revision

1 file changed, 8 insertions, 16 deletions

SpringClean.py

@@ -6,13 +6,13 @@ import os
6 6 # ---------------------------------------------------------
7 7 # STEP 1 - Ask user which spring to process
8 8 # ---------------------------------------------------------
9 - spring_name = input("Enter spring number (example: Spring_1040_01): ").strip()
9 + spring_name = input("Enter spring number (example: 1040_01): ").strip()
10 10
11 11
12 12 # ---------------------------------------------------------
13 13 # STEP 2 - Find all matching test files
14 14 # ---------------------------------------------------------
15 - file_pattern = f"{spring_name}_Test_*.csv"
15 + file_pattern = f"Spring_{spring_name}_Test_*.csv"
16 16
17 17 file_list = glob.glob(file_pattern)
18 18
@@ -35,34 +35,26 @@ time_offset = 0
35 35 # ---------------------------------------------------------
36 36 # STEP 4 - Process each test file
37 37 # ---------------------------------------------------------
38 - for file in file_list:
38 + for test_number, file in enumerate(file_list, start=1):
39 39
40 40 print(f"Processing: {file}")
41 41
42 42 # Read CSV
43 43 df = pd.read_csv(file)
44 44
45 - # -----------------------------------------------------
46 45 # Remove rows containing empty data
47 - # -----------------------------------------------------
48 46 df = df.dropna()
49 47
50 - # -----------------------------------------------------
51 - # Find the time column
52 - #
53 - # Change this name if your machine uses a different
54 - # column title.
55 - # -----------------------------------------------------
48 + # Time column name
56 49 time_column = "Time (s)"
57 50
58 - # -----------------------------------------------------
51 + # Create Test column
52 + df["Test"] = test_number
53 +
59 54 # Create continuous time column
60 - # -----------------------------------------------------
61 55 df["Continuous Time (s)"] = df[time_column] + time_offset
62 56
63 - # -----------------------------------------------------
64 57 # Determine ending time for next test
65 - # -----------------------------------------------------
66 58 last_time = df[time_column].iloc[-1]
67 59
68 60 time_offset += last_time
@@ -80,7 +72,7 @@ final_df = pd.concat(combined_data, ignore_index=True)
80 72 # ---------------------------------------------------------
81 73 # STEP 6 - Create output filename
82 74 # ---------------------------------------------------------
83 - output_file = f"{spring_name}_Cleaned.csv"
75 + output_file = f"Spring_{spring_name}_Cleaned.csv"
84 76
85 77
86 78 # ---------------------------------------------------------

wschrab revised this gist 1 week ago. Go to revision

1 file changed, 93 insertions

SpringClean.py(file created)

@@ -0,0 +1,93 @@
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: Spring_1040_01): ").strip()
10 +
11 +
12 + # ---------------------------------------------------------
13 + # STEP 2 - Find all matching test files
14 + # ---------------------------------------------------------
15 + file_pattern = f"{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 file in file_list:
39 +
40 + print(f"Processing: {file}")
41 +
42 + # Read CSV
43 + df = pd.read_csv(file)
44 +
45 + # -----------------------------------------------------
46 + # Remove rows containing empty data
47 + # -----------------------------------------------------
48 + df = df.dropna()
49 +
50 + # -----------------------------------------------------
51 + # Find the time column
52 + #
53 + # Change this name if your machine uses a different
54 + # column title.
55 + # -----------------------------------------------------
56 + time_column = "Time (s)"
57 +
58 + # -----------------------------------------------------
59 + # Create continuous time column
60 + # -----------------------------------------------------
61 + df["Continuous Time (s)"] = df[time_column] + time_offset
62 +
63 + # -----------------------------------------------------
64 + # Determine ending time for next test
65 + # -----------------------------------------------------
66 + last_time = df[time_column].iloc[-1]
67 +
68 + time_offset += last_time
69 +
70 + # Store cleaned data
71 + combined_data.append(df)
72 +
73 +
74 + # ---------------------------------------------------------
75 + # STEP 5 - Combine all tests into one dataframe
76 + # ---------------------------------------------------------
77 + final_df = pd.concat(combined_data, ignore_index=True)
78 +
79 +
80 + # ---------------------------------------------------------
81 + # STEP 6 - Create output filename
82 + # ---------------------------------------------------------
83 + output_file = f"{spring_name}_Cleaned.csv"
84 +
85 +
86 + # ---------------------------------------------------------
87 + # STEP 7 - Save cleaned data
88 + # ---------------------------------------------------------
89 + final_df.to_csv(output_file, index=False)
90 +
91 + print()
92 + print(f"Finished!")
93 + print(f"Output saved as: {output_file}")
Newer Older