Skip to content

Instantly share code, notes, and snippets.

View joseberlines's full-sized avatar
💭
Python

joseberlines joseberlines

💭
Python
  • European Patent Office
  • Berlin
View GitHub Profile
stations_df.drop('shape', axis =1, inplace = True)
stations_df['width'] = stations_df['score'] * 40 + 40
stations_df['height'] = stations_df['score'] * 40 + 40
stations_df
G=transform_into_ipycytoscape(stations_df,rails_df)
display(G)
stations_df['score'] = stations_df['passengers'].apply(lambda x: (x-stations_df['passengers'].min())/(stations_df['passengers'].max()-stations_df['passengers'].min()))
stations_df
stations_df['shape'] = stations_df['passengers'].apply(lambda x: 'rectangle' if x>=350000 else '')
stations_df
G=transform_into_ipycytoscape(stations_df,rails_df)
display(G)
stations_high_speed_arriving = rails_df['high-speed'].to_list()
list_of_HS_stations =list(set([item for sublist in stations_high_speed_arriving for item in sublist]))
stations_df['background-color'] = stations_df['id'].apply(lambda x: 'violet' if x in list_of_HS_stations else 'yellow')
G=transform_into_ipycytoscape(stations_df,rails_df)
display(G)
rails_df['high-speed'] = rails_df.apply(lambda x: [x.target,x.source] if x.speed in ['400km/h'] else [], axis=1)
rails_df
G=transform_into_ipycytoscape(stations_df,rails_df)
display(G)
stations_df['background-color'] = stations_df['passengers'].apply(lambda x: 'red' if x>200000 else 'blue')
rails_df['line-color'] = rails_df['label'].apply(lambda x: 'red' if x in ['400km/h','300km/h'] else 'green')
stations_df
def transform_into_ipycytoscape(nodes_df,edges_df):
nodes_dict = nodes_df.to_dict('records')
edges_dict = edges_df.to_dict('records')
# building nodes
data_keys = ['id','label','classes']
position_keys = ['position_x','position_y']
rest_keys = ['score','idInt','name','score','group','removed','selected','selectable','locked','grabbed'
stations_df.loc[stations_df['country'] != 'Germany','classes'] = 'EU'
stations_df['background-color']=''
stations_df.loc[stations_df['country'] == 'Germany','background-color'] = 'blue'
stations_df.loc[stations_df['country'] != 'Germany','background-color'] = 'orange'
stations_df.loc[stations_df['id'] == 'BER','background-color'] = 'yellow'
stations_df
import ipycytoscape
import json
import ipywidgets
import pandas as pd
stations = [{'id': 'BER','country': 'Germany','classes': 'east','label': 'BER Hbf','passengers': 400000},
{'id': 'MUN','country': 'Germany','classes': 'west','label': 'MUN Hbf','passengers': 200000},
{'id': 'FRA','country': 'Germany','classes': 'west','label': 'HBf FRA','passengers': 200000},
{'id': 'HAM','country': 'Germany', 'classes': 'west','label': 'HBf HAM','passengers': 150000},
{'id': 'LEP','country': 'Germany','label': 'HBf LEP','classes': 'east','passengers': 50000},