def write_mx_to_json(filename, mg, nNodes, pos, nLayers, nodes_to_remove=[]):
    G1 = nx.from_numpy_array(mx.adjacency_matrix(mg, weight='weight').todense())
    G1.remove_nodes_from(nodes_to_remove)
    G = nx.from_numpy_array(nx.adjacency_matrix(G1).todense())
    scalefact = 20
    L2_classes = np.arange(nLayers)
    L2_array_original = np.array([])
    z_shift = 2
    z_array_original = np.array([])
    x_orig = np.array([])
    y_orig = np.array([])
    L1_orig = np.array([])
    for level in L2_classes:
        L2_array_original = np.concatenate((L2_array_original, np.array([float(level) for i in np.arange(nNodes)])))
        z_array_original = np.concatenate((z_array_original, np.array([float((level * z_shift)) for i in np.arange(nNodes)])))
        x_orig = np.concatenate((x_orig, [(pos[key][0] + scalefact) for key in pos]))
        y_orig = np.concatenate((y_orig, [(pos[key][1] + scalefact) for key in pos]))
        L1_orig = np.concatenate((L1_orig, [i for i in np.arange(nNodes)]))
    L2_array = np.delete(L2_array_original, nodes_to_remove, 0)
    z_array = np.delete(z_array_original, nodes_to_remove, 0)
    x_array = np.delete(x_orig, nodes_to_remove, 0)
    y_array = np.delete(y_orig, nodes_to_remove, 0)
    L1_array = np.delete(L1_orig, nodes_to_remove, 0)
    id_dict = {i: ('id' + str(i)) for i in np.arange((nNodes * nLayers))}
    x_dict = {}
    y_dict = {}
    L2_dict = {i: l2 for (i, l2) in enumerate(L2_array)}
    z_dict = {i: z_val for (i, z_val) in enumerate(z_array)}
    x_dict = {i: x_val for (i, x_val) in enumerate(x_array)}
    y_dict = {i: y_val for (i, y_val) in enumerate(y_array)}
    L1_dict = {i: L1_val for (i, L1_val) in enumerate(L1_array)}
    nx.set_node_attributes(G, id_dict, name='name')
    nx.set_node_attributes(G, x_dict, name='x')
    nx.set_node_attributes(G, y_dict, name='y')
    nx.set_node_attributes(G, z_dict, name='z')
    nx.set_node_attributes(G, L1_dict, name='L1')
    nx.set_node_attributes(G, L2_dict, name='L2')
    G_json = json_graph.node_link_data(G)
    G_json_viz = json.dumps(G_json, indent=4)
    with open(filename, 'w') as fp:
        json.dump(G_json, fp)
    print(f'done writing mx to {filename}')
    return G_json_viz
def write_mx_to_json(filename, mg, nNodes, pos, nLayers, nodes_to_remove=[]):
    G1 = nx.from_numpy_array(mx.adjacency_matrix(mg, weight='weight').todense())
    G1.remove_nodes_from(nodes_to_remove)
    G = nx.from_numpy_array(nx.adjacency_matrix(G1).todense())
    scalefact = 20
    L2_classes = np.arange(nLayers)
    L2_array_original = np.array([])
    z_shift = 2
    z_array_original = np.array([])
    x_orig = np.array([])
    y_orig = np.array([])
    L1_orig = np.array([])
    for level in L2_classes:
        L2_array_original = np.concatenate((L2_array_original, np.array([float(level) for i in np.arange(nNodes)])))
        z_array_original = np.concatenate((z_array_original, np.array([float((level * z_shift)) for i in np.arange(nNodes)])))
        x_orig = np.concatenate((x_orig, [(pos[key][0] + scalefact) for key in pos]))
        y_orig = np.concatenate((y_orig, [(pos[key][1] + scalefact) for key in pos]))
        L1_orig = np.concatenate((L1_orig, [i for i in np.arange(nNodes)]))
    L2_array = np.delete(L2_array_original, nodes_to_remove, 0)
    z_array = np.delete(z_array_original, nodes_to_remove, 0)
    x_array = np.delete(x_orig, nodes_to_remove, 0)
    y_array = np.delete(y_orig, nodes_to_remove, 0)
    L1_array = np.delete(L1_orig, nodes_to_remove, 0)
    id_dict = {i: ('id' + str(i)) for i in np.arange((nNodes * nLayers))}
    x_dict = {}
    y_dict = {}
    L2_dict = {i: l2 for (i, l2) in enumerate(L2_array)}
    z_dict = {i: z_val for (i, z_val) in enumerate(z_array)}
    x_dict = {i: x_val for (i, x_val) in enumerate(x_array)}
    y_dict = {i: y_val for (i, y_val) in enumerate(y_array)}
    L1_dict = {i: L1_val for (i, L1_val) in enumerate(L1_array)}
    nx.set_node_attributes(G, id_dict, name='name')
    nx.set_node_attributes(G, x_dict, name='x')
    nx.set_node_attributes(G, y_dict, name='y')
    nx.set_node_attributes(G, z_dict, name='z')
    nx.set_node_attributes(G, L1_dict, name='L1')
    nx.set_node_attributes(G, L2_dict, name='L2')
    G_json = json_graph.node_link_data(G)
    G_json_viz = json.dumps(G_json, indent=4)
    with open(filename, 'w') as fp:
        json.dump(G_json, fp)
    print(f'done writing mx to {filename}')
    return G_json_viz