Skip to content
This repository was archived by the owner on Sep 9, 2026. It is now read-only.

Commit 264c8ab

Browse files
committed
fix(array): fix plotting
1 parent 6e6c54a commit 264c8ab

7 files changed

Lines changed: 26 additions & 13409 deletions

File tree

‎docarray/array/mixins/io/csv.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def save_csv(
3434
flatten_tags: bool = True,
3535
exclude_fields: Optional[Sequence[str]] = None,
3636
dialect: Union[str, 'csv.Dialect'] = 'excel',
37+
with_header: bool = True,
3738
) -> None:
3839
"""Save array elements into a CSV file.
3940
@@ -52,13 +53,13 @@ def save_csv(
5253

5354
with file_ctx as fp:
5455
if flatten_tags and self[0].tags:
55-
keys = list(self[0].to_dict().keys()) + list(
56+
keys = list(self[0].non_empty_fields) + list(
5657
f'tag__{k}' for k in self[0].tags
5758
)
5859
keys.remove('tags')
5960
else:
6061
flatten_tags = False
61-
keys = list(self[0].to_dict().keys())
62+
keys = list(self[0].non_empty_fields)
6263

6364
if exclude_fields:
6465
for k in exclude_fields:
@@ -67,7 +68,9 @@ def save_csv(
6768

6869
writer = csv.DictWriter(fp, fieldnames=keys, dialect=dialect)
6970

70-
writer.writeheader()
71+
if with_header:
72+
writer.writeheader()
73+
7174
from docarray import Document
7275

7376
for d in self:

‎docarray/array/mixins/plot.py‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,17 @@ def plot_embeddings(
7272
)
7373

7474
self.save_embeddings_csv(os.path.join(path, emb_fn), delimiter='\t')
75+
76+
_exclude_fields = ('embedding', 'blob', 'scores')
77+
with_header = True
78+
if len(set(self[0].non_empty_fields).difference(set(_exclude_fields))) <= 1:
79+
with_header = False
80+
7581
self.save_csv(
7682
os.path.join(path, meta_fn),
77-
exclude_fields=('embedding', 'blob', 'scores'),
83+
exclude_fields=_exclude_fields,
7884
dialect='excel-tab',
85+
with_header=with_header,
7986
)
8087

8188
_epj_config = {
@@ -103,10 +110,12 @@ def plot_embeddings(
103110
with open(os.path.join(path, config_fn), 'w') as fp:
104111
json.dump(_epj_config, fp)
105112

106-
shutil.copyfile(
107-
os.path.join(__resources_path__, 'embedding-projector/index.html'),
108-
os.path.join(path, 'index.html'),
109-
)
113+
import gzip
114+
115+
with gzip.open(
116+
os.path.join(__resources_path__, 'embedding-projector/index.html.gz'), 'rt'
117+
) as fr, open(os.path.join(path, 'index.html'), 'w') as fp:
118+
fp.write(fr.read())
110119

111120
if start_server:
112121

@@ -153,7 +162,6 @@ def _get_fastapi_app():
153162
t_m.join()
154163
return path
155164

156-
157165
def plot_image_sprites(
158166
self,
159167
output: Optional[str] = None,

‎docarray/base.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,7 @@ def __eq__(self, other):
109109
if type(self) is type(other):
110110
return self._data == other._data
111111
return False
112+
113+
def __iter__(self):
114+
for f in self.non_empty_fields:
115+
yield f, getattr(self, f)

‎docarray/proto/io/__init__.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ def parse_proto(pb_msg: 'DocumentProto') -> 'Document':
3535

3636
def flush_proto(doc: 'Document') -> 'DocumentProto':
3737
pb_msg = DocumentProto()
38-
for key in doc.non_empty_fields:
38+
for key, value in doc:
3939
try:
40-
value = getattr(doc, key)
4140
if key in ('blob', 'embedding'):
4241
flush_ndarray(getattr(pb_msg, key), value)
4342
elif key in ('chunks', 'matches'):

‎docarray/resources/embedding-projector/index.html‎

Lines changed: 0 additions & 13397 deletions
This file was deleted.
483 KB
Binary file not shown.

‎docarray/score/mixins/representer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ def __repr__(self):
33
return repr(self.to_dict())
44

55
def to_dict(self):
6-
return {f: getattr(self, f) for f in self.non_empty_fields}
6+
return {f: v for f, v in self}

0 commit comments

Comments
 (0)