def render_top_k_summary(source_path: str, prefix: str, epoch_index: int): """ Show top-k summary plot. Args: source_path (str): The root path of the dumped snapshots data for the corresponding experiment. prefix (str): Prefix of data folders. epoch_index (int): The index of selected epoch. """ helper.render_h3_title("Cike Bike Top K") data = helper.read_detail_csv( os.path.join( source_path, f"{prefix}{epoch_index}", GlobalFileNames.stations_sum ) ) # Convert index to station name. index_name_conversion = helper.read_detail_csv(os.path.join(source_path, GlobalFileNames.name_convert)) data["station name"] = list( map( lambda x: index_name_conversion.loc[int(x[9:])][0], data["name"] ) ) # Generate top summary. top_number = st.select_slider( "Select Top K", list(range(1, 6)) ) top_attributes = ["bikes", "trip_requirement", "fulfillment", "fulfillment_ratio"] for item in top_attributes: helper.generate_by_snapshot_top_summary("station name", data, int(top_number), item)
def _generate_top_k_summary(data: pd.DataFrame, snapshot_index: int, index_name_conversion: pd.DataFrame): """Generate CIM top k summary. Args: data (pd.Dataframe): Data of current snapshot. snapshot_index (int): Selected snapshot index. index_name_conversion (pd.Dataframe): Relationship between index and name. """ data_summary = data[data["frame_index"] == snapshot_index].reset_index(drop=True) data_summary["fulfillment_ratio"] = list( map( lambda x, y: round(x / (y + 1 / 1000), 4), data_summary["acc_fulfillment"], data_summary["acc_booking"] ) ) data_summary["port name"] = list( map( lambda x: index_name_conversion.loc[int(x[6:])][0], data_summary["name"] ) ) helper.render_h3_title("Select Top k:") selected_top_number = st.select_slider( label="", options=list(range(1, 6)) ) top_attributes = CIMItemOption.acc_info + ["fulfillment_ratio"] for item in top_attributes: helper.generate_by_snapshot_top_summary( "port name", data_summary, int(selected_top_number), item, snapshot_index )
def _generate_intra_view_by_snapshot(data_stations: pd.DataFrame, index_name_conversion: pd.DataFrame, attribute_option_candidates: List[str], snapshots_index: List[int], snapshot_num: int, stations_num: int): """Show Citi Bike intra-view data by snapshot. Args: data_stations (pd.Dataframe): Filtered Data. index_name_conversion (pd.Dataframe): Relationship between index and name. attribute_option_candidates (List[str]): All options for users to choose. snapshots_index (List[int]): Sampled snapshot index list. snapshot_num (int): Number of snapshots. stations_num (int): Number of stations. """ # Get selected snapshot index. selected_snapshot = st.sidebar.select_slider(label="snapshot index", options=snapshots_index) helper.render_h3_title(f"Snapshot-{selected_snapshot}: Detail Data") # Get according data with selected snapshot. data_filtered = data_stations[data_stations["frame_index"] == selected_snapshot] # Get increasing rate. sample_ratio = helper.get_sample_ratio_selection_list(stations_num) # Get sample rate (between 0-1). selected_station_sample_ratio = st.sidebar.select_slider( label="Station Sampling Ratio", options=sample_ratio) # Get formula input and output. data_formula = helper.get_filtered_formula_and_data( GlobalScenarios.CITI_BIKE, data_filtered, attribute_option_candidates) # Get sampled data and get station name. down_pooling_sample_list = helper.get_sample_index_list( stations_num, selected_station_sample_ratio) attribute_option = data_formula["attribute_option"] attribute_option.append("name") if selected_station_sample_ratio == 0: empty_head = attribute_option empty_head.append("Station Name") data_filtered = pd.DataFrame(columns=empty_head) else: data_filtered = data_formula["data"][attribute_option] data_filtered = data_filtered.iloc[down_pooling_sample_list] data_filtered["name"] = data_filtered["name"].apply( lambda x: int(x[9:])) data_filtered["Station Name"] = data_filtered["name"].apply( lambda x: index_name_conversion.loc[int(x)]) data_melt = data_filtered.melt(["Station Name", "name"], var_name="Attributes", value_name="Count") intra_snapshot_line_chart = alt.Chart(data_melt).mark_bar().encode( x=alt.X("name:N", axis=alt.Axis(title="Name")), y="Count:Q", color="Attributes:N", tooltip=["Attributes", "Count", "Station Name"], ).properties(width=700, height=380) st.altair_chart(intra_snapshot_line_chart)
def _generate_intra_view_by_station(data_stations: pd.DataFrame, index_name_conversion: pd.DataFrame, attribute_option_candidates: List[str], stations_index: List[int], snapshot_num: int): """ Show Citi Bike intra-view data by station. Args: data_stations (pd.Dataframe): Filtered station data. index_name_conversion (pd.Dataframe): Relationship between index and name. attribute_option_candidates (List[str]): All options for users to choose. stations_index (List[int]): List of station index. snapshot_num (int): Number of snapshots. """ selected_station = st.sidebar.select_slider(label="station index", options=stations_index) helper.render_h3_title( index_name_conversion.loc[int(selected_station)][0] + " Detail Data") # Filter data by station index. data_filtered = data_stations[data_stations["name"] == f"stations_{selected_station}"] snapshot_sample_ratio_list = helper.get_sample_ratio_selection_list( snapshot_num) selected_snapshot_sample_ratio = st.sidebar.select_slider( label="Snapshot Sampling Ratio:", options=snapshot_sample_ratio_list) # Get formula input and output. data_formula = helper.get_filtered_formula_and_data( GlobalScenarios.CITI_BIKE, data_filtered, attribute_option_candidates) attribute_option = data_formula["attribute_option"] attribute_option.append("frame_index") data_filtered = data_formula["data"][attribute_option].reset_index( drop=True) down_pooling_sample_list = helper.get_sample_index_list( snapshot_num, selected_snapshot_sample_ratio) data_filtered = data_filtered.iloc[down_pooling_sample_list] data_filtered.rename(columns={"frame_index": "snapshot_index"}, inplace=True) data_melt = data_filtered.melt("snapshot_index", var_name="Attributes", value_name="Count") intra_station_line_chart = alt.Chart(data_melt).mark_line().encode( x=alt.X("snapshot_index", axis=alt.Axis(title="Snapshot Index")), y="Count", color="Attributes", tooltip=["Attributes", "Count", "snapshot_index"]).properties(width=700, height=380) st.altair_chart(intra_station_line_chart) intra_station_bar_chart = alt.Chart(data_melt).mark_bar().encode( x=alt.X("snapshot_index:N", axis=alt.Axis(title="Snapshot Index")), y="Count:Q", color="Attributes:N", tooltip=["Attributes", "Count", "snapshot_index"]).properties(width=700, height=380) st.altair_chart(intra_station_bar_chart)
def _render_intra_view_by_snapshot(source_path: str, option_epoch: int, data_ports: pd.DataFrame, snapshots_index: List[int], index_name_conversion: pd.DataFrame, attribute_option_candidates: List[str], ports_num: int, prefix: str): """ Show intra-view data by snapshot. Args: source_path (str): The root path of the dumped snapshots data for the corresponding experiment. option_epoch (int): Index of selected epoch. data_ports (pd.Dataframe): Filtered port data. snapshots_index (List[int]): Index of selected snapshot. index_name_conversion (pd.Dataframe): Relationship between index and name. attribute_option_candidates (List[str]): All options for users to choose. ports_num (int): Number of ports in current snapshot. prefix (str): Prefix of data folders. """ selected_snapshot = st.sidebar.select_slider(label="snapshot index", options=snapshots_index) # Get sample ratio. sample_ratio = helper.get_sample_ratio_selection_list(ports_num) selected_port_sample_ratio = st.sidebar.select_slider( label="Ports Sampling Ratio:", options=sample_ratio) # Accumulated data. helper.render_h1_title("Accumulated Data") _render_intra_heat_map(source_path, GlobalScenarios.CIM, option_epoch, selected_snapshot, prefix) helper.render_h3_title( f"SnapShot-{selected_snapshot}: Port Accumulated Attributes") _generate_intra_panel_accumulated_by_snapshot(data_ports, selected_snapshot, ports_num, index_name_conversion, selected_port_sample_ratio) _generate_top_k_summary(data_ports, selected_snapshot, index_name_conversion) # Detailed data. helper.render_h1_title("Detail Data") _render_intra_panel_vessel(source_path, prefix, option_epoch, selected_snapshot) helper.render_h3_title( f"Snapshot-{selected_snapshot}: Port Detail Attributes") data_formula = helper.get_filtered_formula_and_data( GlobalScenarios.CIM, data_ports, attribute_option_candidates) _generate_intra_panel_by_snapshot(data_formula["data"], selected_snapshot, ports_num, index_name_conversion, selected_port_sample_ratio, data_formula["attribute_option"])
def _render_intra_heat_map(source_path: str, scenario: GlobalScenarios, epoch_index: int, snapshot_index: int, prefix: str): """Get matrix data and provide entrance to heat map of different scenario. Args: source_path (str): The root path of the dumped snapshots data for the corresponding experiment. scenario (GlobalScenarios): Name of current scenario: CIM. epoch_index (int): Selected epoch index. snapshot_index (int): Selected snapshot index. prefix (str): Prefix of data folders. """ matrix_data = pd.read_csv( os.path.join(source_path, f"{prefix}{epoch_index}", "matrices.csv")).loc[snapshot_index] if scenario == GlobalScenarios.CIM: helper.render_h3_title( f"snapshot_{snapshot_index}: Accumulated Port Transfer Volume") _generate_intra_heat_map(matrix_data["full_on_ports"])
def _generate_intra_panel_vessel(data_vessel: pd.DataFrame, snapshot_index: int, vessels_num: int): """Generate vessel data plot. Args: data_vessel (pd.Dataframe): Data of vessel information within selected snapshot index. snapshot_index (int): User-selected snapshot index. vessels_num (int): Number of vessels. """ helper.render_h3_title(f"SnapShot-{snapshot_index}: Vessel Attributes") # Get sampled(and down pooling) index. sample_ratio = helper.get_sample_ratio_selection_list(vessels_num) selected_vessel_sample_ratio = st.sidebar.select_slider( label="Vessels Sampling Ratio:", options=sample_ratio ) down_pooling_sample_list = helper.get_sample_index_list(vessels_num, selected_vessel_sample_ratio) data_vessel = data_vessel[ data_vessel["frame_index"] == snapshot_index ][CIMItemOption.vessel_info].reset_index(drop=True) data_rename = pd.DataFrame(columns=CIMItemOption.vessel_info) for index in down_pooling_sample_list: data_rename = pd.concat( [data_rename, data_vessel[data_vessel["name"] == f"vessels_{index}"]], axis=0 ) data_filtered = data_rename.reset_index(drop=True) data_filtered["name"] = data_filtered["name"].apply(lambda x: int(x[8:])) data_melt = data_filtered.melt( "name", var_name="Attributes", value_name="Count" ) intra_vessel_bar_chart = alt.Chart(data_melt).mark_bar().encode( x=alt.X("name:N", axis=alt.Axis(title="Vessel Index")), y="Count:Q", color="Attributes:N", tooltip=["Attributes", "Count", "name"] ).properties( width=700, height=380 ) st.altair_chart(intra_vessel_bar_chart)
def _render_intra_view_by_ports( data_ports: pd.DataFrame, ports_index: int, index_name_conversion: pd.DataFrame, attribute_option_candidates: List[str], snapshot_num: int ): """ Show intra-view data by ports. Args: data_ports (pd.Dataframe): Filtered port data. ports_index (int):Index of port of current data. index_name_conversion (pd.Dataframe): Relationship of index and name. attribute_option_candidates (List[str]): All options for users to choose. snapshot_num (int): Number of snapshots on a port. """ selected_port = st.sidebar.select_slider( label="Choose a Port:", options=ports_index ) sample_ratio = helper.get_sample_ratio_selection_list(snapshot_num) selected_snapshot_sample_ratio = st.sidebar.select_slider( label="Snapshot Sampling Ratio:", options=sample_ratio ) # Accumulated data. helper.render_h1_title("CIM Accumulated Data") helper.render_h3_title( f"Port Accumulated Attributes: {selected_port} - {index_name_conversion.loc[int(selected_port)][0]}" ) _generate_intra_panel_accumulated_by_ports( data_ports, f"ports_{selected_port}", snapshot_num, selected_snapshot_sample_ratio ) # Detailed data. helper.render_h1_title("CIM Detail Data") data_formula = helper.get_filtered_formula_and_data( GlobalScenarios.CIM, data_ports, attribute_option_candidates ) helper.render_h3_title( f"Port Detail Attributes: {selected_port} - {index_name_conversion.loc[int(selected_port)][0]}" ) _generate_intra_panel_by_ports( data_formula["data"], f"ports_{selected_port}", snapshot_num, selected_snapshot_sample_ratio, data_formula["attribute_option"] )