import pandas as pd # --------------------------------------------------------- # STEP 1 - Get user inputs # --------------------------------------------------------- spring_name = input( "Enter spring number (example: 1040_02): " ).strip() threshold = float( input( "Enter peak detection threshold (mm): " ) ) # --------------------------------------------------------- # STEP 2 - Open cleaned file # --------------------------------------------------------- input_file = f"Spring_{spring_name}_Cleaned.csv" print(f"\nLoading {input_file}...") df = pd.read_csv(input_file) # --------------------------------------------------------- # STEP 3 - Define important column names # --------------------------------------------------------- extension_column = "Extension from Preload (mm)" # --------------------------------------------------------- # STEP 4 - Create Above Threshold flag # # 1 = Extension is above threshold # 0 = Extension is below threshold # --------------------------------------------------------- df["Above Threshold"] = ( df[extension_column] >= threshold ).astype(int) # --------------------------------------------------------- # STEP 5 - Create Previous Above Threshold column # # Shift the Above Threshold column down by one row # --------------------------------------------------------- df["Previous Above Threshold"] = ( df["Above Threshold"] .shift(1) .fillna(0) .astype(int) ) # --------------------------------------------------------- # STEP 6 - Create Rising Edge column # # Rising Edge occurs when: # # Previous = 0 # Current = 1 # --------------------------------------------------------- df["Rising Edge"] = ( (df["Above Threshold"] == 1) & (df["Previous Above Threshold"] == 0) ).astype(int) # --------------------------------------------------------- # STEP 7 - Create Falling Edge column # # Falling Edge occurs when: # # Previous = 1 # Current = 0 # --------------------------------------------------------- df["Falling Edge"] = ( (df["Above Threshold"] == 0) & (df["Previous Above Threshold"] == 1) ).astype(int) # --------------------------------------------------------- # STEP 8 - Create Cycle Number # # Every Rising Edge starts a new cycle. # # Example: # # Rising Edge: # 0 0 1 0 0 1 0 # # Cycle Number: # 0 0 1 1 1 2 2 # --------------------------------------------------------- df["Cycle Number"] = ( df["Rising Edge"] .cumsum() ) # --------------------------------------------------------- # STEP 9 - Save analyzed file # --------------------------------------------------------- output_file = f"Spring_{spring_name}_Analyzed.csv" df.to_csv(output_file, index=False) # --------------------------------------------------------- # STEP 10 - Report results # --------------------------------------------------------- total_cycles = int(df["Cycle Number"].max()) print("\nAnalysis Complete") print(f"Total Cycles Found: {total_cycles}") print(f"Output File: {output_file}") # --------------------------------------------------------- # STEP 11 - Create Peak Summary # --------------------------------------------------------- peak_summary = [] # Get all valid cycle numbers cycle_numbers = sorted( df[df["Cycle Number"] > 0]["Cycle Number"].unique() ) for cycle in cycle_numbers: # Get only rows for this cycle that are above threshold cycle_data = df[ (df["Cycle Number"] == cycle) & (df["Above Threshold"] == 1) ] # Skip empty cycles just in case if len(cycle_data) == 0: continue # Find row with maximum load peak_index = cycle_data["Load (lb)"].idxmax() peak_row = df.loc[peak_index] peak_summary.append({ "Cycle Number": cycle, "Test": peak_row["Test"], "Peak Load (lb)": peak_row["Load (lb)"], "Peak Extension (mm)": peak_row["Extension from Preload (mm)"], "Peak Time (s)": peak_row["Continuous Time (s)"], "Peak Row": peak_index })