def neuron_proof_with_restriction(verbose=False,
                                  query_type="dendrite",
                                  add_messy_and_multi_soma_filter=True,
                                  **kwargs):
    """
    
    
    Ex 1: only dendrite restriction: 
    import ease_utils as ez
    restr_table = ez.neuron_proof_with_restriction(query_type = ["dendrite"],verbose = True)
    restr_table
    
    Ex 2: soma and dednrite restriction
    restr_table = ez.neuron_proof_with_restriction(verbose = True)
    restr_table
    
    """

    curr_query = ez.ease_query(query_type=query_type,
                               verbose=verbose,
                               **kwargs)
    if add_messy_and_multi_soma_filter:
        curr_table_before = mgf.neurons_minus_false_and_multi_soma_mergers(
            add_allen_e_i_filter=False)
    else:
        curr_table_before = du.proofreading_neurons_table()

    curr_table = curr_table_before & curr_query

    if verbose:
        print(f"number of neurons in restriction: {len(curr_table)}")

    return curr_table
def node_names_from_manual_cell_type(cell_type,verbose = False):
    """
    Purpose: Return all the node names for the cells that were manually proofread
    """
    seg_ids,sp_idxs = (du.proofreading_neurons_table() & (du.manauL_cell_types_table() & f"cell_type='{cell_type}'").proj()).fetch("segment_id","split_index")
    node_names = [pv.node_name(s_i,sp_i) for s_i,sp_i in zip(seg_ids,sp_idxs)]
    if verbose:
        print(f"Found {len(node_names)} {cell_type} cells")
    return node_names
def restrict_proof_neuron_table(segment_id,
                               split_index = None):
    """
    Purpose: to restrict the proofreading 
    table by the node name
    """
    segement_id,split_index = pv.segment_id_and_split_index(segment_id,split_index)

    return du.proofreading_neurons_table() & dict(segment_id=segment_id,split_index = split_index)
def add_node_attributes_to_proofread_graph(G,
                                           attributes=None,
                                           add_visual_area=True,
                                           debug=False):
    """
    Pseudocode: 
    1) Download all of the attributes want to store in the nodes
    2) Create a dictionar mapping the nuclei to a dict of attribute values
    3) set the attributes of the original graph
    """
    if attributes is None:
        attributes_to_download = [
            "spine_category",
            "cell_type_predicted",
            "n_axons",
            "axon_length",
            "n_apicals",
            "n_spines",
            "n_boutons",
            "n_nuclei_in_radius",
            "skeletal_length",
        ]

    if "nucleus_id" not in attributes_to_download:
        attributes_to_download.append("nucleus_id")

    if add_visual_area:
        for s_t in ["soma_x", "soma_y", "soma_z"]:
            if s_t not in attributes_to_download:
                attributes_to_download.append(s_t)

    neuron_data = du.proofreading_neurons_table().fetch(
        *attributes_to_download, as_dict=True)

    if add_visual_area:
        soma_points = np.array([[k["soma_x"], k["soma_y"], k["soma_z"]]
                                for k in neuron_data])
        visual_area_labels = mru.EM_coordinates_to_visual_areas(soma_points)
        layer_labels = mru.EM_coordinates_to_layer(soma_points)
        if debug:
            print(f"soma_points.shape = {soma_points.shape}")
            print(f"visual_area_labels.shape = {visual_area_labels.shape}")

    attr_dict = dict()
    for j, k in enumerate(neuron_data):
        curr_dict = {k1: v for k1, v in k.items() if k != "nucleus_id"}

        if add_visual_area:
            curr_dict["visual_area"] = visual_area_labels[j]
            curr_dict["layer"] = layer_labels[j]

        attr_dict[k["nucleus_id"]] = curr_dict

    nx.set_node_attributes(G, attr_dict)
    return G
"""
False somas or messy neuron filters


"""

import datajoint_utils as du

default_min_n_syn_soma = 3
proof_version = 6
#proof_version = 7

neuron_proof_table = du.proofreading_neurons_table(version = proof_version)

allen_e_i_filter = "(allen_e_i is NULL) or (allen_e_i = baylor_e_i)"

small_n_syn_low_dendrite_median_exc_inh = (neuron_proof_table & 
            "((n_syn_soma <= 17) AND (cell_type = 'excitatory') AND (dendrite_branch_length_mean < 35))" 
            f" OR "
            "((n_syn_soma <= 17) AND (cell_type = 'inhibitory') AND (dendrite_branch_length_mean < 28))" ).proj()
    
def small_n_syn_soma_errors(min_n_syn_soma = default_min_n_syn_soma):
    #aprint(f"min_n_syn_soma = {min_n_syn_soma}")
    small_n_syn_soma_errors_table= (neuron_proof_table & f"n_syn_soma <= {min_n_syn_soma}").proj()
    return small_n_syn_soma_errors_table

def false_messy_soma_filter(min_n_syn_soma = default_min_n_syn_soma):
    
    """
    Code to verify this restriction: 
    
def restricted_proof_table_by_node_names(node_names):
    return du.proofreading_neurons_table() & restriction_list_by_node_names(node_names)