def test_assign(self): a_text = "Some text stream" a_binary = b"Some binary stream" value_and_operation_factory = [ ( FileVal("other/../other/file.txt"), lambda _: UploadFile(_, "txt", os.getcwd() + "/other/file.txt"), ), ( FileVal.from_stream(StringIO(a_text)), lambda _: UploadFileContent( _, "txt", base64_encode(a_text.encode("utf-8"))), ), ( FileVal.from_stream(BytesIO(a_binary)), lambda _: UploadFileContent(_, "bin", base64_encode(a_binary)), ), ] for value, operation_factory in value_and_operation_factory: processor = MagicMock() exp, path, wait = ( self._create_run(processor), self._random_path(), self._random_wait(), ) var = File(exp, path) var.assign(value, wait=wait) processor.enqueue_operation.assert_called_once_with( operation_factory(path), wait)
def test_assign_content(self): # given wait = self._random_wait() path = self._random_path() op_processor = MagicMock() exp = self._create_run(processor=op_processor) attr = FileSeries(exp, path) file = File.as_image(numpy.random.rand(10, 10) * 255) # when attr.assign([file], wait=wait) # then op_processor.enqueue_operation.assert_has_calls([ call(ClearImageLog(path), False), call( LogImages( path, [ LogImages.ValueType( ImageValue(base64_encode(file.content), None, None), None, self._now(), ) ], ), wait, ), ])
def test_log_content(self): # given wait = self._random_wait() path = self._random_path() op_processor = MagicMock() exp = self._create_run(processor=op_processor) attr = FileSeries(exp, path) file = File.as_image(numpy.random.rand(10, 10) * 255) # when attr.log( file, step=3, timestamp=self._now(), wait=wait, name="nazwa", description="opis", ) # then op_processor.enqueue_operation.assert_called_once_with( LogImages( path, [ LogImages.ValueType( ImageValue(base64_encode(file.content), "nazwa", "opis"), 3, self._now(), ) ], ), wait, )
def log_artifact(self, experiment, artifact, destination=None): if isinstance(artifact, str): if os.path.isfile(artifact): target_name = (os.path.basename(artifact) if destination is None else destination) dest_path = self._get_dest_and_ext(target_name) operation = alpha_operation.UploadFile( path=dest_path, ext="", file_path=os.path.abspath(artifact), ) elif os.path.isdir(artifact): for path, file_destination in self._log_dir_artifacts( artifact, destination): self.log_artifact(experiment, path, file_destination) return else: raise FileNotFound(artifact) elif hasattr(artifact, "read"): if not destination: raise ValueError("destination is required for IO streams") dest_path = self._get_dest_and_ext(destination) data = artifact.read() content = data.encode("utf-8") if isinstance(data, str) else data operation = alpha_operation.UploadFileContent( path=dest_path, ext="", file_content=base64_encode(content)) else: raise ValueError("Artifact must be a local path or an IO object") self._execute_upload_operations_with_400_retry(experiment, operation)
def _get_base64_image_content(file: File) -> str: if file.path is not None: if not os.path.exists(file.path): raise FileNotFound(file.path) with open(file.path, "rb") as image_file: file = File.from_stream(image_file) ext = imghdr.what("", h=file.content) if not ext: raise OperationNotSupported( "FileSeries supports only image files for now. " "Other file types will be implemented in future." ) return base64_encode(file.content)
def assign(self, value: FileVal, wait: bool = False) -> None: verify_type("value", value, FileVal) if value.path is not None: operation = UploadFile(self._path, ext=value.extension, file_path=os.path.abspath(value.path)) elif value.content is not None: operation = UploadFileContent( self._path, ext=value.extension, file_content=base64_encode(value.content), ) else: raise ValueError("File path and content are None") with self._container.lock(): self._enqueue_operation(operation, wait)
def test_log_path(self): # given wait = self._random_wait() path = self._random_path() op_processor = MagicMock() exp = self._create_run(processor=op_processor) attr = FileSeries(exp, path) file = File.as_image(numpy.random.rand(10, 10) * 255) with create_file(file.content, binary_mode=True) as tmp_filename: saved_file = File(tmp_filename) # when attr.log( saved_file, step=3, timestamp=self._now(), wait=wait, description="something", ) # then op_processor.enqueue_operation.assert_called_once_with( LogImages( path, [ LogImages.ValueType( ImageValue(base64_encode(file.content), None, "something"), 3, self._now(), ) ], ), wait, )
def test_execute_operations(self, upload_mock, swagger_client_factory): # given swagger_client = self._get_swagger_client_mock(swagger_client_factory) backend = HostedNeptuneBackend(credentials) container_uuid = str(uuid.uuid4()) response_error = MagicMock() response_error.errorDescription = "error1" swagger_client.api.executeOperations().response().result = [response_error] swagger_client.api.executeOperations.reset_mock() upload_mock.return_value = [FileUploadError("file1", "error2")] some_text = "Some streamed text" some_binary = b"Some streamed binary" for container_type in self.container_types: with self.subTest(msg=f"For type {container_type.value}"): upload_mock.reset_mock() swagger_client_factory.reset_mock() # when result = backend.execute_operations( container_id=container_uuid, container_type=container_type, operations=[ UploadFile( path=["some", "files", "some_file"], ext="", file_path="path_to_file", ), UploadFileContent( path=["some", "files", "some_text_stream"], ext="txt", file_content=base64_encode(some_text.encode("utf-8")), ), UploadFileContent( path=["some", "files", "some_binary_stream"], ext="bin", file_content=base64_encode(some_binary), ), LogFloats(["images", "img1"], [LogFloats.ValueType(1, 2, 3)]), AssignString(["properties", "name"], "some text"), UploadFile( path=["some", "other", "file.txt"], ext="txt", file_path="other/file/path.txt", ), ], ) # then swagger_client.api.executeOperations.assert_called_once_with( **{ "experimentId": str(container_uuid), "operations": [ { "path": "images/img1", "logFloats": { "entries": [ { "value": 1, "step": 2, "timestampMilliseconds": 3000, } ] }, }, { "path": "properties/name", "assignString": {"value": "some text"}, }, ], **DEFAULT_REQUEST_KWARGS, } ) upload_mock.assert_has_calls( [ call( swagger_client=backend.leaderboard_client, container_id=container_uuid, attribute="some/other/file.txt", source="other/file/path.txt", ext="txt", multipart_config=backend._client_config.multipart_config, ), call( swagger_client=backend.leaderboard_client, container_id=container_uuid, attribute="some/files/some_file", source="path_to_file", ext="", multipart_config=backend._client_config.multipart_config, ), call( swagger_client=backend.leaderboard_client, container_id=container_uuid, attribute="some/files/some_text_stream", source=some_text.encode("utf-8"), ext="txt", multipart_config=backend._client_config.multipart_config, ), call( swagger_client=backend.leaderboard_client, container_id=container_uuid, attribute="some/files/some_binary_stream", source=some_binary, ext="bin", multipart_config=backend._client_config.multipart_config, ), ], any_order=True, ) self.assertEqual( ( 6, [ FileUploadError("file1", "error2"), FileUploadError("file1", "error2"), FileUploadError("file1", "error2"), FileUploadError("file1", "error2"), MetadataInconsistency("error1"), ], ), result, )