add id to index name on aligneddynamictable correctly

This commit is contained in:
sneakers-the-rat 2024-09-11 21:16:37 -07:00
parent 91b2abf07e
commit 95fbce1c4a
Signed by untrusted user who does not match committer: jonny
GPG key ID: 6DCB96EF1E4D232D
14 changed files with 97 additions and 97 deletions

View file

@ -627,23 +627,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -651,8 +652,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -551,13 +551,13 @@ def test_aligned_dynamictable_indexing(aligned_table):
row.columns
== pd.MultiIndex.from_tuples(
[
("table1", "index"),
("table1", "id"),
("table1", "col1"),
("table1", "col2"),
("table2", "index"),
("table2", "id"),
("table2", "col3"),
("table2", "col4"),
("table3", "index"),
("table3", "id"),
("table3", "col5"),
("table3", "col6"),
]
@ -754,11 +754,11 @@ def test_aligned_dynamictable_ictable(intracellular_recordings_table):
rows.columns
== pd.MultiIndex.from_tuples(
[
("electrodes", "index"),
("electrodes", "id"),
("electrodes", "electrode"),
("stimuli", "index"),
("stimuli", "id"),
("stimuli", "stimulus"),
("responses", "index"),
("responses", "id"),
("responses", "response"),
]
)

View file

@ -704,23 +704,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -728,8 +729,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -704,23 +704,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -728,8 +729,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -704,23 +704,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -728,8 +729,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any:

View file

@ -706,23 +706,24 @@ class AlignedDynamicTableMixin(BaseModel):
ids = self.id[item]
if not isinstance(ids, Iterable):
ids = pd.Series([ids])
ids = pd.DataFrame({"id": ids}, index=pd.Index(data=ids, name="id"))
tables = [ids]
ids = pd.Index(data=ids, name="id")
tables = []
for category_name, category in self._categories.items():
table = category[item]
if isinstance(table, pd.DataFrame):
table = table.reset_index()
table.index = ids
elif isinstance(table, np.ndarray):
table = pd.DataFrame({category_name: [table]}, index=ids.index)
table = pd.DataFrame({category_name: [table]}, index=ids)
elif isinstance(table, Iterable):
table = pd.DataFrame({category_name: table}, index=ids.index)
table = pd.DataFrame({category_name: table}, index=ids)
else:
raise ValueError(
f"Don't know how to construct category table for {category_name}"
)
tables.append(table)
names = [self.name] + self.categories
# names = [self.name] + self.categories
# construct below in case we need to support array indexing in the future
else:
raise ValueError(
@ -730,8 +731,7 @@ class AlignedDynamicTableMixin(BaseModel):
"need an int, string, slice, ndarray, or tuple[int | slice, str]"
)
df = pd.concat(tables, axis=1, keys=names)
df.set_index((self.name, "id"), drop=True, inplace=True)
df = pd.concat(tables, axis=1, keys=self.categories)
return df
def __getattr__(self, item: str) -> Any: