def test_token_without_user_path(tmpdir): config_path = tmpdir / 'substra.json' config_path.write_text(json.dumps(configuration.DEFAULT_CONFIG), "UTF-8") client = substra.Client(config_path=config_path, profile_name='default') assert client._current_profile['token'] == '' client = substra.Client(config_path=config_path, profile_name='default', token='foo') assert client._current_profile['token'] == 'foo'
def test_client_multi_nodes_dataset(dataset_query): """Assert that the owner is gotten from the metadata in debug mode""" client = substra.Client(debug=True) dataset_query['metadata'] = {substra.DEBUG_OWNER: 'owner_1'} key = client.add_dataset(dataset_query) asset = client.get_dataset(key) assert asset.owner == 'owner_1'
def main(network): client = substra.Client() if network == 'docker': client.add_profile('default', 'substra', 'p@$swr0d44', 'http://substra-backend.owkin.xyz', '0.0') elif network == 'skaffold': client.add_profile('default', 'node-1', 'p@$swr0d44', 'http://substra-backend.node-1.com', '0.0') else: raise Exception('Unknow network') client.login() client.set_profile('default') assets = {} assets['objective'] = json.dumps(client.list_objective(), indent=4) assets['datamanager'] = json.dumps(client.list_dataset(), indent=4) assets['algo'] = json.dumps(client.list_algo(), indent=4) assets['traintuple'] = json.dumps(client.list_traintuple(), indent=4) assets['testtuple'] = json.dumps(client.list_testtuple(), indent=4) assets['computeplan'] = json.dumps(client.list_compute_plan(), indent=4) assets['compositetraintuple'] = json.dumps( client.list_composite_traintuple(), indent=4) assets['compositealgo'] = json.dumps(client.list_composite_algo(), indent=4) assets['model'] = json.dumps([ res for res in client.client.list('model') if (('traintuple' in res or 'compositeTraintuple' in res) and 'testtuple' in res) ], indent=4) with open(assets_path, 'w') as f: f.write('"""\nWARNING\n=======\n\nDO NOT MANUALLY EDIT THIS FILE!\n\n' 'It is generated using substrapp/tests/generate_assets.py\n\n' 'In order to update this file:\n' '1. start a clean instance of substra\n' '2. run populate.py\n' '3. run substrapp/tests/generate_assets.py\n"""\n\n') for k, v in assets.items(): if network == 'docker': v = v.replace('substra-backend.owkin.xyz:8000', 'testserver') v = v.replace('substra-backend.chunantes.xyz:8001', 'testserver') if network == 'skaffold': v = v.replace('substra-backend.node-1.com', 'testserver') v = v.replace('substra-backend.node-2.com', 'testserver') v = v.replace('true', 'True') v = v.replace('false', 'False') v = v.replace('null', 'None') f.write(f'{k} = {v}') f.write('\n\n')
def __init__(self, node_name, node_id, address, user, password): super().__init__() # session added/modified assets during the session lifetime self.state = _State() # node / client self.node_id = node_id self._client = substra.Client() self._client.add_profile(node_name, address, '0.0', user=user, password=password)
def test_client_multi_nodes_cp(asset_factory): """Assert that there is one CP local folder per node""" client = substra.Client(debug=True) dataset_query = asset_factory.create_dataset( metadata={substra.DEBUG_OWNER: 'owner_1'}) dataset_1_key = client.add_dataset(dataset_query) dataset_2_query = asset_factory.create_dataset( metadata={substra.DEBUG_OWNER: 'owner_2'}) dataset_2_key = client.add_dataset(dataset_2_query) data_sample = asset_factory.create_data_sample(datasets=[dataset_1_key], test_only=False) sample_1_key = client.add_data_sample(data_sample) data_sample = asset_factory.create_data_sample(datasets=[dataset_2_key], test_only=False) sample_2_key = client.add_data_sample(data_sample) algo_query = asset_factory.create_algo() algo_key = client.add_algo(algo_query) cp = asset_factory.create_compute_plan() cp.traintuples = [ substra.sdk.schemas.ComputePlanTraintupleSpec( algo_key=algo_key, data_manager_key=dataset_1_key, traintuple_id=1, train_data_sample_keys=[sample_1_key], ), substra.sdk.schemas.ComputePlanTraintupleSpec( algo_key=algo_key, data_manager_key=dataset_2_key, traintuple_id=2, train_data_sample_keys=[sample_2_key], ) ] client.add_compute_plan(cp) path_cp_1 = Path.cwd() / "local-worker" / "compute_plans" / "owner_1" path_cp_2 = Path.cwd() / "local-worker" / "compute_plans" / "owner_2" assert path_cp_1.is_dir() assert path_cp_2.is_dir()
def __init__(self, debug, node_id=None, address=None, token=None, user=None, password=None): super().__init__() self.node_id = node_id self.debug = debug self._client = substra.Client(debug=debug, url=address, insecure=False, token=token) if not token: token = self._client.login(user, password) self.token = token
def test_compute_plan_add_update(asset_factory): client = substra.Client(debug=True) compute_plan = client.add_compute_plan( substra.sdk.schemas.ComputePlanSpec( tag=None, clean_models=False, metadata=dict(), )) dataset_query = asset_factory.create_dataset() dataset_key = client.add_dataset(dataset_query) data_sample = asset_factory.create_data_sample(datasets=[dataset_key], test_only=False) data_sample_key = client.add_data_sample(data_sample) algo_query = asset_factory.create_algo() algo_key = client.add_algo(algo_query) traintuple_key = client.add_traintuple( substra.sdk.schemas.TraintupleSpec( algo_key=algo_key, data_manager_key=dataset_key, train_data_sample_keys=[data_sample_key], in_models_keys=None, compute_plan_key=compute_plan.key, rank=None, metadata=None, ) ) traintuple = substra.sdk.schemas.ComputePlanTraintupleSpec( algo_key=algo_key, data_manager_key=dataset_key, train_data_sample_keys=[data_sample_key], in_models_ids=[traintuple_key], traintuple_id=0, ) compute_plan = client.update_compute_plan( key=compute_plan.key, data={ "traintuples": [traintuple], } )
def main(): client = substra.Client(url='http://substra-backend.node-1.com', insecure=False) client.login('node-1', 'p@$swr0d44') assets = {} assets['objective'] = json.dumps(client.list_objective(), indent=4) assets['datamanager'] = json.dumps(client.list_dataset(), indent=4) assets['algo'] = json.dumps(client.list_algo(), indent=4) assets['traintuple'] = json.dumps(client.list_traintuple(), indent=4) assets['testtuple'] = json.dumps(client.list_testtuple(), indent=4) assets['computeplan'] = json.dumps(client.list_compute_plan(), indent=4) assets['compositetraintuple'] = json.dumps( client.list_composite_traintuple(), indent=4) assets['compositealgo'] = json.dumps(client.list_composite_algo(), indent=4) models = client._backend.list(substra.sdk.schemas.Type.Model) models = [(model, list(model.keys()).pop()) for model in models] assets['model'] = json.dumps([ client._backend.get(substra.sdk.schemas.Type.Model, model[mtype]['key']) for model, mtype in models ], indent=4) with open(assets_path, 'w') as f: f.write('"""\nWARNING\n=======\n\nDO NOT MANUALLY EDIT THIS FILE!\n\n' 'It is generated using substrapp/tests/generate_assets.py\n\n' 'In order to update this file:\n' '1. start a clean instance of substra\n' '2. run computation on it (with e2e tests for instance)\n' '3. run substrapp/tests/generate_assets.py\n"""\n\n') for k, v in assets.items(): v = v.replace('substra-backend.node-1.com', 'testserver') v = v.replace('substra-backend.node-2.com', 'testserver') v = v.replace('true', 'True') v = v.replace('false', 'False') v = v.replace('null', 'None') f.write(f'{k} = {v}') f.write('\n\n')
# See the License for the specific language governing permissions and # limitations under the License. import json import os import zipfile import substra current_directory = os.path.dirname(__file__) assets_directory = os.path.join(current_directory, '../assets') with open(os.path.join(current_directory, '../../config.json'), 'r') as f: config = json.load(f) client = substra.Client() client.add_profile(config['profile_name'], config['username'], config['password'], config['url']) client.login() ALGO = { 'name': 'Constant death predictor', 'description': os.path.join(assets_directory, 'algo_constant/description.md') } ALGO_DOCKERFILE_FILES = [ os.path.join(assets_directory, 'algo_constant/algo.py'), os.path.join(assets_directory, 'algo_constant/Dockerfile'), ] ######################################################## # Build archive ########################################################
pg = tqdm(total=length) progress_handler = logging.StreamHandler(SimpleNamespace(write=lambda x: pg.write(x, end=''))) substra_logger.removeHandler(default_stream_handler) substra_logger.addHandler(progress_handler) try: yield pg finally: pg.close() substra_logger.removeHandler(progress_handler) substra_logger.addHandler(default_stream_handler) current_directory = os.path.dirname(__file__) assets_directory = os.path.join(current_directory, '../assets') client = substra.Client(profile_name="node-1") DATASET = { 'name': 'deepfake-detection', 'type': 'mp4,npy', 'data_opener': os.path.join(assets_directory, 'dataset/opener.py'), 'description': os.path.join(assets_directory, 'dataset/description.md'), 'permissions': { 'public': False, 'authorized_ids': [] }, } TEST_DATA_SAMPLES_PATHS = [ os.path.join(assets_directory, 'test_data_samples', path) for path in os.listdir(os.path.join(assets_directory, 'test_data_samples'))
def test_client_tmp_dir(): """Test the creation of a temp directory for the debug client""" client = substra.Client(debug=True) assert client.temp_directory
def test_login_without_profile(tmpdir): client = substra.Client() with pytest.raises(substra.exceptions.SDKException): client.login('foo', 'bar')
def client(tmpdir): c = substra.Client(url="http://foo.io") return c
def test_login_remote_without_url(tmpdir): with pytest.raises(substra.exceptions.SDKException): substra.Client()
def client(tmpdir): config_path = tmpdir / "substra.cfg" c = substra.Client(config_path=str(config_path)) c.add_profile('test', 'foo', 'password', url="http://foo.io") return c
substra_logger.addHandler(progress_handler) try: yield pg finally: pg.close() substra_logger.removeHandler(progress_handler) substra_logger.addHandler(default_stream_handler) # Define the current and asset directories current_directory = pathlib.Path(__file__).resolve().parents[1] assets_directory = current_directory.parent / "titanic" / "assets" algo_directory = current_directory.parent / "compute_plan" / "assets" / "algo_sgd" # Define the client client = substra.Client(debug=True) DATASET = { "name": "Titanic", "type": "csv", "data_opener": str(assets_directory / "dataset" / "opener.py"), "description": str(assets_directory / "dataset" / "description.md"), "permissions": {"public": False, "authorized_ids": []}, } TEST_DATA_SAMPLES_PATHS = [ assets_directory / "test_data_samples" / path for path in (assets_directory / "test_data_samples").glob("*") ] TRAIN_DATA_SAMPLES_PATHS = [