Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main(real_net, vect_post):
import numpy as np
from copy import deepcopy
from .__init__ import Network
from . import proc_df_labels
net = deepcopy(Network())
sigs = vect_post['columns']
all_rows = []
all_sigs = []
for inst_sig in sigs:
all_sigs.append(inst_sig['col_name'])
col_data = inst_sig['data']
for inst_row_data in col_data:
all_rows.append(inst_row_data['row_name'])
all_rows = sorted(list(set(all_rows)))
all_sigs = sorted(list(set(all_sigs)))
all_filt = list(range(10))
all_filt = [i / float(10) for i in all_filt]
mat = deepcopy(df['mat'])
sum_row = np.sum(mat, axis=1)
max_sum = max(sum_row)
for inst_filt in all_filt:
cutoff = inst_filt * max_sum
copy_net = deepcopy(net)
inst_df = deepcopy(df)
inst_df = run_filter.df_filter_row_sum(inst_df, cutoff, take_abs=False)
tmp_net = deepcopy(Network())
tmp_net.df_to_dat(inst_df)
try:
try:
calc_clust.cluster_row_and_col(tmp_net, dist_type=dist_type,
run_clustering=True)
except:
calc_clust.cluster_row_and_col(tmp_net, dist_type=dist_type,
run_clustering=False)
inst_view = {}
inst_view['pct_row_' + rank_type] = inst_filt
inst_view['dist'] = 'cos'
inst_view['nodes'] = {}
inst_view['nodes']['row_nodes'] = tmp_net.viz['row_nodes']
from .__init__ import Network
from copy import deepcopy
from . import calc_clust
sim_dict = {}
for inst_rc in which_sim:
sim_dict[inst_rc] = dm_to_sim(inst_dm[inst_rc], make_squareform=True,
filter_sim=filter_sim)
sim_net = {}
for inst_rc in which_sim:
sim_net[inst_rc] = deepcopy(Network())
sim_net[inst_rc].dat['mat'] = sim_dict[inst_rc]
sim_net[inst_rc].dat['nodes']['row'] = net.dat['nodes'][inst_rc]
sim_net[inst_rc].dat['nodes']['col'] = net.dat['nodes'][inst_rc]
sim_net[inst_rc].dat['node_info']['row'] = net.dat['node_info'][inst_rc]
sim_net[inst_rc].dat['node_info']['col'] = net.dat['node_info'][inst_rc]
calc_clust.cluster_row_and_col(sim_net[inst_rc])
all_views = []
df = sim_net[inst_rc].dat_to_df()
send_df = deepcopy(df)
sim_net[inst_rc].viz['views'] = all_views
rows_sorted = run_filter.get_sorted_rows(df['mat'], rank_type)
for inst_keep in keep_top:
tmp_df = deepcopy(df)
check_keep_num = inst_keep
# convert 'all' to -1 to clean up checking mechanism
if check_keep_num == 'all':
check_keep_num = -1
if check_keep_num < len(rows_sorted):
tmp_net = deepcopy(Network())
if inst_keep != 'all':
keep_rows = rows_sorted[0:inst_keep]
tmp_df['mat'] = tmp_df['mat'].loc[keep_rows]
if 'mat_orig' in tmp_df:
tmp_df['mat_orig'] = tmp_df['mat_orig'].loc[keep_rows]
tmp_df = run_filter.df_filter_col_sum(tmp_df, 0.001)
tmp_net.df_to_dat(tmp_df)
else:
tmp_net.df_to_dat(tmp_df)
def widget_df(self):
'''
Export a DataFrame from the front-end visualization. For instance, a user
can filter to show only a single cluster using the dendrogram and then
get a dataframe of this cluster using the widget_df method.
'''
if hasattr(self, 'widget_instance') == True:
if self.widget_instance.mat_string != '':
tmp_net = deepcopy(Network())
df_string = self.widget_instance.mat_string
tmp_net.load_file_as_string(df_string)
df = tmp_net.export_df()
return df
else:
return self.export_df()
else:
if hasattr(self, 'widget_class') == True:
print('Please make the widget before exporting the widget DataFrame.')
print('Do this using the widget method: net.widget()')
def df_filter_row_sum(df, threshold, take_abs=True):
''' filter rows in matrix at some threshold
and remove columns that have a sum below this threshold '''
from copy import deepcopy
from .__init__ import Network
net = Network()
if take_abs is True:
df_copy = deepcopy(df.abs())
else:
df_copy = deepcopy(df)
ini_rows = df_copy.index.values.tolist()
df_copy = df_copy.transpose()
tmp_sum = df_copy.sum(axis=0)
tmp_sum = tmp_sum.abs()
tmp_sum.sort_values(inplace=True, ascending=False)
tmp_sum = tmp_sum[tmp_sum > threshold]
keep_rows = sorted(tmp_sum.index.values.tolist())
if len(keep_rows) < len(ini_rows):
def df_filter_col_sum(df, threshold, take_abs=True):
''' filter columns in matrix at some threshold
and remove rows that have all zero values '''
from copy import deepcopy
from .__init__ import Network
net = Network()
if take_abs is True:
df_copy = deepcopy(df.abs())
else:
df_copy = deepcopy(df)
df_copy = df_copy.transpose()
df_copy = df_copy[df_copy.sum(axis=1) > threshold]
df_copy = df_copy.transpose()
df_copy = df_copy[df_copy.sum(axis=1) > 0]
if take_abs is True:
inst_rows = df_copy.index.tolist()
inst_cols = df_copy.columns.tolist()
df = grab_df_subset(df, inst_rows, inst_cols)