def render_inter_view(source_path: str, epoch_num: int): """Render the cross-epoch infomartion chart of Citi Bike data. This part would be displayed only if epoch_num > 1. Args: source_path (str): The root path of the dumped snapshots data for the corresponding experiment. epoch_num (int): Total number of epoches, i.e. the total number of data folders since there is a folder per epoch. """ helper.render_h1_title("Citi Bike Inter Epoch Data") sample_ratio = helper.get_sample_ratio_selection_list(epoch_num) # Get epoch sample number. down_pooling_range = helper._get_sampled_epoch_range(epoch_num, sample_ratio) attribute_option_candidates = ( CITIBIKEItemOption.quick_info + CITIBIKEItemOption.requirement_info + CITIBIKEItemOption.station_info ) # Generate data. data_summary = helper.read_detail_csv( os.path.join(source_path, GlobalFileNames.stations_sum) ).iloc[down_pooling_range] # Get formula and selected data. data_formula = helper.get_filtered_formula_and_data( GlobalScenarios.CITI_BIKE, data_summary, attribute_option_candidates ) _generate_inter_view_panel( data_formula["data"][data_formula["attribute_option"]], down_pooling_range )
def render_inter_view(source_path: str, epoch_num: int): """Show CIM inter-view plot. Args: source_path (str): The root path of the dumped snapshots data for the corresponding experiment. epoch_num (int): Total number of epoches, i.e. the total number of data folders since there is a folder per epoch. """ helper.render_h1_title("CIM Inter Epoch Data") sample_ratio = helper.get_sample_ratio_selection_list(epoch_num) # Get epoch sample list. down_pooling_range = helper._get_sampled_epoch_range(epoch_num, sample_ratio) attribute_option_candidates = ( CIMItemOption.quick_info + CIMItemOption.port_info + CIMItemOption.booking_info ) # Generate data. data = helper.read_detail_csv(os.path.join(source_path, GlobalFileNames.ports_sum)).iloc[down_pooling_range] data["remaining_space"] = list( map( lambda x, y, z: x - y - z, data["capacity"], data["full"], data["empty"] ) ) # Get formula and selected data. data_formula = helper.get_filtered_formula_and_data(GlobalScenarios.CIM, data, attribute_option_candidates) _generate_inter_view_panel( data_formula["data"][data_formula["attribute_option"]], down_pooling_range )
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 _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"] )
def test_get_sample_ratio_selection_list(self): """Test method get_sample_ratio_selection_list(a)""" self.assertEqual([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], get_sample_ratio_selection_list(5))