From 20042b0ab03c72d19886b03caf7c5b26bc17f9b7 Mon Sep 17 00:00:00 2001 From: audun myers Date: Thu, 11 Apr 2024 12:26:17 -0400 Subject: [PATCH 1/3] updating index column naming for factory also fixed incidence store unused column. --- hypernetx/classes/factory.py | 28 ++++++++++++++++++++++++---- hypernetx/classes/incidence_store.py | 2 -- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/hypernetx/classes/factory.py b/hypernetx/classes/factory.py index c3f99f52..d68ca1f7 100644 --- a/hypernetx/classes/factory.py +++ b/hypernetx/classes/factory.py @@ -83,6 +83,15 @@ def grabweight(cell): # remove duplicate indices and aggregate using aggregation methods specified. dfp = dfp[~dfp.index.duplicated(keep="first")] + + #rename index columns if necessary + if level == 0 or level == 1: + #rename index column to 'uid' + dfp.index.names = ['uid'] + elif level == 2: + #rename index columns to 'edges' and 'nodes' + dfp.index.names = ['edges', 'nodes'] + return dfp @@ -231,17 +240,28 @@ def dict_factory_method( attribute_df = pd.DataFrame(attribute_data) DF = pd.concat([DF, attribute_df], axis=1) - # id the dataeframe is for edges or nodes. + # if the dictionary is for edges or nodes. elif level == 1 or level == 0: attribute_data = [] for key in D: attributes_of_key = D[key] attribute_data.append(attributes_of_key) + + attribute_data = {weight_col: [], misc_properties_col: []} + for uid in D.keys(): + if isinstance(D[uid], dict): + attributes_of_uid = D[uid] + if weight_col in attributes_of_uid: + weight_val = attributes_of_uid.pop(weight_col) + attribute_data[weight_col] += [weight_val] + else: + attribute_data[weight_col] += [default_weight] + attribute_data[misc_properties_col] += [attributes_of_uid] + attribute_df = pd.DataFrame(attribute_data) DF = pd.concat( - [pd.DataFrame(list(D.keys()), columns=uid_cols), attribute_df], axis=1 - ) - + [pd.DataFrame(list(D.keys())), attribute_df], axis=1) + # get property store from dataframe PS = dataframe_factory_method( DF, diff --git a/hypernetx/classes/incidence_store.py b/hypernetx/classes/incidence_store.py index 2f269f35..3747d31e 100644 --- a/hypernetx/classes/incidence_store.py +++ b/hypernetx/classes/incidence_store.py @@ -212,10 +212,8 @@ def restrict_to(self, level, items, inplace=False): def equivalence_classes(self, level=0): if level == 0: old_dict = self._elements - col = "edges" elif level == 1: old_dict = self._memberships - col = "nodes" else: return self.data From e9dc62d5c9db20435d58ec5cde02a9573e0659f1 Mon Sep 17 00:00:00 2001 From: audun myers Date: Thu, 11 Apr 2024 14:24:19 -0400 Subject: [PATCH 2/3] Fixed PR comments from mark --- hypernetx/classes/factory.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/hypernetx/classes/factory.py b/hypernetx/classes/factory.py index d68ca1f7..fa531891 100644 --- a/hypernetx/classes/factory.py +++ b/hypernetx/classes/factory.py @@ -242,15 +242,11 @@ def dict_factory_method( # if the dictionary is for edges or nodes. elif level == 1 or level == 0: - attribute_data = [] - for key in D: - attributes_of_key = D[key] - attribute_data.append(attributes_of_key) attribute_data = {weight_col: [], misc_properties_col: []} - for uid in D.keys(): - if isinstance(D[uid], dict): - attributes_of_uid = D[uid] + for data_uid in D.values(): + if isinstance(data_uid, dict): + attributes_of_uid = data_uid if weight_col in attributes_of_uid: weight_val = attributes_of_uid.pop(weight_col) attribute_data[weight_col] += [weight_val] From 307016673f2e2bc77eee75081d7b8a9abeb9bf5c Mon Sep 17 00:00:00 2001 From: Mark Bonicillo Date: Thu, 11 Apr 2024 11:36:21 -0700 Subject: [PATCH 3/3] Fix formatting --- hypernetx/classes/factory.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/hypernetx/classes/factory.py b/hypernetx/classes/factory.py index fa531891..b838d363 100644 --- a/hypernetx/classes/factory.py +++ b/hypernetx/classes/factory.py @@ -83,15 +83,15 @@ def grabweight(cell): # remove duplicate indices and aggregate using aggregation methods specified. dfp = dfp[~dfp.index.duplicated(keep="first")] - - #rename index columns if necessary + + # rename index columns if necessary if level == 0 or level == 1: - #rename index column to 'uid' - dfp.index.names = ['uid'] + # rename index column to 'uid' + dfp.index.names = ["uid"] elif level == 2: - #rename index columns to 'edges' and 'nodes' - dfp.index.names = ['edges', 'nodes'] - + # rename index columns to 'edges' and 'nodes' + dfp.index.names = ["edges", "nodes"] + return dfp @@ -242,7 +242,7 @@ def dict_factory_method( # if the dictionary is for edges or nodes. elif level == 1 or level == 0: - + attribute_data = {weight_col: [], misc_properties_col: []} for data_uid in D.values(): if isinstance(data_uid, dict): @@ -253,11 +253,10 @@ def dict_factory_method( else: attribute_data[weight_col] += [default_weight] attribute_data[misc_properties_col] += [attributes_of_uid] - + attribute_df = pd.DataFrame(attribute_data) - DF = pd.concat( - [pd.DataFrame(list(D.keys())), attribute_df], axis=1) - + DF = pd.concat([pd.DataFrame(list(D.keys())), attribute_df], axis=1) + # get property store from dataframe PS = dataframe_factory_method( DF,