Skip to content
Snippets Groups Projects
Commit b33a5047 authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Implemented list/get/add/delete accessors

parent eb1f2d68
No related branches found
No related tags found
No related merge requests found
......@@ -157,8 +157,7 @@ class GenericImageDataAccessor(ABC):
def info(self):
return {
'shape_dict': self.shape_dict,
'dtype': self.dtype,
'loaded': True,
'dtype': str(self.dtype),
}
class InMemoryDataAccessor(GenericImageDataAccessor):
......@@ -182,7 +181,7 @@ class GenericImageFileAccessor(GenericImageDataAccessor): # image data is loaded
@property
def info(self):
d = super().info()
d = super().info
d['filepath'] = self.fpath.__str__()
return d
......
......@@ -62,8 +62,11 @@ def list_active_models():
@app.get('/accessors')
def list_accessors():
# TODO: can't return accessor objects, so instead return info
return list(session.accessors)
return session.list_accessors()
@app.get('/accessors/{accessor_id}')
def get_accessor(accessor_id: str):
return session.get_accessor_info(accessor_id)
@app.put('/accessors/read_from_file')
def read_accessor_from_file(filename: str, accessor_id: Union[str, None] = None):
......@@ -73,7 +76,7 @@ def read_accessor_from_file(filename: str, accessor_id: Union[str, None] = None)
acc = generate_file_accessor(fp)
return session.add_accessor(acc, accessor_id=accessor_id)
@app.put('/accessors/delete')
@app.get('/accessors/delete/{accessor_id}')
def delete_accessor(accessor_id: str):
return session.del_accessor(accessor_id)
......@@ -94,12 +94,12 @@ class _Session(object):
if accessor_id is None:
idx = len(self.accessors)
accessor_id = f'auto_{idx:06d}'
self.accessors[accessor_id] = acc
self.accessors[accessor_id] = {'loaded': True, 'object': acc, **acc.info}
return accessor_id
def del_accessor(self, accessor_id: str):
"""
Replace accessor object with its info dictionary
Remove accessor object but retain its info dictionary
:param accessor_id: accessor's ID
:return: ID of accessor
"""
......@@ -108,14 +108,25 @@ class _Session(object):
v = self.accessors[accessor_id]
if isinstance(v, dict) and v['loaded'] is False:
logger.warning(f'Accessor {accessor_id} is already deleted')
info = v
else:
assert isinstance(v, GenericImageDataAccessor)
info = v.info
info['loaded'] = False
self.accessors[accessor_id] = info
assert isinstance(v['object'], GenericImageDataAccessor)
v['loaded'] = False
v['object'] = None
return accessor_id
def list_accessors(self) -> dict:
"""
List information about all accessors in JSON-readable format
:return:
"""
return pd.DataFrame(self.accessors).drop('object').to_dict()
def get_accessor_info(self, acc_id: str):
"""
Get information about a single accessor
"""
return self.list_accessors()[acc_id]
@staticmethod
def make_paths(root: str = None) -> dict:
"""
......
......@@ -184,6 +184,8 @@ class TestApiFromAutomatedClient(TestServerTestCase):
def test_add_and_delete_accessor(self):
self.copy_input_file_to_server()
# add accessor to session
resp_add_acc = self._put(
f'accessors/read_from_file',
query={
......@@ -193,15 +195,15 @@ class TestApiFromAutomatedClient(TestServerTestCase):
acc_id = resp_add_acc.json()
self.assertTrue(acc_id.startswith('auto_'))
# confirm that accessor is listed in session context
resp_list_acc = self._get(
f'accessors',
)
self.assertEqual(len(resp_list_acc.json()), 1)
self.assertTrue(resp_list_acc.json()[0].startswith('auto_'))
self.assertTrue(list(resp_list_acc.json().keys())[0].startswith('auto_'))
self.assertTrue(resp_list_acc.json()[acc_id]['loaded'])
resp_del_acc = self._put(
f'accessors/delete',
query={
'accessor_id': czifile['name'],
},
)
\ No newline at end of file
# delete and check that its 'loaded' state changes
self.assertTrue(self._get(f'accessors/{acc_id}').json()['loaded'])
self.assertEqual(self._get(f'accessors/delete/{acc_id}').json(), acc_id)
self.assertFalse(self._get(f'accessors/{acc_id}').json()['loaded'])
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment