def test_api3_plot_from_pandas(self): g = graphistry.edges(pd.DataFrame({ 's': [0], 'd': [0] })).bind(source='s', destination='d') ds = g.plot(skip_upload=True) assert isinstance(ds.edges, pa.Table)
def plotter_thread(df2): g = graphistry.edges(df2).bind(edge_color='my_color', source='Source', destination='Destination') g = g.settings(url_params={'bg': '%23FFFFFF'}) graph = g.plot(render=False, name="Zargonovski") print("Thread Plotted {}".format(graph))
def test_addStyle_good(self): g = graphistry.bind() bg = {'color': 'red'} fg = {'blendMode': 1} logo = {'url': 'zzz'} page = {'title': 'zzz'} assert g.addStyle()._style == {} g.addStyle(fg={'blendMode': 'screen'}) assert g.addStyle()._style == {} assert g.addStyle(bg=copy.deepcopy(bg))._style == {'bg': bg} assert g.addStyle(bg={ 'color': 'blue' }).addStyle(bg=copy.deepcopy(bg))._style == { 'bg': bg } assert g.addStyle(bg={ 'image': { 'url': 'http://asdf.com/b.png' } }).addStyle(bg=copy.deepcopy(bg))._style == { 'bg': { **bg, 'image': { 'url': 'http://asdf.com/b.png' } } } assert g.addStyle(bg=copy.deepcopy(bg), fg=copy.deepcopy(fg), logo=copy.deepcopy(logo), page=copy.deepcopy(page))._style == { 'bg': bg, 'fg': fg, 'logo': logo, 'page': page } assert g.addStyle(bg=copy.deepcopy(bg), fg=copy.deepcopy(fg), logo=copy.deepcopy(logo), page=copy.deepcopy(page))\ .addStyle(bg={'color': 'green'})._style == {'bg': {'color': 'green'}, 'fg': fg, 'logo': logo, 'page': page} g2 = graphistry.edges(pd.DataFrame({ 's': [0], 'd': [0] })).bind(source='s', destination='d') ds = g2.addStyle(bg=copy.deepcopy(bg), fg=copy.deepcopy(fg), page=copy.deepcopy(page), logo=copy.deepcopy(logo)).plot(skip_upload=True) assert ds.metadata['bg'] == bg assert ds.metadata['fg'] == fg assert ds.metadata['logo'] == logo assert ds.metadata['page'] == page
def test_styleApi_reject(self): bg = {'color': 'red'} fg = {'blendMode': 1} logo = {'url': 'zzz'} page = {'title': 'zzz'} g2 = graphistry.edges(pd.DataFrame({ 's': [0], 'd': [0] })).bind(source='s', destination='d') g3 = g2.addStyle(bg=copy.deepcopy(bg), fg=copy.deepcopy(fg), page=copy.deepcopy(page), logo=copy.deepcopy(logo)) with pytest.raises(ValueError): g3.plot(skip_upload=True)
import graphistry as graphistry from dash.dependencies import Input, Output, State import pandas as pd import urllib.request from pandas.tseries.offsets import * graphistry.register(api=3, protocol="https", server="hub.graphistry.com", username="******", password="******") url = "https://raw.githubusercontent.com/uber-web/kepler.gl-data/master/earthquakes/data.csv" download_file = urllib.request.urlretrieve(url, "data.csv") df = pd.read_csv("data.csv") my_src_col = 'DateTime' my_dest_col = 'EventID' g = None g = graphistry.edges(df).bind(source=my_src_col, destination=my_dest_col) graph = g.plot() graph_viz = html.Iframe(src=graph, height='800', width='100%') graphistry_page = html.Div([html.H2("Graphistry Visualization"), graph_viz]) def get_page(): return graphistry_page
def data_overview(): result = conn.runInstalledQuery('data_overview', timeout = 16000) data = pd.DataFrame(result[0]['@@edgeList']) g = graphistry.edges(data, 'from_id', 'to_id') graph = g.plot(render=False) return graph
def main_area(fdnm, conn): file_path = os.path.dirname(os.path.realpath(__file__)) parent_path = os.path.abspath(os.path.join(file_path, os.path.pardir)) logo_its = Image.open(os.path.join(parent_path, 'logo.jpeg')) st.image(logo_its, caption=None, width=250, use_column_width='True', clamp=False, channels='RGB', output_format='auto') st.write(""" # LODHalal: Search Product """) st.write('Search product that contains the input word.') tic = time.perf_counter() if conn is None: logger.error('Cannot run tg demo without creds') st.write( RuntimeError( 'Demo requires a TigerGraph connection. Put creds into left sidebar, or fill in envs/tigergraph.env & restart' )) return None # Search Product by Name query = "GetProductByName" resultTG = conn.runInstalledQuery(query, params={'foodname': fdnm}) results = resultTG[0]['@@tupleRecords'] data = pd.DataFrame(results) data = pd.DataFrame({ 'Food ID': data['id'], 'Name': data['foodname'], 'Halal Certifate': data['cert'], 'Issuing Authority': data['org'] }) try: res = data.values.tolist() toc = time.perf_counter() logger.info(f'Query Execution: {toc - tic:0.02f} seconds') logger.debug('Query Result Count: %s', len(res)) metrics['tigergraph_time'] = toc - tic # Calculate the metrics metrics['node_cnt'] = data.size metrics['prop_cnt'] = (data.size * data.columns.size) if data.size > 0: # url = plot_url(nodes_df, edges_df) g = graphistry.edges(data).bind(source='Name', destination='Name') \ .nodes(data).bind(node='Name') url = g.plot(render=False, as_files=True) else: url = "" except Exception as e: logger.error('oops in TigerGraph', exc_info=True) raise e logger.info("Finished compute phase") try: pass except RuntimeError as e: if str( e ) == "There is no current event loop in thread 'ScriptRunner.scriptThread'.": loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) else: raise e except Exception as e: logger.error('oops in TigerGraph', exc_info=True) raise e logger.info('rendering main area, with url: %s', url) GraphistrySt().render_url(url)
def run_filters(num_nodes, num_edges, bank, bank_ids): transactions_map_df = fetch_csv() #### GLOBAL STATS ##### sample_df = transactions_map_df #.sample(10000) originator_stats_df = sample_df.groupby('originator_bank_id').agg({ 'begin_date': 'min', 'end_date': 'max', 'number_transactions': ['sum', 'max'], 'amount_transactions': ['sum', 'max'] }) originator_stats_df.columns = [ 'originator_' + '_'.join(tup).rstrip('_') for tup in originator_stats_df.columns ] originator_stats_df = originator_stats_df.reset_index().rename( columns={'originator_bank_id': 'bank_id'}) beneficiary_stats_df = sample_df.groupby('beneficiary_bank_id').agg({ 'begin_date': 'min', 'end_date': 'max', 'number_transactions': ['sum', 'max'], 'amount_transactions': ['sum', 'max'] }) beneficiary_stats_df.columns = [ 'beneficiary_' + '_'.join(tup).rstrip('_') for tup in beneficiary_stats_df.columns ] beneficiary_stats_df = beneficiary_stats_df.reset_index().rename( columns={'beneficiary_bank_id': 'bank_id'}) originator_cols = [c for c in sample_df.columns if 'originator_' in c] beneficiary_cols = [c for c in sample_df.columns if 'beneficiary_' in c] nodes_df = pd.concat( [ sample_df[originator_cols]\ .rename(columns={c: c.replace("originator_", "") for c in originator_cols})\ .drop_duplicates(subset=['bank_id'])\ .assign(type='bank', entity_id=sample_df['originator_bank_id']), sample_df[beneficiary_cols]\ .rename(columns={c: c.replace("beneficiary_", "") for c in beneficiary_cols})\ .drop_duplicates(subset=['bank_id'])\ .assign(type='bank')\ .assign(type='bank', entity_id=sample_df['beneficiary_bank_id']) ], ignore_index=True, sort=False) nodes_df = nodes_df\ .merge(beneficiary_stats_df, on='bank_id', how='left')\ .merge(originator_stats_df, on='bank_id', how='left') nodes_df['abbreviation'] = nodes_df['bank'].apply( lambda s: ''.join([x[:1] for x in s.split(" ")[:3]])) nodes_df['sum_transactions'] = nodes_df[ 'originator_amount_transactions_sum'].fillna( 0) + nodes_df['beneficiary_amount_transactions_sum'].fillna(0) nodes_df['number_transactions'] = nodes_df[ 'originator_number_transactions_sum'].fillna( 0) + nodes_df['beneficiary_number_transactions_sum'].fillna(0) iso3611_df = fetch_iso3611() iso3_to_iso2 = { row['ISO3166-1-Alpha-3'].lower(): row['ISO3166-1-Alpha-2'].lower() for row in iso3611_df[['ISO3166-1-Alpha-2', 'ISO3166-1-Alpha-3']].to_dict(orient='records') if ('ISO3166-1-Alpha-3' in row and type(row['ISO3166-1-Alpha-3']) == str) \ and ('ISO3166-1-Alpha-2' in row and type(row['ISO3166-1-Alpha-2']) == str) } nodes_df['iso2'] = nodes_df['iso'].apply( lambda v: iso3_to_iso2[v.lower()] if v.lower() in iso3_to_iso2 else '') iso2_to_flags = { iso2: 'flag-icon-' + iso2 for iso2 in nodes_df['iso2'].unique() } ##### PROJECTION ###### ids = bank_name_to_ids(nodes_df, bank, bank_ids) ego_banks_df = nodes_df.merge(pd.DataFrame({'entity_id': ids}), how='inner') nodes_df, sample_df = ego_edges(nodes_df, sample_df, ids, src='originator_bank_id', dst='beneficiary_bank_id', node='entity_id') ##### VIZ ##### #abbrv_to_abbrv = {x: x for x in nodes_df['abbreviation'].unique().tolist()} bank_to_bank = {x: x for x in nodes_df['bank'].unique().tolist()} g = graphistry.edges(sample_df).nodes(nodes_df)\ .bind(source='originator_bank_id', destination='beneficiary_bank_id', node='entity_id')\ .bind(point_title='bank', point_size='sum_transactions')\ .encode_point_icon('bank', as_text=True, categorical_mapping=bank_to_bank)\ .encode_point_badge('iso2', 'TopRight', categorical_mapping=iso2_to_flags)\ .encode_point_color('beneficiary_amount_transactions_sum', palette=['white', 'pink', 'purple'], as_continuous=True, for_current=True)\ .encode_edge_color('amount_transactions', ["maroon", "red", "yellow", "white", "cyan"], as_continuous=True, for_current=True)\ .addStyle(bg={'color': '#EEE'})\ .settings( height=800, url_params={ 'pointOpacity': 0.3 if len(sample_df) > 1500 else 1.0, 'edgeOpacity': 0.2 if len(sample_df) > 1500 else 1.0, 'strongGravity': True, 'showPointsOfInterestLabel': False, 'play': 5000}) graph_url = g.plot(render=False) return { 'nodes_df': g._nodes, 'edges_df': g._edges, 'graph_url': graph_url, 'ego_banks_df': ego_banks_df }