Skip to content

Commit

Permalink
Updated io.interface_utils to be compatible with new ipyfilechooser v…
Browse files Browse the repository at this point in the history
…ersion.

Added test to catch braking changes in the future.
  • Loading branch information
gduscher committed Oct 22, 2021
1 parent ed7beb0 commit 3b4a630
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 39 deletions.
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

# TODO: Move requirements to requirements.txt
requirements = ['numpy>=1.10',
'toolz', # dask installation failing without this
'cytoolz', # dask installation failing without this
'toolz', # dask installation failing without this
'cytoolz', # dask installation failing without this
'dask>=0.10',
'h5py>=2.6.0',
'matplotlib>=2.0.0',
'distributed>=2.0.0'
'psutil',
'six',
'ipyfilechooser',
'ipyfilechooser>=0.0.6',
'joblib>=0.11.0',
'ipywidgets>=5.2.2',
'ipython>=5.1.0,<6;python_version<"3.3"', # IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2
'ipython>=5.1.0,<6;python_version<"3.3"', # IPython 6.0+ does not support Python < 3.5
'ipython>=6.0;python_version>="3.3"', # Beginning with IPython 6.0, Python 3.3 and above is required.
]

Expand Down
4 changes: 2 additions & 2 deletions sidpy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = '0.0.6'
time = '2021-09-19 16:08:00'
version = '0.0.7'
time = '2021-10-22 16:08:00'
14 changes: 8 additions & 6 deletions sidpy/hdf/hdf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,15 @@ def write_simple_attrs(h5_obj, attrs, force_to_str=True, verbose=False):
try:
h5_obj.attrs[key] = clean_val
except Exception as excp:
if force_to_str:
warn('Casting attribute value: {} of type: {} to str'
''.format(val, type(val)))
h5_obj.attrs[key] = str(val)
else:
raise excp('Could not write attribute value: {} of type: {}'
if verbose:
if force_to_str:
warn('Casting attribute value: {} of type: {} to str'
''.format(val, type(val)))
h5_obj.attrs[key] = str(val)
else:
raise excp('Could not write attribute value: {} of type: {}'
''.format(val, type(val)))

if verbose:
print('Wrote all (simple) attributes to {}: {}\n'
''.format(type(h5_obj), h5_obj.name.split('/')[-1]))
Expand Down
9 changes: 6 additions & 3 deletions sidpy/io/interface_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import division, print_function, absolute_import, unicode_literals
import os
import sys
import warnings
import ipyfilechooser

if sys.version_info.major == 3:
Expand All @@ -19,8 +20,9 @@

class open_file_dialog(ipyfilechooser.FileChooser):
def __init__(self, directory='.'):
super().__init__(directory)
self._use_dir_icons = True
super().__init__(directory)



def _apply_selection(self):
Expand Down Expand Up @@ -72,7 +74,7 @@ def _set_form_values(self, path: str, filename: str) -> None:
dircontent_real_names = ipyfilechooser.utils.get_dir_contents(
path,
show_hidden=self._show_hidden,
prepend_icons=False,
dir_icon_append=False,
show_only_dirs=self._show_only_dirs,
filter_pattern=self._filter_pattern
)
Expand All @@ -81,7 +83,7 @@ def _set_form_values(self, path: str, filename: str) -> None:
dircontent_display_names = ipyfilechooser.utils.get_dir_contents(
path,
show_hidden=self._show_hidden,
prepend_icons=self._use_dir_icons,
dir_icon_append=self._use_dir_icons,
show_only_dirs=self._show_only_dirs,
filter_pattern=self._filter_pattern
)
Expand Down Expand Up @@ -403,6 +405,7 @@ def progress_bar(title='Progress', start=0, stop=100):
>>> progress.setValue(count)
"""
# Check whether QT is available
warnings.warn("progress_bar() is deprecated; use tqdm package instead", warnings.DeprecationWarning)
try:
from PyQt5 import QtGui, QtWidgets, QtCore
except ImportError:
Expand Down
5 changes: 2 additions & 3 deletions tests/hdf/test_dtype_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ def setUp(self):
1j * np.random.random(size=num_elems), dtype=np.complex64)
h5_f.flush()
return

def tearDown(self):
os.remove(file_path)



class TestStackRealToComplex(unittest.TestCase):

def test_single(self):
Expand Down Expand Up @@ -592,7 +591,7 @@ class TestValidateDtype(unittest.TestCase):

def test_valid(self):
struct_dtype = np.dtype({'names': ['r', 'g', 'b'],
'formats': [np.float32, np.uint16, np.float64]})
'formats': [np.float32, np.uint16, np.float64]})
for dtype in [np.float32, np.float16, np.complex, np.complex64, np.uint8, np.int16, struct_dtype]:
self.assertTrue(dtype_utils.validate_dtype(dtype))

Expand Down
43 changes: 22 additions & 21 deletions tests/hdf/test_hdf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ class TestGetH5ObjRefs(TestHDFUtilsBase):
def test_many(self):
with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
h5_obj_refs = [h5_f,
4.123,
np.arange(6),
h5_f['/Raw_Measurement/Position_Indices'],
h5_f['/Raw_Measurement/source_main-Fitter_000'],
h5_f['/Raw_Measurement/source_main-Fitter_000/Spectroscopic_Indices'],
h5_f['/Raw_Measurement/Spectroscopic_Values']]
4.123,
np.arange(6),
h5_f['/Raw_Measurement/Position_Indices'],
h5_f['/Raw_Measurement/source_main-Fitter_000'],
h5_f['/Raw_Measurement/source_main-Fitter_000/Spectroscopic_Indices'],
h5_f['/Raw_Measurement/Spectroscopic_Values']]
chosen_objs = [h5_f['/Raw_Measurement/Position_Indices'],
h5_f['/Raw_Measurement/source_main-Fitter_000'],
h5_f['/Raw_Measurement/source_main-Fitter_000/Spectroscopic_Indices']]
h5_f['/Raw_Measurement/source_main-Fitter_000'],
h5_f['/Raw_Measurement/source_main-Fitter_000/Spectroscopic_Indices']]

target_ref_names = ['Position_Indices', 'source_main-Fitter_000', 'Spectroscopic_Indices']

Expand All @@ -250,12 +250,12 @@ def test_many(self):
def test_single(self):
with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
h5_obj_refs = [h5_f,
4.123,
np.arange(6),
h5_f['/Raw_Measurement/Position_Indices'],
h5_f['/Raw_Measurement/source_main-Fitter_000'],
h5_f['/Raw_Measurement/source_main-Fitter_000/Spectroscopic_Indices'],
h5_f['/Raw_Measurement/Spectroscopic_Values']]
4.123,
np.arange(6),
h5_f['/Raw_Measurement/Position_Indices'],
h5_f['/Raw_Measurement/source_main-Fitter_000'],
h5_f['/Raw_Measurement/source_main-Fitter_000/Spectroscopic_Indices'],
h5_f['/Raw_Measurement/Spectroscopic_Values']]
chosen_objs = [h5_f['/Raw_Measurement/Position_Indices']]

target_ref_names = ['Position_Indices']
Expand Down Expand Up @@ -324,7 +324,7 @@ def test_invalid_val_type_in_dict(self):
data_utils.delete_existing_file(file_path)
with h5py.File(file_path, mode='w') as h5_f:
h5_group = h5_f.create_group('Blah')
#with self.assertRaises(TypeError):
# with self.assertRaises(TypeError):
# hdf_utils.write_simple_attrs(h5_group, {'att_1': [{'a': 'b'}]})
os.remove(file_path)

Expand Down Expand Up @@ -405,10 +405,10 @@ def test_nested_attrs(self):
attrs = {'att_1': 'string_val',
'att_2': {'attr_3': [1, 2, 3.14, 4],
'att_4': ['s', 'tr', 'str_3']},
'att_5': {'att_6': 4},
'att_5': {'att_6': 4},
}
# with self.assertRaises(ValueError):
# hdf_utils.write_simple_attrs(h5_group, attrs)
# with self.assertRaises(ValueError):
# hdf_utils.write_simple_attrs(h5_group, attrs)

os.remove(file_path)

Expand Down Expand Up @@ -670,7 +670,7 @@ def test_file(self):
data_utils.delete_existing_file(file_path)
with h5py.File(file_path, mode='w') as h5_f:
hdf_utils.write_book_keeping_attrs(h5_f)
data_utils.verify_book_keeping_attrs (self, h5_f)
data_utils.verify_book_keeping_attrs(self, h5_f)
os.remove(file_path)

def test_group(self):
Expand All @@ -679,7 +679,7 @@ def test_group(self):
with h5py.File(file_path, mode='w') as h5_f:
h5_g = h5_f.create_group('group')
hdf_utils.write_book_keeping_attrs(h5_g)
data_utils.verify_book_keeping_attrs (self, h5_g)
data_utils.verify_book_keeping_attrs(self, h5_g)
os.remove(file_path)

def test_dset(self):
Expand All @@ -688,7 +688,7 @@ def test_dset(self):
with h5py.File(file_path, mode='w') as h5_f:
h5_dset = h5_f.create_dataset('dset', data=[1, 2, 3])
hdf_utils.write_book_keeping_attrs(h5_dset)
data_utils.verify_book_keeping_attrs (self, h5_dset)
data_utils.verify_book_keeping_attrs(self, h5_dset)
os.remove(file_path)

def test_invalid(self):
Expand Down Expand Up @@ -1136,6 +1136,7 @@ def test_existing_dset_same_name_diff_data(self):
exist_dset_diff_data_shape=False,
exist_dset_diff_data=True, exist_grp_inst_dset=False)


"""
def test_linking_main_plus_other_dsets(self):
file_path = 'check_and_link_ancillary.h5'
Expand Down
Empty file added tests/io/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions tests/io/test_interface_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 22 2021
@author: Gerd Duscher
"""

from __future__ import division, print_function, unicode_literals, absolute_import
import unittest
import sys

sys.path.append("../../sidpy/")
from sidpy.io import interface_utils


class TestInterface(unittest.TestCase):

def test_open_file_dialog(self):
file_widget = interface_utils.open_file_dialog()
print(file_widget._use_dir_icons)
self.assertTrue(file_widget.selected is None)

0 comments on commit 3b4a630

Please sign in to comment.