Search in sources :

Example 1 with ExperimentContext

use of ml.comet.experiment.context.ExperimentContext in project comet-java-sdk by comet-ml.

the class ExperimentAssetLink method readContext.

private ExperimentContext readContext() {
    long ctxStep = 0;
    String ctxStr = "";
    if (this.step != null) {
        ctxStep = this.step;
    }
    if (StringUtils.isNotBlank(this.runContext)) {
        ctxStr = this.runContext;
    }
    return new ExperimentContext(ctxStep, 0, ctxStr);
}
Also used : ExperimentContext(ml.comet.experiment.context.ExperimentContext) LogMessages.getString(ml.comet.experiment.impl.resources.LogMessages.getString)

Example 2 with ExperimentContext

use of ml.comet.experiment.context.ExperimentContext in project comet-java-sdk by comet-ml.

the class LogAssetsSupportTest method testLogAndGetAssetsFolder.

@ParameterizedTest(name = "[{index}] flat: {0}, recursive: {1}")
@CsvSource({ "false, true", "true, true", "true, false", "false, false" })
public void testLogAndGetAssetsFolder(boolean flatDirectoryStructure, boolean recursive) {
    try (OnlineExperimentImpl experiment = (OnlineExperimentImpl) createOnlineExperiment()) {
        // Make sure experiment has no assets
        // 
        assertTrue(experiment.getAllAssetList().isEmpty());
        // Log assets folder and wait for completion
        // 
        ExperimentContext context = TestUtils.SOME_FULL_CONTEXT;
        OnlineExperimentTest.OnCompleteAction onComplete = new OnlineExperimentTest.OnCompleteAction();
        experiment.logAssetFolder(assetsFolder.toFile(), !flatDirectoryStructure, recursive, !flatDirectoryStructure, context, Optional.of(onComplete));
        awaitForCondition(onComplete, "log assets' folder timeout", 60);
        // wait for assets become available and validate results
        // 
        int expectedSize = recursive ? assetFolderFiles.size() : assetFolderFiles.size() - assetSubFolderFiles.size();
        awaitForCondition(() -> experiment.getAllAssetList().size() == expectedSize, "Assets was uploaded");
        List<LoggedExperimentAsset> assets = experiment.getAllAssetList();
        validateAllAssetsFromFolder(assets, context, null, flatDirectoryStructure, recursive);
    } catch (Exception e) {
        fail(e);
    }
}
Also used : LoggedExperimentAsset(ml.comet.experiment.asset.LoggedExperimentAsset) ExperimentContext(ml.comet.experiment.context.ExperimentContext) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ExperimentContext

use of ml.comet.experiment.context.ExperimentContext 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 4 with ExperimentContext

use of ml.comet.experiment.context.ExperimentContext in project comet-java-sdk by comet-ml.

the class BaseExperimentAsync method logAssetFolder.

