Exemple #1
0
def edges_as_df(
        *circuits: Circuit,
        sanitise=False) -> Tuple[pd.DataFrame, Dict[Circuit, pd.DataFrame]]:
    """
    circuit, pre_id, pre_name, pre_side, pre_segment, post_id, post_name, post_side, post_segment, contact_number, synaptic_area

    :param circuits:
    :return:
    """
    if not circuits:
        circuits = sorted(Circuit, key=lambda x: str(x).lower())

    headers = ("circuit", "pre_id", "pre_name", "pre_side", "pre_segment",
               "post_id", "post_name", "post_side", "post_segment",
               "contact_number", "synaptic_area")

    all_data = []
    per_circuit = dict()

    for circuit in circuits:
        g = multidigraph_to_digraph(get_data(circuit))
        rows = []
        total_count = 0

        for pre, post, edata in iter_data(g):
            row = [circuit]
            for node in (pre, post):
                row.extend([node.id, node.name, node.side, node.segment])
            row.append(edata["count"])
            row.append(edata["area"])
            assert len(row) == len(headers)
            if sanitise:
                row = sanitise_row(row)
            rows.append(row)

            total_count += edata["count"]

        logger.info(f"Total synapse count for %s: %s", circuit, total_count)
        rows = sorted(rows)
        per_circuit[circuit] = pd.DataFrame(data=rows, columns=headers)
        all_data.extend(rows)

    return pd.DataFrame(data=all_data, columns=headers), per_circuit
    def __init__(self, graph: nx.MultiDiGraph, name=None):
        """
        Note: this is made more complicated by:

        - some ambiguous chordotonals (e.g. lch5-2/4)
            - resolved by summing their counts/areas

        - some LNs which do not have a side (e.g. Ladder)
            - Resolved by defining edge side by the postsynaptic partner

        Assumes that only presynaptic neurons can be ambiguous,
        and that that all postsynaptic partners have a side,
        and that there are no contralateral edges.
        """
        super().__init__(graph, name)
        self.graph = multidigraph_to_digraph(graph)
        # self.graph, self.filtered_nodes = self._filter_unilateral_nodes()
        # self.graph, self.filtered_edges = self._filter_unilateral_edges()

        self.name = name
Exemple #3
0
def df_to_graph(detected_df: pd.DataFrame) -> nx.DiGraph:
    g = nx.MultiDiGraph()

    syn_to_skels = defaultdict(list)
    for row in detected_df.itertuples():
        syn_to_skels[row.synapse_object_id].append(row)
        if row.skeleton_id not in g.nodes:
            g.add_node(row.skeleton_id, synapses=dict())
        g.node[row.skeleton_id]["synapses"][row.synapse_object_id] = (
            row.contact_px * PX_AREA)

    for this_skid, synapses in g.nodes(data="synapses"):
        for synapse_id, contact_px in synapses.items():
            for row in syn_to_skels[synapse_id]:
                if row.skeleton_id != this_skid:
                    g.add_edge(
                        this_skid,
                        row.skeleton_id,
                        area=row.contact_px * PX_AREA,
                        synapse_object_id=synapse_id,
                        pre_area=contact_px * PX_AREA,
                    )

    return multidigraph_to_digraph(g)
Exemple #4
0
 def __init__(self, graph: nx.MultiDiGraph, name=""):
     super().__init__(graph, name)
     self.graph = multidigraph_to_digraph(self.graph)
Exemple #5
0
        done_edges.add((pre, post))
        done_edges.add((post, pre))

    erroneous_edges = set()

    for pre_post in g_auto.edges:
        if pre_post not in done_edges:
            erroneous_edges.add(pre_post)

    print(f"{len(erroneous_edges)} erroneous edges found: ")
    for pre, post in sorted(erroneous_edges):
        print(f"\t{pre} -> {post}")

    fig, ax_arr = plt.subplots(1, 2)
    count_ax, area_ax = ax_arr.flatten()

    manual_auto_scatter(manual_counts, auto_counts, "Contact number", count_ax)
    manual_auto_scatter(manual_areas, auto_areas, "Contact area", area_ax)


if __name__ == "__main__":
    catmaid = get_catmaid()
    multi_g = get_data("ORN-PN")
    g_manual = multidigraph_to_digraph(multi_g)
    skids = list(g_manual.nodes)
    detected_df = catmaid.get_detected_synapses_between(*skids)
    g_auto = df_to_graph(detected_df)

    compare_edges(g_manual, g_auto)
    plt.show()
data_dir = TABLES_DIR / "out" / "count_frac_area"
data_dir.mkdir(parents=True, exist_ok=True)

HEADERS = ("circuit", "pre_id", "pre_name", "pre_side", "pre_segment",
           "post_id", "post_name", "post_side", "post_segment",
           "contact_number", "contact_fraction", "synaptic_area")

all_data = []

tgt_total_counts_df = pd.read_csv(CATMAID_CSV_DIR /
                                  "dendritic_synapse_counts.csv",
                                  index_col=0)

for circuit in Circuit:
    g = multidigraph_to_digraph(get_data(circuit))
    rows = []
    total_count = 0

    for pre, post, edata in iter_data(g):
        row = [str(circuit)]
        for node in (pre, post):
            row.extend([node.id, node.name, str(node.side), str(node.segment)])
        row.append(edata["count"])

        frac = edata["count"] / tgt_total_counts_df["post_count"].loc[post.id]
        row.append(frac)

        row.append(edata["area"])
        rows.append(row)