Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1e4f0a7
fix: allow doclist to have nested optional document
samsja Apr 27, 2023
54aa32f
Merge branch 'main' into fix-docarray-optional
Apr 28, 2023
35b41e0
refactor: rename test
samsja May 2, 2023
264bd84
refactor: allow doc vec column to be None
samsja May 3, 2023
910864f
refactor: add better error message when DocVec fail
samsja May 3, 2023
ba50e17
fix: add more test on none mode
samsja May 3, 2023
28d40ed
docs: update docstring for doc vec
samsja May 3, 2023
593578e
docs: add docstring to doc list
samsja May 3, 2023
733bfe6
docs: add docs about optional field
samsja May 3, 2023
b6c59cf
docs: add warning
samsja May 3, 2023
e07d1f6
feat: add integreation test
samsja May 3, 2023
43ccfbf
fix: apply joahnnes docs suggestion
samsja May 4, 2023
d482752
refactor: use none directly
samsja May 4, 2023
6ec5a4f
Merge branch 'fix-docarray-optional' of github.com:docarray/docarray …
samsja May 4, 2023
07bb27b
feat: apply johannes suggestion
samsja May 4, 2023
7107ccb
fix: fix is none
samsja May 4, 2023
9947cd2
feat: apply johannes suggestion
samsja May 4, 2023
1a78450
fix: fix last things
samsja May 4, 2023
123661b
Merge branch 'fix-docarray-optional' of github.com:docarray/docarray …
samsja May 4, 2023
49a142d
fix: ad missing check for nested optional doclist
samsja May 4, 2023
8ecc117
fix: fix documentation docs
samsja May 4, 2023
4c8eb1a
feat: apply johannes suggestion
samsja May 4, 2023
6ef95b6
fix: fix docs
samsja May 4, 2023
dcf07d2
fix: fix docs
samsja May 4, 2023
f60c1e3
Merge branch 'fix-docarrayoptional' of github.com:docarray/docarray i…
samsja May 4, 2023
cea57eb
feat: apply johannes suggestion
samsja May 5, 2023
38c80cc
docs: fix mistake
samsja May 5, 2023
cc00c9d
Merge branch 'main' into fix-docarray-optional
samsja May 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docarray/array/any_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __getattr__(self, item: str):
def _get_data_column(
self: T,
field: str,
) -> Union[MutableSequence, T, 'AbstractTensor']:
) -> Union[MutableSequence, T, 'AbstractTensor', None]:
"""Return all values of the fields from all docs this array contains

:param field: name of the fields to extract
Expand Down
8 changes: 8 additions & 0 deletions docarray/array/doc_list/doc_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ class Image(BaseDoc):
del docs[0:5] # remove elements for 0 to 5 from DocList
```

!!! note
If the DocList is homogeneous and its schema contains nested BaseDoc
(i.e, BaseDoc inside a BaseDoc) where the nested Document is `Optional`, calling
`docs.nested_doc` will return a List of the nested BaseDoc instead of DocList.
This is because the nested field could be None and therefore could not fit into
a DocList.

:param docs: iterable of Document

"""
Expand Down Expand Up @@ -200,6 +207,7 @@ def __class_getitem__(cls, item: Union[Type[BaseDoc], TypeVar, str]):

if (
not is_union_type(field_type)
and self.__class__.doc_type.__fields__[field].required
and isinstance(field_type, type)
and issubclass(field_type, BaseDoc)
):
Expand Down
48 changes: 39 additions & 9 deletions docarray/array/doc_vec/column_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Dict,
Iterable,
MutableMapping,
Optional,
Type,
TypeVar,
Union,
Expand Down Expand Up @@ -37,9 +38,9 @@ class ColumnStorage:

def __init__(
self,
tensor_columns: Dict[str, AbstractTensor],
doc_columns: Dict[str, 'DocVec'],
docs_vec_columns: Dict[str, ListAdvancedIndexing['DocVec']],
tensor_columns: Dict[str, Optional[AbstractTensor]],
doc_columns: Dict[str, Optional['DocVec']],
docs_vec_columns: Dict[str, Optional[ListAdvancedIndexing['DocVec']]],
any_columns: Dict[str, ListAdvancedIndexing],
tensor_type: Type[AbstractTensor] = NdArray,
):
Expand All @@ -63,12 +64,22 @@ def __len__(self) -> int:
def __getitem__(self: T, item: IndexIterType) -> T:
if isinstance(item, tuple):
item = list(item)
tensor_columns = {key: col[item] for key, col in self.tensor_columns.items()}
doc_columns = {key: col[item] for key, col in self.doc_columns.items()}
tensor_columns = {
key: col[item] if col is not None else None
for key, col in self.tensor_columns.items()
}
doc_columns = {
key: col[item] if col is not None else None
for key, col in self.doc_columns.items()
}
docs_vec_columns = {
key: col[item] for key, col in self.docs_vec_columns.items()
key: col[item] if col is not None else None
for key, col in self.docs_vec_columns.items()
}
any_columns = {
key: col[item] if col is not None else None
for key, col in self.any_columns.items()
}
any_columns = {key: col[item] for key, col in self.any_columns.items()}

return self.__class__(
tensor_columns,
Expand All @@ -91,15 +102,34 @@ def __init__(self, index: int, storage: ColumnStorage):
def __getitem__(self, name: str) -> Any:
if name in self.storage.tensor_columns.keys():
tensor = self.storage.tensor_columns[name]
if tensor is None:
return None
if tensor.get_comp_backend().n_dim(tensor) == 1:
# to ensure consistensy between numpy and pytorch
# we wrap the scalr in a tensor of ndim = 1
# otherwise numpy pass by value whereas torch by reference
return self.storage.tensor_columns[name][self.index : self.index + 1]
col = self.storage.tensor_columns[name]

return self.storage.columns[name][self.index]
if col is not None:
return col[self.index : self.index + 1]
else:
return None

col = self.storage.columns[name]

if col is None:
return None
return col[self.index]

def __setitem__(self, name, value) -> None:
if self.storage.columns[name] is None:
raise ValueError(
f'Cannot set an item to a None column. This mean that '
f'the DocVec that encapsulate this doc has the field '
f'{name} set to None. If you want to modify that you need to do it at the'
f'DocVec level. `docs.field = np.zeros(10)`'
)

self.storage.columns[name][self.index] = value

def __delitem__(self, key):
Expand Down
Loading