/**
 * Asynchronous version that only logs any received exceptions or failures.
 *
 * @param folder               the folder you want to log.
 * @param logFilePath          if {@code true}, log the file path with each file.
 * @param recursive            if {@code true}, recurse the folder.
 * @param prefixWithFolderName if {@code true} then path of each asset file will be prefixed with folder name
 *                             in case if {@code logFilePath} is {@code true}.
 * @param assetType            optional type of the asset (default: ASSET).
 * @param groupingName         optional name of group the assets should belong.
 * @param metadata             the optional metadata to associate.
 * @param context              the context to be associated with logged assets.
 * @param onCompleteAction     The optional action to be invoked when this operation
 *                             asynchronously completes. Can be empty if not interested in completion signal.
 */
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
void logAssetFolder(@NonNull File folder, boolean logFilePath, boolean recursive, boolean prefixWithFolderName, @NonNull Optional<String> assetType, @NonNull Optional<String> groupingName, @NonNull Optional<Map<String, Object>> metadata, @NonNull ExperimentContext context, @NonNull Optional<Action> onCompleteAction) {
    if (!folder.isDirectory()) {
        getLogger().warn(getString(LOG_ASSET_FOLDER_EMPTY, folder));
        return;
    }
    this.updateContext(context);
    // make deep copy of the current experiment context to avoid side effects
    // if base experiment context become updated while operation is still in progress
    ExperimentContext assetContext = new ExperimentContext(this.baseContext);
    AtomicInteger successfullyLoggedCount = new AtomicInteger();
    try {
        Stream<AssetImpl> assets = AssetUtils.walkFolderAssets(folder, logFilePath, recursive, prefixWithFolderName, metadata, assetType, groupingName).peek(asset -> asset.setContext(assetContext));
        // create parallel execution flow with errors delaying
        // allowing processing of items even if some of them failed
        Observable<RestApiResponse> responseObservable = Observable.fromStream(assets).flatMap(asset -> Observable.fromSingle(this.sendAssetAsync(getRestApiClient()::logAsset, asset).doOnSuccess(apiResponse -> {
            if (!apiResponse.hasFailed()) {
                successfullyLoggedCount.incrementAndGet();
            }
        })), true);
        if (onCompleteAction.isPresent()) {
            responseObservable = responseObservable.doFinally(onCompleteAction.get());
        }
        // subscribe for processing results
        // noinspection ResultOfMethodCallIgnored
        responseObservable.ignoreElements().subscribe(() -> getLogger().info(getString(ASSETS_FOLDER_UPLOAD_COMPLETED, folder, successfullyLoggedCount.get())), throwable -> getLogger().error(getString(FAILED_TO_LOG_SOME_ASSET_FROM_FOLDER, folder), throwable));
    } catch (Throwable t) {
        getLogger().error(getString(FAILED_TO_LOG_ASSET_FOLDER, folder), t);
    }
}
Also used : AssetImpl(ml.comet.experiment.impl.asset.AssetImpl) RemoteAssetImpl(ml.comet.experiment.impl.asset.RemoteAssetImpl) ArtifactAssetImpl(ml.comet.experiment.impl.asset.ArtifactAssetImpl) ExperimentContext(ml.comet.experiment.context.ExperimentContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RestApiResponse(ml.comet.experiment.impl.rest.RestApiResponse)

Example 5 with ExperimentContext

use of ml.comet.experiment.context.ExperimentContext in project comet-java-sdk by comet-ml.

the class LogModelExample method run.

private static void run(OnlineExperiment experiment) throws Exception {
    experiment.setExperimentName("LogModel Examples");
    // log model file
    // 
    ExperimentContext context = new ExperimentContext(0, 0, "example");
    Map<String, Object> metadata = BaseExample.createMetaData();
    File file = getResourceFile(CHART_IMAGE_FILE);
    experiment.logModel(SOME_MODEL_NAME, file, AMAZING_CHART_NAME, true, metadata, context);
    System.out.printf("Logging file '%s' of the model '%s' with logical path '%s'\n\n", file, SOME_MODEL_NAME, AMAZING_CHART_NAME);
    // log model data
    // 
    byte[] data = SOME_MODEL_DATA.getBytes(StandardCharsets.UTF_8);
    experiment.logModel(SOME_MODEL_NAME, data, SOME_MODEL_LOGICAL_PATH, true, metadata, context);
    System.out.printf("Logging data of the model '%s' with logical path '%s'\n\n", SOME_MODEL_NAME, SOME_MODEL_LOGICAL_PATH);
    // log model folder
    // 
    Path assetDir = BaseExample.copyResourcesToTmpDir();
    experiment.logModelFolder(SOME_MODEL_NAME, assetDir.toFile(), true, metadata, context);
    System.out.printf("Logging data folder '%s' of the model '%s''\n\n", assetDir, SOME_MODEL_NAME);
    // read logged assets of the model
    // 
    List<LoggedExperimentAsset> assets = new ArrayList<>();
    // wait until all assets upload completes asynchronously
    Awaitility.await("failed to wait for model's assets to be uploaded").atMost(60, TimeUnit.SECONDS).pollInterval(1, TimeUnit.SECONDS).until(() -> {
        List<LoggedExperimentAsset> loggedAsset = experiment.getAssetList(MODEL_ELEMENT.type());
        if (loggedAsset.size() > 0) {
            assets.addAll(loggedAsset);
            return true;
        } else {
            return false;
        }
    });
    System.out.printf("Retrieved %d logged assets of the model '%s':\n", assets.size(), SOME_MODEL_NAME);
    assets.forEach(loggedExperimentAsset -> System.out.printf("\t%s\n", loggedExperimentAsset));
    System.out.println("===== Experiment completed ====");
}
Also used : Path(java.nio.file.Path) LoggedExperimentAsset(ml.comet.experiment.asset.LoggedExperimentAsset) ExperimentContext(ml.comet.experiment.context.ExperimentContext) ArrayList(java.util.ArrayList) Utils.getResourceFile(ml.comet.examples.Utils.getResourceFile) File(java.io.File)

Aggregations

ExperimentContext (ml.comet.experiment.context.ExperimentContext)9 LoggedExperimentAsset (ml.comet.experiment.asset.LoggedExperimentAsset)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 CsvSource (org.junit.jupiter.params.provider.CsvSource)3 Path (java.nio.file.Path)2 OnlineExperiment (ml.comet.experiment.OnlineExperiment)2 ExperimentTestFactory.createOnlineExperiment (ml.comet.experiment.impl.ExperimentTestFactory.createOnlineExperiment)2 LoggedExperimentAssetImpl (ml.comet.experiment.impl.asset.LoggedExperimentAssetImpl)2 File (java.io.File)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)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 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Utils.getResourceFile (ml.comet.examples.Utils.getResourceFile)1 CometApi (ml.comet.experiment.CometApi)1