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}")