Skip to content

[loaders-] guess types (hdf5), understand unsigned int type (npy) #2713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
4 changes: 3 additions & 1 deletion visidata/loaders/_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def save_dta(vd, p, *sheets):
vs = sheets[0]

columns = [col.name for col in vs.visibleCols]

# Get data types
types = list()
dispvals = next(vs.iterdispvals(format=True))
Expand Down Expand Up @@ -154,6 +154,8 @@ def reload(self):
readfunc = self.read_tsv
elif filetype == 'jsonl':
readfunc = partial(pd.read_json, lines=True)
elif filetype == 'hdf5':
readfunc = partial(pd.read_hdf, lines=True)
else:
readfunc = getattr(pd, 'read_'+filetype) or vd.error('no pandas.read_'+filetype)
# readfunc() handles binary and text open()
Expand Down
12 changes: 9 additions & 3 deletions visidata/loaders/hdf5.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from visidata import VisiData, vd, Sheet, Path, Column, ItemColumn, BaseSheet
from visidata import VisiData, vd, Sheet, Path, Column, ColumnItem, ItemColumn, BaseSheet, anytype
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: ItemColumn is the preferred nomenclature; ColumnItem is deprecated. (All column types are named FooColumn.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


@VisiData.api
def open_h5(vd, p):
Expand Down Expand Up @@ -28,8 +28,8 @@ def iterload(self):
elif isinstance(source, h5py.Dataset):
if len(source.shape) == 1:
if source.dtype.names:
for i, colname in enumerate(source.dtype.names):
self.addColumn(ItemColumn(colname, colname), index=i)
for i, (colname, fmt, *_) in enumerate(source.dtype.descr):
self.addColumn(ColumnItem(colname, i, type=_guess_type(fmt)))
yield from source # copy
else:
self.addColumn(ItemColumn(source.name, 0))
Expand Down Expand Up @@ -59,5 +59,11 @@ def openRow(self, row):
if isinstance(row, numpy.ndarray):
return NpySheet(None, npy=row)

def _guess_type(fmt):
if 'i' in fmt or 'u' in fmt:
return int
elif 'f' in fmt:
return float
return anytype

Hdf5ObjSheet.addCommand('A', 'dive-metadata', 'vd.push(SheetDict(cursorRow.name + "_attrs", source=cursorRow.attrs))', 'open metadata sheet for object referenced in current row')
2 changes: 1 addition & 1 deletion visidata/loaders/npy.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def reloadCols(self):
elif 'M' in fmt:
self.addColumn(Column(name, type=date, getter=lambda c,r,i=i: str(r[i])))
continue
elif 'i' in fmt:
elif 'i' in fmt or 'u' in fmt:
t = int
elif 'f' in fmt:
t = float
Expand Down