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_panel_by_ports( data: pd.DataFrame, option_port_name: str, snapshot_num: int, snapshot_sample_num: float, attribute_option: List[str] = None ): """Generate intra-view plot. View info within different resource holders(In this senario, ports) in the same epoch. Change snapshot sampling num freely. Args: data (pd.Dataframe): Filtered data within selected conditions. option_port_name (str): Condition for filtering the name attribute in the data. snapshot_num (int): Number of snapshots. snapshot_sample_num (float): Number of sampled snapshots. attribute_option (List[str]): Translated user-selecteded option. """ if attribute_option is not None: attribute_option.append("frame_index") else: attribute_option = ["frame_index"] attribute_temp_option = attribute_option attribute_temp_option.append("name") data_acc = data[attribute_temp_option] down_pooling_sample_list = helper.get_sample_index_list(snapshot_num, snapshot_sample_num) port_filtered = data_acc[data_acc["name"] == option_port_name][attribute_option].reset_index(drop=True) attribute_option.remove("name") data_filtered = port_filtered.loc[down_pooling_sample_list] data_filtered = data_filtered[attribute_option] data_filtered.rename( columns={"frame_index": "snapshot_index"}, inplace=True ) data_melt = data_filtered.melt( "snapshot_index", var_name="Attributes", value_name="Count" ) port_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(port_line_chart) port_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(port_bar_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 _generate_intra_panel_by_snapshot(data: pd.DataFrame, snapshot_index: int, ports_num: int, index_name_conversion: pd.DataFrame, sample_ratio: List[float], attribute_option: List[str] = None): """Generate intra-view plot. View info within different snapshot in the same epoch. Args: data (pd.Dataframe): Filtered data within selected conditions. snapshot_index (int): user-selected snapshot index. ports_num (int): Number of ports. index_name_conversion (pd.Dataframe): Relationship between index and name. sample_ratio (List[float]): Sampled port index list. attribute_option (List[str]): Translated user-selected options. """ if attribute_option is not None: attribute_option.append("name") else: attribute_option = ["name"] attribute_temp_option = attribute_option attribute_temp_option.append("frame_index") data_acc = data[attribute_temp_option] down_pooling_sample_list = helper.get_sample_index_list( ports_num, sample_ratio) snapshot_filtered = data_acc[data_acc["frame_index"] == snapshot_index][attribute_option].reset_index( drop=True) data_rename = pd.DataFrame(columns=attribute_option) for index in down_pooling_sample_list: data_rename = pd.concat([ data_rename, snapshot_filtered[snapshot_filtered["name"] == f"ports_{index}"] ], axis=0) data_rename = data_rename.reset_index(drop=True) attribute_option.remove("frame_index") data_rename["name"] = data_rename["name"].apply(lambda x: int(x[6:])) data_rename = data_rename[attribute_option] data_rename["Port Name"] = data_rename["name"].apply( lambda x: index_name_conversion.loc[int(x)][0]) data_melt = data_rename.melt(["name", "Port Name"], var_name="Attributes", value_name="Count") intra_bar_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", "Port Name"]).properties(width=700, height=380) st.altair_chart(intra_bar_chart)
def _generate_intra_panel_accumulated_by_ports( data: pd.DataFrame, option_port_name: str, snapshot_num: int, snapshot_sample_num: float ): """Generate intra-view accumulated plot by ports. Args: data (pd.Dataframe): Filtered data within selected conditions. option_port_name (str): Condition for filtering the name attribute in the data. snapshot_num (int): Number of snapshots. snapshot_sample_num (float): Number of sampled snapshots. """ info_selector = CIMItemOption.basic_info + CIMItemOption.acc_info data_acc = data[info_selector] info_selector.pop(0) down_pooling_sample_list = helper.get_sample_index_list(snapshot_num, snapshot_sample_num) port_filtered = data_acc[data_acc["name"] == option_port_name][info_selector].reset_index(drop=True) port_filtered.rename( columns={"frame_index": "snapshot_index"}, inplace=True ) data_filtered = port_filtered.loc[down_pooling_sample_list] data_melt = data_filtered.melt( "snapshot_index", var_name="Attributes", value_name="Count" ) port_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(port_line_chart) port_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(port_bar_chart)
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 _generate_intra_panel_accumulated_by_snapshot( data: pd.DataFrame, snapshot_index: int, ports_num: int, index_name_conversion: pd.DataFrame, sample_ratio: List[float] ): """Generate intra-view accumulated plot by snapshot. Args: data (pd.Dataframe): Filtered data within selected conditions. snapshot_index (int): user-selected snapshot index. ports_num (int): Number of ports. index_name_conversion (pd.Dataframe): Relationship between index and name. sample_ratio (List[float]): Sampled port index list. """ info_selector = CIMItemOption.basic_info + CIMItemOption.acc_info data_acc = data[info_selector] info_selector.pop(1) down_pooling_sample_list = helper.get_sample_index_list(ports_num, sample_ratio) snapshot_filtered = data_acc[data_acc["frame_index"] == snapshot_index][info_selector].reset_index(drop=True) data_rename = pd.DataFrame(columns=info_selector) for index in down_pooling_sample_list: data_rename = pd.concat( [data_rename, snapshot_filtered[snapshot_filtered["name"] == f"ports_{index}"]], axis=0 ) data_rename = data_rename.reset_index(drop=True) data_rename["name"] = data_rename["name"].apply(lambda x: int(x[6:])) data_rename["Port Name"] = data_rename["name"].apply(lambda x: index_name_conversion.loc[int(x)][0]) data_melt = data_rename.melt( ["name", "Port Name"], var_name="Attributes", value_name="Count" ) intra_bar_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", "Port Name"] ).properties( width=700, height=380 ) st.altair_chart(intra_bar_chart)
def test_get_sample_index_list(self): """Test method get_sample_index_list(a,b)""" self.assertEqual([0, 10, 20, 30, 40], get_sample_index_list(50, 0.1))