Search in sources :

Example 1 with LoggedExperimentAssetImpl

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();
}
Also used : LoggedExperimentAsset(ml.comet.experiment.asset.LoggedExperimentAsset) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CsvSource(org.junit.jupiter.params.provider.CsvSource) Optional.empty(java.util.Optional.empty) LoggedExperimentAsset(ml.comet.experiment.asset.LoggedExperimentAsset) ExperimentContext(ml.comet.experiment.context.ExperimentContext) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) TestUtils.awaitForCondition(ml.comet.experiment.impl.TestUtils.awaitForCondition) Map(java.util.Map) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Tag(org.junit.jupiter.api.Tag) URI(java.net.URI) LoggedExperimentAssetImpl(ml.comet.experiment.impl.asset.LoggedExperimentAssetImpl) ExperimentTestFactory.createOnlineExperiment(ml.comet.experiment.impl.ExperimentTestFactory.createOnlineExperiment) OnlineExperiment(ml.comet.experiment.OnlineExperiment) ExperimentContext(ml.comet.experiment.context.ExperimentContext) LoggedExperimentAssetImpl(ml.comet.experiment.impl.asset.LoggedExperimentAssetImpl) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with LoggedExperimentAssetImpl

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));
    }
}
Also used : LoggedExperimentAsset(ml.comet.experiment.asset.LoggedExperimentAsset) ExperimentContext(ml.comet.experiment.context.ExperimentContext) LoggedExperimentAssetImpl(ml.comet.experiment.impl.asset.LoggedExperimentAssetImpl)

Example 3 with LoggedExperimentAssetImpl

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;
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) LoggedExperimentAssetImpl(ml.comet.experiment.impl.asset.LoggedExperimentAssetImpl)

Aggregations

LoggedExperimentAssetImpl (ml.comet.experiment.impl.asset.LoggedExperimentAssetImpl)3 URI (java.net.URI)2 LoggedExperimentAsset (ml.comet.experiment.asset.LoggedExperimentAsset)2 ExperimentContext (ml.comet.experiment.context.ExperimentContext)2 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 OnlineExperiment (ml.comet.experiment.OnlineExperiment)1 ExperimentTestFactory.createOnlineExperiment (ml.comet.experiment.impl.ExperimentTestFactory.createOnlineExperiment)1 TestUtils.awaitForCondition (ml.comet.experiment.impl.TestUtils.awaitForCondition)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 Assertions.fail (org.junit.jupiter.api.Assertions.fail)1 DisplayName (org.junit.jupiter.api.DisplayName)1 Tag (org.junit.jupiter.api.Tag)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 CsvSource (org.junit.jupiter.params.provider.CsvSource)1