def cracked_concrete_plots(c: Config): """Contour plots of cracked concrete scenarios.""" response_type = ResponseType.YTranslation # 10 x 10 grid of points on the bridge deck where to record fem. points = [ Point(x=x, y=0, z=z) for x, z in itertools.product( np.linspace(c.bridge.x_min, c.bridge.x_max, 10), np.linspace(c.bridge.z_min, c.bridge.z_max, 10), ) ] # Create empty traffic array and collect fem. response_array = responses_to_traffic_array( c=c, traffic_array=load_normal_traffic_array(c)[0], response_type=response_type, bridge_scenario=cracked_scenario, points=points, sim_runner=OSRunner, ) for t in range(len(response_array)): top_view_bridge(c.bridge, abutments=True, piers=True) responses = Responses.from_responses( response_type=response_type, responses=[(response_array[t][p], point) for p, point in enumerate(points)], ) plot_contour_deck(c=c, responses=responses, center_norm=True) plt.title("Cracked Concrete") plt.savefig(c.get_image_path("cracked-scenario", f"cracked-time-{t}")) plt.close()
def pairwise_cluster(c: Config, load: bool): """Cluster pairwise maps from healthy and damaged scenarios.""" features_path = c.get_data_path("features", "pairwise-cluster", bridge=False) if not load: normal_traffic_array, _ = load_normal_traffic_array(c=c, mins=24) normal_traffic_array = normal_traffic_array[ int(len(normal_traffic_array) / 24) : ] response_type = ResponseType.YTranslation grid_points = [ Point(x=x, y=0, z=-9.65) for x, _ in itertools.product( np.linspace(c.bridge.x_min, c.bridge.x_max, 50), # np.linspace(c.bridge.x_min, c.bridge.x_max, 4), [1], ) ] # Collect a list of features per scenarios scenario. features = [] for damage_scenario in healthy_and_cracked_scenarios[1:]: damage_c = damage_scenario.use(c) responses = responses_to_traffic_array( c=damage_c, traffic_array=normal_traffic_array, response_type=response_type, bridge_scenario=damage_scenario, points=grid_points, sim_runner=OSRunner, ).T ks_values = [] for p0_i, point0 in enumerate(grid_points): print_i(f"Point {p0_i + 1} / {len(grid_points)}", end="\r") ks_values.append([]) for p1_i, point1 in enumerate(grid_points): ks = ks_no_outliers(responses[p0_i], responses[p1_i]) ks_values[-1].append(ks) features.append((ks_values, damage_scenario.name)) # Save features to disk. features = np.array(features) np.save(features_path, features) features = np.load(features_path) # Reduce each pairwise map to a sum per sensor. for f_i, (feature, feature_name) in enumerate(features): features[f_i] = ([sum(sensor) for sensor in feature], feature_name) features[f_i] = ([sum(sensor) for sensor in features[f_i]], feature_name) # Cluster each pairwise map. from sklearn.cluster import KMeans kmeans = KMeans(n_clusters=2) kmeans.fit(features)
def traffic_response_plots(c: Config, times: int = 3): """Response to normal traffic per scenarios scenario at multiple time steps.""" response_type = ResponseType.YTranslation # 10 x 10 grid of points on the bridge deck where to record fem. points = [ Point(x=x, y=0, z=z) for x, z in itertools.product( np.linspace(c.bridge.x_min, c.bridge.x_max, 10), np.linspace(c.bridge.z_min, c.bridge.z_max, 10), ) ] # for damage_scenario in all_scenarios(c): for damage_scenario in [unit_temp_scenario]: response_array = responses_to_traffic_array( c=c, traffic_array=load_normal_traffic_array(c, mins=1)[0], response_type=response_type, bridge_scenario=damage_scenario, points=points, sim_runner=OSRunner, ) print(response_array.shape) mean_response_array = np.mean(response_array, axis=0).T print(mean_response_array.shape) print(mean_response_array.shape) for t in range(times): time_index = -1 + abs(t) top_view_bridge(c.bridge, abutments=True, piers=True) responses = Responses.from_responses( response_type=response_type, responses=[(response_array[time_index][p], point) for p, point in enumerate(points)], ) plot_contour_deck(c=c, responses=responses, center_norm=True, levels=100) plt.title(damage_scenario.name) plt.savefig( c.get_image_path( "contour-traffic-response", f"{damage_scenario.name}-time={time_index}", )) plt.close()
def oneclass(c: Config): normal_traffic_array, traffic_scenario = load_normal_traffic_array(c) bridge_scenarios = [HealthyScenario()] + each_pier_scenarios(c) response_type = ResponseType.YTranslation points = [ Point(x=x, y=0, z=z) for x, z in itertools.product( np.linspace(c.bridge.x_min, c.bridge.x_max / 2, 20), np.linspace(c.bridge.z_min, c.bridge.z_max / 2, 3), ) ] results = [] for b, bridge_scenario in enumerate(bridge_scenarios): print_i(f"One class: bridge scenario {bridge_scenario.name}") responses = responses_to_traffic_array( c=c, traffic_array=normal_traffic_array, response_type=response_type, bridge_scenario=bridge_scenario, points=points, fem_runner=OSRunner(c), ).T print(len(normal_traffic_array)) print(responses.shape) # Fit on the healthy scenario. if b == 0: assert len(responses) == len(points) clfs = [] for r, rs in enumerate(responses): print_i(f"Training classifier {r} / {len(responses)}") clfs.append(OneClassSVM().fit(rs.reshape(-1, 1))) scenario_results = [] for p, _ in enumerate(points): print_i(f"Predicting points {p} / {len(points)}") prediction = clfs[p].predict(responses[p].reshape(-1, 1)) print(prediction) print(len(prediction[prediction < 0])) print(len(prediction[prediction > 0]))
def pairwise_sensors(c: Config, dist_measure=ks_no_outliers): """Compare distribution of pairs of sensors under HealthyScenario.""" normal_traffic_array, traffic_scenario = load_normal_traffic_array(c) response_type = ResponseType.YTranslation points = [ Point(x=x, y=0, z=z) for x, z in itertools.product( np.linspace(c.bridge.x_min, c.bridge.x_max, 50), np.linspace(c.bridge.z_min, c.bridge.z_max, 4), ) ] bridge_scenario = HealthyScenario() responses = responses_to_traffic_array( c=c, traffic_array=normal_traffic_array, response_type=response_type, bridge_scenario=bridge_scenario, points=points, sim_runner=OSRunner, ).T assert len(responses) == len(points) ks_values_healthy = [] for p0, point0 in enumerate(points): print_i(f"Point {p0 + 1} / {len(points)}") ks_values_healthy.append([]) for p1, point1 in enumerate(points): ks = dist_measure(responses[p0], responses[p1]) ks_values_healthy[-1].append(ks) plt.landscape() plt.imshow(ks_values_healthy) plt.savefig(c.get_image_path("joint-clustering", "healthy-bridge")) plt.close() bridge_scenario = each_pier_scenarios(c)[0] responses = responses_to_traffic_array( c=c, traffic_array=normal_traffic_array, response_type=response_type, bridge_scenario=bridge_scenario, points=points, sim_runner=OSRunner, ).T assert len(responses) == len(points) ks_values_damage = [] for p0, point0 in enumerate(points): print_i(f"Point {p0 + 1} / {len(points)}") ks_values_damage.append([]) for p1, point1 in enumerate(points): ks = dist_measure(responses[p0], responses[p1]) ks_values_damage[-1].append(ks) plt.imshow(ks_values_damage) plt.savefig(c.get_image_path("joint-clustering", "scenarios-bridge")) plt.close() ks_values_comp = [] for p0, point0 in enumerate(points): ks_values_comp.append([]) for p1, point1 in enumerate(points): comp = abs(ks_values_healthy[p0][p1] - ks_values_damage[p0][p1]) ks_values_comp[-1].append(comp) plt.landscape() plt.imshow(ks_values_comp) plt.savefig(c.get_image_path("joint-clustering", "scenarios-bridge-comp")) plt.close() responses = Responses.from_responses( response_type=response_type, responses=[(sum(ks_values_comp[p]), point) for p, point in enumerate(points)], ) top_view_bridge(c.bridge, abutments=True, piers=True) plot_contour_deck(c=c, responses=responses) plt.savefig(c.get_image_path("joint-clustering", "scenarios-bridge-comp-contour")) plt.close()