use of ml.comet.experiment.impl.asset.LoggedExperimentAssetImpl in project comet-java-sdk by comet-ml.
the class LogAssetsSupportTest method testLogAndGetAssets.
@Test
public void testLogAndGetAssets() {
OnlineExperimentImpl experiment = (OnlineExperimentImpl) createOnlineExperiment();
// Make sure experiment has no assets
//
assertTrue(experiment.getAllAssetList().isEmpty());
// Upload few assets and wait for completion
//
OnlineExperimentTest.OnCompleteAction onComplete = new OnlineExperimentTest.OnCompleteAction();
experiment.logAssetFileAsync(Objects.requireNonNull(TestUtils.getFile(IMAGE_FILE_NAME)), IMAGE_FILE_NAME, false, TestUtils.SOME_FULL_CONTEXT, Optional.of(onComplete));
awaitForCondition(onComplete, "image file onComplete timeout", 30);
onComplete = new OnlineExperimentTest.OnCompleteAction();
experiment.logAssetFileAsync(Objects.requireNonNull(TestUtils.getFile(SOME_TEXT_FILE_NAME)), SOME_TEXT_FILE_NAME, false, TestUtils.SOME_FULL_CONTEXT, Optional.of(onComplete));
awaitForCondition(onComplete, "text file onComplete timeout", 30);
// wait for assets become available and validate results
//
awaitForCondition(() -> experiment.getAllAssetList().size() == 2, "Assets was uploaded");
List<LoggedExperimentAsset> assets = experiment.getAllAssetList();
validateAsset(assets, IMAGE_FILE_NAME, IMAGE_FILE_SIZE, TestUtils.SOME_FULL_CONTEXT);
validateAsset(assets, SOME_TEXT_FILE_NAME, SOME_TEXT_FILE_SIZE, TestUtils.SOME_FULL_CONTEXT);
// update one of the assets and validate
//
onComplete = new OnlineExperimentTest.OnCompleteAction();
experiment.logAssetFileAsync(Objects.requireNonNull(TestUtils.getFile(ANOTHER_TEXT_FILE_NAME)), SOME_TEXT_FILE_NAME, true, TestUtils.SOME_FULL_CONTEXT, Optional.of(onComplete));
awaitForCondition(onComplete, "update text file onComplete timeout", 30);
awaitForCondition(() -> {
List<LoggedExperimentAsset> assetList = experiment.getAllAssetList();
return assetList.stream().filter(asset -> SOME_TEXT_FILE_NAME.equals(asset.getLogicalPath())).anyMatch(asset -> {
ExperimentContext context = ((LoggedExperimentAssetImpl) asset).getContext();
return ANOTHER_TEXT_FILE_SIZE == asset.getSize().orElse(0L) && Objects.equals(context.getStep(), TestUtils.SOME_FULL_CONTEXT.getStep()) && context.getContext().equals(TestUtils.SOME_FULL_CONTEXT.getContext());
});
}, "Asset was updated");
experiment.end();
}
use of ml.comet.experiment.impl.asset.LoggedExperimentAssetImpl in project comet-java-sdk by comet-ml.
the class AssetsBaseTest method validateAsset.
/**
* Validates that provided list of assets contains asset with provided logical path and this asset conforms to the
* provided parameters.
*
* @param assets the list of assets to check against.
* @param expectedAssetLogicalPath the logical path of asset in question.
* @param expectedSize the expected size of the asset.
* @param expectedMetadata the expected metadata to be associated with the asset.
* @param expectedContext the expected {@link ExperimentContext} to be associated with the asset.
*/
static void validateAsset(List<LoggedExperimentAsset> assets, String expectedAssetLogicalPath, long expectedSize, Map<String, Object> expectedMetadata, ExperimentContext expectedContext) {
LoggedExperimentAsset asset = assets.stream().filter(loggedExperimentAsset -> expectedAssetLogicalPath.equals(loggedExperimentAsset.getLogicalPath())).findFirst().orElse(null);
assertNotNull(asset, String.format("asset expected for path: %s", expectedAssetLogicalPath));
assertEquals(expectedSize, asset.getSize().orElse(0L), String.format("wrong asset size: %s", expectedAssetLogicalPath));
ExperimentContext assetContext = ((LoggedExperimentAssetImpl) asset).getContext();
assertNotNull(assetContext, String.format("asset context expected: %s", expectedAssetLogicalPath));
assertEquals(expectedContext.getStep(), assetContext.getStep(), String.format("wrong asset's context step: %s", expectedAssetLogicalPath));
if (StringUtils.isNotBlank(expectedContext.getContext())) {
assertEquals(expectedContext.getContext(), assetContext.getContext(), String.format("wrong asset's context ID: %s", expectedAssetLogicalPath));
}
if (expectedMetadata != null) {
assertEquals(expectedMetadata, asset.getMetadata(), String.format("wrong metadata: %s", expectedAssetLogicalPath));
} else {
assertEquals(0, asset.getMetadata().size(), String.format("empty metadata expected: %s", expectedAssetLogicalPath));
}
}
use of ml.comet.experiment.impl.asset.LoggedExperimentAssetImpl in project comet-java-sdk by comet-ml.
the class ExperimentAssetLink method toExperimentAsset.
/**
* Converts this into {@link LoggedExperimentAsset} exposed by public API.
*
* @param logger the logger to be used for output.
* @return the initialized {@link LoggedExperimentAsset} instance.
*/
public LoggedExperimentAsset toExperimentAsset(Logger logger) {
LoggedExperimentAssetImpl a = new LoggedExperimentAssetImpl();
a.setAssetId(this.assetId);
a.setLogicalPath(this.fileName);
a.setRemote(this.remote);
a.setFileSize(this.fileSize);
a.setContext(this.readContext());
a.setMetadata(this.parseMetadata(logger));
a.setType(this.type);
a.setCurlDownload(this.curlDownload);
if (this.remote && StringUtils.isNotBlank(this.link)) {
try {
a.setUri(new URI(this.link));
} catch (URISyntaxException ex) {
logger.error(getString(FAILED_TO_PARSE_REMOTE_ASSET_LINK, this.link), ex);
}
}
if (this.createdAt != null) {
a.setCreatedAt(Instant.ofEpochMilli(this.createdAt.getTime()));
}
return a;
}
Aggregations