def test_static(): # Make an example static method model = PythonStaticMethodModel.create_model('numpy', 'max', autobatch=False, function_kwargs={'axis': 0}) model.set_title('Example function') model.set_name('function') model.set_inputs('ndarray', 'Matrix', shape=[None, None]) model.set_outputs('ndarray', 'Max of a certain axis', shape=[None]) # Make the servable servable = PythonStaticMethodServable(**model.to_dict()) # Test it out, _ = servable.run([[1, 2], [3, 4]]) assert np.isclose([3, 4], out).all() # Test giving it parameters assert np.isclose([2, 4], servable.run([[1, 2], [3, 4]], parameters=dict(axis=1))[0]).all() # Test the autobatch model.servable.methods['run'].method_details['autobatch'] = True servable = PythonStaticMethodServable(**model.to_dict()) out, _ = servable.run([[1, 2], [3, 4]]) assert np.isclose([2, 4], out).all()
def test_visibility(): model = PythonStaticMethodModel.create_model('numpy.linalg', 'norm') model.set_name('1d_norm') model.set_title('Norm of a 1D Array') model.set_inputs('ndarray', 'Array to be normed', shape=[None]) model.set_outputs('number', 'Norm of the array') model.set_visibility(users=['bec215bc-9169-4be9-af49-4872b5e11ef8' ]) # Setting visibility to a user validate_against_dlhub_schema(model.to_dict(), 'servable') assert model.dlhub.visible_to[0].startswith('urn:globus:auth:identity:') model.set_visibility(groups=['fdb38a24-03c1-11e3-86f7-12313809f035' ]) # Setting visibility to a group validate_against_dlhub_schema(model.to_dict(), 'servable') assert len( model.dlhub.visible_to) == 1 # Ensure was replaced, not appended assert model.dlhub.visible_to[0].startswith('urn:globus:groups:id:') model.set_visibility(users=['foo']) # Test using a non-UUID for user with raises(ValidationError): validate_against_dlhub_schema(model.to_dict(), 'servable') model.set_visibility() # Default visibility is "public" validate_against_dlhub_schema(model.to_dict(), 'servable') assert model.dlhub.visible_to == ['public']
def test_submit(self): # Make an example function model = PythonStaticMethodModel.create_model('numpy.linalg', 'norm') model.set_name('1d_norm') model.set_title('Norm of a 1D Array') model.set_inputs('ndarray', 'Array to be normed', shape=[None]) model.set_outputs('number', 'Norm of the array') # Submit the model self.dl.publish_servable(model)
def test_loader(self): # Make an example static method model = PythonStaticMethodModel.create_model( 'numpy', 'max', autobatch=False, function_kwargs={'axis': 0}) model.set_title('Example function') model.set_name('function') model.set_inputs('ndarray', 'Matrix', shape=[None, None]) model.set_outputs('ndarray', 'Max of a certain axis', shape=[None]) # Test the loader servable = create_servable(model.to_dict()) self.assertIsInstance(servable, PythonStaticMethodServable) self.assertEqual([0], servable.run([0, -1])[0])
def test_pipeline(self): """Make a pipeline composed of two numpy steps""" # Generate the two steps step1 = PythonStaticMethodModel.create_model('numpy', 'max', function_kwargs={'axis': 1})\ .set_name('step1') step2 = PythonStaticMethodModel.create_model('numpy', 'mean').set_name('step2') # Make the pipeline pipeline = PipelineModel().set_title( 'Average of Column Maximums').set_name('numpy_test') pipeline.add_step('username', step1.name, 'Maximum of each column', {'axis': 0}) pipeline.add_step('username', step2.name, 'Average of the maximums') # Generate the pipeline metadata metadata = pipeline.to_dict() correct_metadata = { 'datacite': { 'creators': [], 'titles': [{ 'title': 'Average of Column Maximums' }], 'publisher': 'DLHub', 'publicationYear': _year, 'identifier': { 'identifier': '10.YET/UNASSIGNED', 'identifierType': 'DOI' }, 'resourceType': { 'resourceTypeGeneral': 'InteractiveResource' }, "descriptions": [], "fundingReferences": [], "relatedIdentifiers": [], "alternateIdentifiers": [], "rightsList": [] }, 'dlhub': { 'version': __version__, 'domains': [], 'visible_to': ['public'], 'name': 'numpy_test', 'type': 'pipeline', 'files': {} }, 'pipeline': { 'steps': [{ 'author': 'username', 'name': step1.name, 'description': 'Maximum of each column', 'parameters': { 'axis': 0 } }, { 'author': 'username', 'name': step2.name, 'description': 'Average of the maximums' }] } } self.assertEqual(metadata, correct_metadata) validate_against_dlhub_schema(metadata, 'pipeline')
from dlhub_sdk.models.servables.python import PythonStaticMethodModel from dlhub_sdk.utils.schemas import validate_against_dlhub_schema import json import os # Create a model that invokes the "run" function from the model = PythonStaticMethodModel.create_model('application', 'run') # Describe the inputs and outputs model.set_inputs('list', 'Paths to all images in a dataset', item_type='string') model.set_outputs('ndarray', 'Accumulated result of decoding all the images', shape=[208, 208]) # Add provenance information model.set_title("Deep-Learning Super-resolution Image Reconstruction (DSIR)") model.set_name('dsir') model.set_authors(['Duarte, Alex'], ['The Institute of Photonic Sciences']) model.add_alternate_identifier( "https://github.com/leaxp/Deep-Learning-Super-Resolution-Image-Reconstruction-DSIR", 'URL') # Add requirements model.add_requirement('torch', 'detect') model.add_requirement('torchvision', 'detect') # Add app.py, which holds the noop function, to the list of associated files to be submitted model.add_file("app.py")
from dlhub_sdk.models.servables.python import PythonStaticMethodModel from dlhub_sdk.utils.schemas import validate_against_dlhub_schema from dlhub_sdk.utils.types import compose_argument_block import json import os # Create a model that invokes the "run" function from the model = PythonStaticMethodModel.create_model('app', 'run', function_kwargs={'relax': False}) # Describe the inputs and outputs model.set_inputs('string', 'Molecule in XYZ format') model.set_outputs( 'dict', 'Forces and energies of the molecule', properties={ 'energy': compose_argument_block('number', 'Energy of the whole system'), 'forces': compose_argument_block('ndarray', 'Forces acting on each atom in each direction', shape=[None, 3]) }) # Add provenance information model.set_title("SchNet C20 Force and Energy Predictor") model.set_name('schnet_c20') model.set_domains(['physics']) model.set_abstract( "A model based on the SchNet architecture that predicts the energy and forces of a C20 molecule. Useful for molecular dynmaics simulations."