Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key Error Updating Data with selected Features #2

Open
dawbarn opened this issue Mar 30, 2023 · 0 comments
Open

Key Error Updating Data with selected Features #2

dawbarn opened this issue Mar 30, 2023 · 0 comments

Comments

@dawbarn
Copy link

dawbarn commented Mar 30, 2023

When running the cell:

2.6 Update dataset with selected features

I get 'KeyError: 'normal_e' at line 16. The error shows that I am trying to access a column named "normal_e", which is not present in the data.

I got it around the problem by implementing:

            if 'normal_e' not in data[adj_matrix.value]:
                data[adj_matrix.value]['normal_e'] = []
            if 'normal_x' not in data[adj_matrix.value]:
                data[adj_matrix.value]['normal_x'] = []

I applied this between under each of the if and elif statements, so it looked like:

def filter_features(data, gender=None):
    """
    gender: either None, 'm' or 'w'. Use m/w when trying out gender-aware model, this will add an extra node_feature
    the node feature will be 0 (m) and 1 (f). This is only relevent after loading both the M and W datasets (not implemented atm)
    """
    edge_feature_idxs = [idx for idx, x in enumerate(edge_f_box.children) if x.value]
    node_feature_idxs = [idx for idx, x in enumerate(node_f_box.children) if x.value]
    node_features = [x.description for x in node_f_box.children if x.value]

    # Check for empty edge features or node features.
    if not any(edge_feature_idxs) and not any(node_feature_idxs):
        print("\nCannot have zero edge features and zero node features.\n")
        print("\nDefaulting to the previous configuration.")    
    else:    
        if not gender:
            if 'normal_e' not in data[adj_matrix.value]:
                data[adj_matrix.value]['normal_e'] = []
            if 'normal_x' not in data[adj_matrix.value]:
                data[adj_matrix.value]['normal_x'] = []
            data[adj_matrix.value]['normal_e'] = [x[:, edge_feature_idxs] for x in data[adj_matrix.value]['normal_e']]
            data[adj_matrix.value]['normal_x'] = [x[:, node_feature_idxs] for x in data[adj_matrix.value]['normal_x']]
        elif gender == 'm':
            if 'normal_e' not in data[adj_matrix.value]:
                data[adj_matrix.value]['normal_e'] = []
            if 'normal_x' not in data[adj_matrix.value]:
                data[adj_matrix.value]['normal_x'] = []
            data[adj_matrix.value]['normal_e'] = [x[:, edge_feature_idxs] for x in data[adj_matrix.value]['normal_e']]
            data[adj_matrix.value]['normal_x'] = [np.append(
                x[:, node_feature_idxs], 
                np.zeros(shape=(x.shape[0], 1)), axis=1
            ) for x in data[adj_matrix.value]['normal_x']]
        elif gender == 'w':
            if 'normal_e' not in data[adj_matrix.value]:
                data[adj_matrix.value]['normal_e'] = []
            if 'normal_x' not in data[adj_matrix.value]:
                data[adj_matrix.value]['normal_x'] = []
            data[adj_matrix.value]['normal_e'] = [x[:, edge_feature_idxs] for x in data[adj_matrix.value]['normal_e']]
            data[adj_matrix.value]['normal_x'] = [np.append(
                x[:, node_feature_idxs], 
                np.ones(shape=(x.shape[0], 1)), axis=1
            ) for x in data[adj_matrix.value]['normal_x']]
        else:
            raise NotImplementedError()
    
    return data

data = filter_features(og_data.copy())

However, I'm aware there may be another fix using .loc. Just a thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant