150 lines
4.8 KiB
Python
150 lines
4.8 KiB
Python
import pickle
|
|
import shutil
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
from pointcloudset import Dataset
|
|
|
|
# define data paths
|
|
all_data_path = Path("/home/fedex/mt/data/subter")
|
|
output_path = Path("/home/fedex/mt/plots/data_particles_near_sensor_anomalies")
|
|
datetime_folder_name = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
|
|
|
latest_folder_path = output_path / "latest"
|
|
archive_folder_path = output_path / "archive"
|
|
output_datetime_path = output_path / datetime_folder_name
|
|
|
|
# if output does not exist, create it
|
|
output_path.mkdir(exist_ok=True, parents=True)
|
|
output_datetime_path.mkdir(exist_ok=True, parents=True)
|
|
latest_folder_path.mkdir(exist_ok=True, parents=True)
|
|
archive_folder_path.mkdir(exist_ok=True, parents=True)
|
|
|
|
data_resolution = 32 * 2048
|
|
|
|
# find all bag files and sort them correctly by name
|
|
normal_experiment_paths, anomaly_experiment_paths = [], []
|
|
for bag_file_path in all_data_path.iterdir():
|
|
if bag_file_path.suffix != ".bag":
|
|
continue
|
|
if "smoke" in bag_file_path.name:
|
|
anomaly_experiment_paths.append(bag_file_path)
|
|
else:
|
|
normal_experiment_paths.append(bag_file_path)
|
|
|
|
|
|
def plot_timeline_comparison(
|
|
anomaly_experiment_paths, title, range_threshold, num_bins=50
|
|
):
|
|
"""Plot near-sensor measurements percentage over normalized timeline for moving anomaly experiments"""
|
|
# Sort experiments by filesize first (to match original processing order)
|
|
anomaly_experiment_paths = sorted(
|
|
anomaly_experiment_paths, key=lambda path: path.stat().st_size
|
|
)
|
|
|
|
# Filter out stationary experiments
|
|
moving_exp_indices = [
|
|
i
|
|
for i, path in enumerate(anomaly_experiment_paths)
|
|
if "stationary" not in path.name
|
|
]
|
|
moving_anomaly_paths = [anomaly_experiment_paths[i] for i in moving_exp_indices]
|
|
|
|
# Try to load cached data
|
|
cache_path = (
|
|
Path("/home/fedex/mt/plots/data_particles_near_sensor")
|
|
/ f"particles_near_sensor_counts_{range_threshold}.pkl"
|
|
)
|
|
if not cache_path.exists():
|
|
print(
|
|
f"No cached data found for range threshold {range_threshold}. Please run the original script first."
|
|
)
|
|
return
|
|
|
|
with open(cache_path, "rb") as file:
|
|
_, particles_near_sensor_anomaly = pickle.load(file)
|
|
|
|
# Get data for moving experiments only (using original indices)
|
|
moving_anomaly_data = [particles_near_sensor_anomaly[i] for i in moving_exp_indices]
|
|
|
|
# Create figure
|
|
plt.figure(figsize=(12, 6))
|
|
|
|
# Plot each experiment's timeline
|
|
for i, exp_data in enumerate(moving_anomaly_data):
|
|
# Convert to percentage
|
|
percentages = np.array(exp_data) / data_resolution * 100
|
|
|
|
# Create normalized timeline bins
|
|
exp_len = len(percentages)
|
|
bins = np.linspace(0, exp_len - 1, num_bins)
|
|
binned_data = np.zeros(num_bins)
|
|
|
|
# Bin the data
|
|
for bin_idx in range(num_bins):
|
|
if bin_idx == num_bins - 1:
|
|
start_idx = int(bins[bin_idx])
|
|
end_idx = exp_len
|
|
else:
|
|
start_idx = int(bins[bin_idx])
|
|
end_idx = int(bins[bin_idx + 1])
|
|
|
|
binned_data[bin_idx] = np.mean(percentages[start_idx:end_idx])
|
|
|
|
# Plot with slight transparency to show overlaps
|
|
plt.plot(
|
|
range(num_bins),
|
|
binned_data,
|
|
alpha=0.6,
|
|
label=f"Experiment {moving_anomaly_paths[i].stem}",
|
|
)
|
|
|
|
plt.title(f"{title}\n(Range Threshold: {range_threshold / 1000:.1f}m)")
|
|
plt.xlabel("Normalized Timeline")
|
|
# Add percentage ticks on x-axis
|
|
plt.xticks(
|
|
np.linspace(0, num_bins - 1, 5), [f"{x:.0f}%" for x in np.linspace(0, 100, 5)]
|
|
)
|
|
plt.ylabel("Near-Sensor Measurements (%)")
|
|
plt.grid(True, alpha=0.3)
|
|
plt.legend()
|
|
plt.tight_layout()
|
|
|
|
# Save the plot
|
|
plt.savefig(
|
|
output_datetime_path
|
|
/ f"near_sensor_measurements_timeline_{range_threshold}.png",
|
|
dpi=150,
|
|
)
|
|
plt.close()
|
|
|
|
|
|
# Generate timeline comparison plots for each range threshold
|
|
range_thresholds = [500, 750, 1000, 1250, 1500]
|
|
for rt in range_thresholds:
|
|
print(f"Processing range threshold {rt}...")
|
|
plot_timeline_comparison(
|
|
anomaly_experiment_paths,
|
|
"Near-Sensor Lidar Measurements Over Time\n(Moving Anomaly Experiments Only)",
|
|
rt,
|
|
)
|
|
|
|
# delete current latest folder
|
|
shutil.rmtree(latest_folder_path, ignore_errors=True)
|
|
|
|
# create new latest folder
|
|
latest_folder_path.mkdir(exist_ok=True, parents=True)
|
|
|
|
# copy contents of output folder to the latest folder
|
|
for file in output_datetime_path.iterdir():
|
|
shutil.copy2(file, latest_folder_path)
|
|
|
|
# copy this python script to preserve the code used
|
|
shutil.copy2(__file__, output_datetime_path)
|
|
shutil.copy2(__file__, latest_folder_path)
|
|
|
|
# move output date folder to archive
|
|
shutil.move(output_datetime_path, archive_folder_path)
|