Skip to content

VarshneyDataReader

VarshneyDataReader

Bases: ConnectomeDataset

Reader for Varshney et al. 2011 connectivity dataset

Source code in cect/readers/VarshneyDataReader.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class VarshneyDataReader(ConnectomeDataset):
    """Reader for Varshney et al. 2011 connectivity dataset"""

    def __init__(self, include_nmj=False):
        ConnectomeDataset.__init__(self)

        self.typed_conns = {
            SEND_SYN: [],
            SEND_POLY_SYN: [],
            RECEIVE_SYN: [],
            RECEIVE_POLY_SYN: [],
            SEND_ANY: set(),
            RECEIVE_ANY: set(),
            ELECT_JUNC_SYN: [],
            NMJ_ENDPOINT: [],
        }

        self.include_nmj = include_nmj

        cells, neuron_conns = self.read_data()

        for conn in neuron_conns:
            self.add_connection_info(
                conn,
                append_existing_connections=True,
                check_overwritten_connections=True,
                fail_on_any_repeated_connection=False,
            )

    def _check_valid_synapse_type(self, syn_type):
        if syn_type not in self.typed_conns:
            raise ValueError(
                f"Synapse type '{syn_type}' not recognized for {NAME}. Valid types are: {list(self.typed_conns.keys())}"
            )
        return syn_type

    def read_data(self):
        cells = []
        conns = []

        if filename.endswith(".xls"):
            from xlrd import open_workbook

            wb = open_workbook(filename)
            rows = []
            sheet = wb.sheet_by_index(0)
            for row in range(1, sheet.nrows):
                rows.append(
                    (
                        str(sheet.cell(row, 0).value),
                        str(sheet.cell(row, 1).value),
                        str(sheet.cell(row, 2).value),
                        int(sheet.cell(row, 3).value),
                    )
                )
        else:
            wb = load_workbook(filename)
            sheet = wb.worksheets[0]
            rows = sheet.iter_rows(min_row=2, values_only=True)

        print_("Opened the Excel file: " + filename)

        for row in rows:  # Assuming data starts from the second row
            pre = str(row[0])
            post = str(row[1])

            pre = _remove_leading_index_zero(pre)
            post = _remove_leading_index_zero(post)

            if post == NMJ_ENDPOINT:
                post = UNSPECIFIED_BODY_WALL_MUSCLE

            syntype_here = self._check_valid_synapse_type(str(row[2]))
            num = int(row[3])

            self.typed_conns[syntype_here].append(f"{pre}_{post}_{num}")
            if syntype_here in [SEND_SYN, SEND_POLY_SYN]:
                self.typed_conns[SEND_ANY].add(f"{pre}_{post}")
            elif syntype_here in [RECEIVE_SYN, RECEIVE_POLY_SYN]:
                self.typed_conns[RECEIVE_ANY].add(f"{pre}_{post}")

            if syntype_here != NMJ_ENDPOINT or self.include_nmj:
                synclass = (
                    GENERIC_ELEC_SYN_CLASS
                    if syntype_here == ELECT_JUNC_SYN
                    else GENERIC_CHEM_SYN_CLASS
                    if (syntype_here == SEND_POLY_SYN or syntype_here == SEND_SYN)
                    else GENERIC_CHEM_SYN_CLASS
                    if (syntype_here == NMJ_ENDPOINT)
                    else None
                )

                if syntype_here == ELECT_JUNC_SYN:
                    syntype = ELECTRICAL_SYN_TYPE
                else:
                    syntype = CHEMICAL_SYN_TYPE

                if synclass is not None:
                    conns.append(ConnectionInfo(pre, post, num, syntype, synclass))
                    if pre not in cells:
                        cells.append(pre)
                    if post not in cells:
                        cells.append(post)
                else:
                    if not (
                        syntype_here == RECEIVE_SYN or syntype_here == RECEIVE_POLY_SYN
                    ):
                        raise ValueError(
                            f"Warning: Unrecognized synapse type '{syntype_here}' for connection {pre} -> {post} for {NAME}."
                        )

        total = 0
        for syn_type, conn_list in self.typed_conns.items():
            total += len(conn_list)
            info = ""
            if syn_type in [SEND_SYN, SEND_POLY_SYN, RECEIVE_SYN, RECEIVE_POLY_SYN]:
                info = f"\t({', '.join(conn_list[:5])}..., {conn_list[-1]})"

            print_(
                f"  {syn_type}: {len(conn_list)} connections\t({len(set(conn_list))} unique) {info}"
            )

        print_(f"  Total: {total} connections (half: {total / 2})")

        s_tot = len(self.typed_conns[SEND_SYN]) + len(self.typed_conns[SEND_POLY_SYN])

        r_tot = len(self.typed_conns[RECEIVE_SYN]) + len(
            self.typed_conns[RECEIVE_POLY_SYN]
        )

        print_(
            f"  Total chemical synapses: {s_tot} (send) + {r_tot} (receive) = {s_tot + r_tot}"
        )
        gj = len(self.typed_conns[ELECT_JUNC_SYN])
        print_(f"  Total electrical synapses: {gj}, half: {gj / 2}")

        return cells, conns

    def read_muscle_data(self):
        conns = []
        neurons = []
        muscles = []

        return neurons, muscles, conns