def main_area(url, nodes, edges, state): logger.debug('rendering main area, with url: %s', url) GraphistrySt().render_url(url) # Get the count by state of visits shown bar_chart_data = edges[edges['label'] == 'visited'] group_label = 'state' if not state == 'All States': # If a state is chosen group by city group_label = 'city' bar_chart_data['count'] = bar_chart_data.groupby( group_label)[group_label].transform('count') bar_chart_data = bar_chart_data[[ group_label, 'count' ]].drop_duplicates().reset_index()[[group_label, 'count']] # Sort the values by group_label bar_chart_data.sort_values(by=[group_label], inplace=True) chart = alt.Chart(bar_chart_data).mark_bar().encode(x=group_label, y='count') st.altair_chart(chart, use_container_width=True) # Show a datatable with the values transposed st.dataframe(bar_chart_data.set_index(group_label).T) st.markdown(f'''<small> Neptune Load Time (s): {float(metrics['neptune_time']):0.2f} | Graphistry Load Time (s): {float(metrics['graphistry_time']):0.2f} | Node Count: {metrics['node_cnt']} | Edge Count: {metrics['edge_cnt']} | Property Count: {metrics['prop_cnt']} </small>''', unsafe_allow_html=True)
def plot_url(edges_df, n): nodes_df = pd.DataFrame( {'n': pd.concat([edges_df['s'], edges_df['d']]).unique()}) nodes_df['nc'] = nodes_df['n'].apply( lambda v: 0x01000000 * round(255 * v / n)) logger.info('Starting graphistry plot') if not GraphistrySt().test_login(): return '' url = graphistry\ .bind(source="s", destination="d")\ .edges(edges_df)\ .nodes(nodes_df)\ .bind(node='n', point_color='nc')\ .settings(url_params={ 'pointSize': 0.3, 'splashAfter': 'false', 'bg': '%23' + 'f0f2f6' })\ .plot(render=False) logger.info('Generated viz, got back urL: %s', url) return url
def main_area(num_nodes, num_edges, bank, bank_ids, nodes_df, edges_df, graph_url, ego_banks_df): logger.debug('rendering main area, with url: %s', graph_url) GraphistrySt().render_url(graph_url) st.subheader('Selected banks') st.write(ego_banks_df) st.subheader('Surrounding banks') st.write(nodes_df) st.subheader('Reported transactions') st.write(edges_df)
def main_area(url, nodes, edges, user_id, conn): logger.debug('rendering main area, with url: %s', url) GraphistrySt().render_url(url) dates = [] amounts = [] transfer_type = [] results = None try: results = conn.runInstalledQuery("totalTransaction", params={"Source": user_id})[0] except Exception as e: print(e) # Create bar chart of transactions if results != None: for action in results: for transfer in results[action]: dates.append( datetime.datetime.fromtimestamp( transfer['attributes']['ts'])) amounts.append(transfer['attributes']['amount']) transfer_type.append(action) cols = list(zip(dates, amounts, transfer_type)) cols = sorted(cols, key=lambda x: x[0].day) cols = sorted(cols, key=lambda x: x[0].month) cols = sorted(cols, key=lambda x: x[0].year) df = pd.DataFrame(data=cols, columns=['Date', 'Amount', 'Type']) df['Date'] = pd.to_datetime(df['Date']) map_color = { "receive": "rgba(0,0,255,0.5)", "transfer": "rgba(255,0,0,0.5)" } df['Color'] = df['Type'].map(map_color) df = df.groupby([df['Date'].dt.to_period('M'), 'Type', 'Color']).sum() df = df.reset_index(level=['Type', 'Color']) df.index = df.index.values.astype('datetime64[M]') bar = px.bar( df, x=df.index, y='Amount', labels={'x': 'Date'}, color='Type', color_discrete_map=map_color, text='Amount', title="Transaction Amounts by Month for User {}".format(user_id), height=350, barmode='group') bar.update_xaxes(dtick="M1", tickformat="%b\n%Y") try: for trace in bar.data: trace.name = trace.name.split('=')[1].capitalize() except: for trace in bar.data: trace.name = trace.name.capitalize() st.plotly_chart(bar, use_container_width=True) st.markdown(f'''<small> TigerGraph Load Time (s): {float(metrics['tigergraph_time']):0.2f} | Graphistry Load Time (s): {float(metrics['graphistry_time']):0.2f} | Node Count: {metrics['node_cnt']} | Edge Count: {metrics['edge_cnt']} | Property Count: {metrics['prop_cnt']} </small>''', unsafe_allow_html=True)
def main_area(top, selval, selstr, 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: Similar Product """) st.write('Search product with similar ingredients. We can enter the name of the product (Based on 10 Similar Product Name), \ then the system will search up to 10 products that contain the input word, and finally return the products that \ contain similar ingredients with them. Or we can enter the product ID (Based on Product ID) and then the system \ will return the products that contain similar ingredients with the input product.') 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 new_from_id = [] new_foodname = [] new_cert = [] new_org = [] new_to_id = [] if selval == 1: # Get Similar Product error = 0 try: query2 = "GetSimilarProduct" resultTG2 = conn.runInstalledQuery(query2, params={'ids': selstr, 'top': top}) results2 = resultTG2[0]['SIMILAR_Product'] except Exception as e: print(e) error = 1 if error == 1: new_from_id.append(selstr) new_foodname.append('') new_cert.append('') new_org.append('') new_to_id.append(selstr) else: dataawal = pd.DataFrame(results2) dataname = [] for dataattr in dataawal['attributes']: name = dataattr.get('Subjects.food_name') dataname.append(name) i=0 for product in results2: new_from_id.append(selstr) new_foodname.append(dataname[i]) new_cert.append('') new_org.append('') new_to_id.append(product['v_id']) i += 1 elif selval == 0: # Search Product by Name query = "GetProductByName" resultTG = conn.runInstalledQuery(query, params={'foodname': selstr}) results = resultTG[0]['@@tupleRecords'] results = results[0:10] # Get Similar Product for product in results: error = 0 try: query2 = "GetSimilarProduct" resultTG2 = conn.runInstalledQuery(query2, params={'ids': product['id'], 'top': top}) results2 = resultTG2[0]['SIMILAR_Product'] except Exception as e: print(e) error = 1 if error == 1: new_from_id.append(product['id']) new_foodname.append(product['foodname']) new_cert.append(product['cert']) new_org.append(product['org']) new_to_id.append(product['id']) else: for simproduct in results2: new_from_id.append(product['id']) new_foodname.append(product['foodname']) new_cert.append(product['cert']) new_org.append(product['org']) new_to_id.append(simproduct['v_id']) nodes_from_df = pd.DataFrame({ 'Food ID': [int (i) for i in new_from_id], 'Name': new_foodname, 'Halal Certifate': new_cert, 'Issuing Authority': new_org }) nodes_to_df = pd.DataFrame({ 'Food ID': [int (i) for i in new_to_id], 'Name': '', 'Halal Certifate': '', 'Issuing Authority': '' }) nodes_df = nodes_from_df.append(nodes_to_df) edges_df = pd.DataFrame({ 'from_id': [int (i) for i in new_from_id], 'to_id': [int (i) for i in new_to_id] }) nodes_df = nodes_df.drop_duplicates(subset='Food ID') edges_df = edges_df.drop_duplicates() try: res = nodes_df.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'] = nodes_df.size metrics['prop_cnt'] = (nodes_df.size * nodes_df.columns.size) + \ (edges_df.size * edges_df.columns.size) if nodes_df.size > 0: url = plot_url(nodes_df, edges_df) # g = graphistry.edges(edges_df).bind(source='from_id', destination='to_id') # 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 main_area(num_nodes, num_edges, nodes_df, edges_df, graph_url): logger.debug('rendering main area, with url: %s', graph_url) GraphistrySt().render_url(graph_url) st.header('Edges (RAPIDS GPU cudf DataFrame)') st.write(edges_df)
def main_area(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: Halal Product & Manufacture Link """) st.write( 'Shows the link between products and its manufacturers. The \'cutlery\' icon in the node is for product while \ the \'building\' icon is for manufacturer.') 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 query = "ProductManufactureLink" resultTG = conn.runInstalledQuery(query) results = resultTG[0]['@@tupleRecords'] data = pd.DataFrame(results[0:2000]) edges_df = data nodes_src = pd.DataFrame({'Name': data['src'], 'Type': 'Product'}) nodes_dest = pd.DataFrame({'Name': data['dest'], 'Type': 'Manufacture'}) nodes_df = nodes_src.append(nodes_dest) nodes_df = nodes_df.drop_duplicates(subset='Name') 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) 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 main_area(num_nodes, num_edges, nodes_df, edges_df, graph_url): #logger.debug('rendering main area, with url: %s', graph_url) GraphistrySt().render_url(graph_url)
def main_area(edges_df, url): logger.debug('rendering main area, with url: %s', url) GraphistrySt().render_url(url)
def main_area(url, nodes, edges, person_a, person_b, k_hops, conn): GraphistrySt().render_url(url)
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 main_area(clusnum, 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: Ingredient Cluster """) st.write('Shows the cluster of product\'s ingredients.') 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 file_path = os.path.dirname(os.path.realpath(__file__)) if (clusnum == 2): data = pd.read_csv(os.path.join(file_path, 'klasters3.csv')) else: data = pd.read_csv(os.path.join(file_path, 'klasters.csv')) nan_value = float("NaN") data.replace("", nan_value, inplace=True) data.dropna(subset = ['0'], inplace=True) cluster_num = [] for vertex in data['1']: if int(vertex)<1: cluster_num.append('Cluster {}'.format(int(vertex)+1)) else: cluster_num.append('Cluster {}'.format(int(vertex))) nodes_df = pd.DataFrame({ 'Ingredient': data['0'], 'Cluster': cluster_num }) edges_df = nodes_df 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) 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)