SpringAnalyze.py
· 3.0 KiB · Python
Raw
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}")
| 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}") |
SpringClean.py
· 2.3 KiB · Python
Raw
import pandas as pd
import glob
import os
# ---------------------------------------------------------
# STEP 1 - Ask user which spring to process
# ---------------------------------------------------------
spring_name = input("Enter spring number (example: 1040_01): ").strip()
# ---------------------------------------------------------
# STEP 2 - Find all matching test files
# ---------------------------------------------------------
file_pattern = f"Spring_{spring_name}_Test_*.csv"
file_list = glob.glob(file_pattern)
# Make sure files are processed in order
file_list.sort()
if len(file_list) == 0:
print(f"No files found matching: {file_pattern}")
exit()
# ---------------------------------------------------------
# STEP 3 - Variables used while combining files
# ---------------------------------------------------------
combined_data = []
time_offset = 0
# ---------------------------------------------------------
# STEP 4 - Process each test file
# ---------------------------------------------------------
for test_number, file in enumerate(file_list, start=1):
print(f"Processing: {file}")
# Read CSV
df = pd.read_csv(file)
# Remove rows containing empty data
df = df.dropna()
# Time column name
time_column = "Time (s)"
# Create Test column
df["Test"] = test_number
# Create continuous time column
df["Continuous Time (s)"] = df[time_column] + time_offset
# Determine ending time for next test
last_time = df[time_column].iloc[-1]
time_offset += last_time
# Store cleaned data
combined_data.append(df)
# ---------------------------------------------------------
# STEP 5 - Combine all tests into one dataframe
# ---------------------------------------------------------
final_df = pd.concat(combined_data, ignore_index=True)
# ---------------------------------------------------------
# STEP 6 - Create output filename
# ---------------------------------------------------------
output_file = f"Spring_{spring_name}_Cleaned.csv"
# ---------------------------------------------------------
# STEP 7 - Save cleaned data
# ---------------------------------------------------------
final_df.to_csv(output_file, index=False)
print()
print(f"Finished!")
print(f"Output saved as: {output_file}")
| 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}") |