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);
}
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);
}
}
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();
}
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);
}
}
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 ====");
}
Aggregations