Skip to content

Commit fbb4c3a

Browse files
authored
Merge pull request #40 from comet-ml/CM-1630-logging-remote-asset
[CM-1630] Add support for logging remote assets
2 parents 0c5cc34 + 0c6356d commit fbb4c3a

File tree

23 files changed

+907
-324
lines changed

23 files changed

+907
-324
lines changed

README.md

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
|--------|--------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------|----------------------------------------------------------------------------|
66
| master | [![CI](https://github.com/comet-ml/comet-java-sdk/actions/workflows/ci-maven.yml/badge.svg)](https://github.com/comet-ml/comet-java-sdk/workflows/ci-maven.yml) | [![codecov](https://codecov.io/gh/comet-ml/comet-java-sdk/branch/master/graph/badge.svg)](https://codecov.io/gh/comet-ml/comet-java-sdk) | [![Lint Code Base](https://github.com/comet-ml/comet-java-sdk/actions/workflows/super-linter.yml/badge.svg)](https://github.com/comet-ml/comet-java-sdk/actions/workflows/super-linter.yml) | [![CodeQL](https://github.com/comet-ml/comet-java-sdk/actions/workflows/codeQL.yml/badge.svg)](https://github.com/comet-ml/comet-java-sdk/actions/workflows/codeQL.yml) |
77

8-
### Using Comet Java SDK:
9-
#### Add dependency to pom.xml:
8+
## Using Comet Java SDK
9+
### Add dependency to the pom.xml
1010
```
1111
<dependencies>
1212
<dependency>
@@ -16,44 +16,70 @@
1616
</dependency>
1717
</dependencies>
1818
```
19-
#### Create experiment and log metrics and parameters:
19+
### Create experiment and log metrics and parameters
2020
```java
2121
OnlineExperiment experiment = ExperimentBuilder.OnlineExperiment()
2222
.withApiKey("someApiKey")
2323
.withProjectName("someProject")
2424
.withWorkspace("someWorkspace")
2525
.build();
26-
experiment.setExperimentName("My experiment");
27-
experiment.logParameter("batch_size", "500");
28-
experiment.logMetric("strMetric", 123);
29-
experiment.end();
26+
experiment.setExperimentName("My experiment");
27+
experiment.logParameter("batch_size", "500");
28+
experiment.logMetric("strMetric", 123);
29+
experiment.end();
3030
```
31-
32-
#### Configure you experiment object:
31+
The ```OnlineExperiment``` also can be used with [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) statement which automatically
32+
handles call to the ```experiment.end()```.
3333
```java
34-
# Configuration hierarchy:
35-
Environment Variable > Configuration File Override > Default config file (application.conf)
34+
try (OnlineExperiment experiment = ExperimentBuilder.OnlineExperiment()
35+
.withApiKey("someApiKey")
36+
.withProjectName("someProject")
37+
.withWorkspace("someWorkspace")
38+
.build()) {
39+
experiment.setExperimentName("My experiment");
40+
experiment.logParameter("batch_size", "500");
41+
experiment.logMetric("strMetric", 123);
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
}
45+
```
3646

37-
# Setting configuration in code:
38-
OnlineExperimentImpl.builder().withApiKey("someApiKey").build();
47+
### Configure experiment object
3948

40-
# Override configuration file (can have partial keys)
41-
OnlineExperimentImpl.builder().withConfig(new File("/tmp/comet.conf")).build();
49+
#### Configuration sources hierarchy
4250

43-
# Read from environment variables OR from configuration file in classpath (application.conf)
44-
OnlineExperimentImpl.builder().build();
45-
```
51+
The configuration parameters search order as following (first-listed are higher priority):
52+
* system properties or environment variables
53+
* configuration file set by call to [```withConfigOverride(java.io.File)```](comet-java-client/src/main/java/ml/comet/experiment/builder/BaseCometBuilder.java)
54+
* ```application.conf``` (all resources on the classpath with this name)
55+
* ```reference.conf``` (all resources on the classpath with this name)
56+
57+
#### Programmatic configuration
4658

47-
#### Full list of environment variables:
59+
It is possible to override some or all configuration parameters programmatically when
60+
you create a new experiment's instance using [```ExperimentBuilder```](comet-java-client/src/main/java/ml/comet/experiment/ExperimentBuilder.java)
61+
factory.
4862
```java
63+
// Setting specific configuration parameters with builder
64+
ExperimentBuilder.OnlineExperiment().withApiKey("someApiKey").build();
65+
66+
// Override configuration file (can have partial keys)
67+
ExperimentBuilder.OnlineExperiment().withConfigOverride(new File("/tmp/comet.conf")).build();
68+
69+
// Read from environment variables OR from configuration file in classpath (application.conf)
70+
ExperimentBuilder.OnlineExperiment().build();
71+
```
72+
73+
### Full list of environment variables
74+
```text
4975
COMET_API_KEY
5076
COMET_PROJECT_NAME
5177
COMET_WORKSPACE_NAME
5278
COMET_BASE_URL
5379
COMET_MAX_AUTH_RETRIES
5480
```
5581

56-
#### Examples
82+
### Examples
5783

5884
* You also can check
5985
* [sample experiment](comet-examples/src/main/java/ml/comet/examples/OnlineExperimentExample.java)

comet-examples/src/main/java/ml/comet/examples/OnlineExperimentExample.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.apache.commons.io.file.PathUtils;
77

88
import java.io.IOException;
9+
import java.net.URI;
910
import java.nio.file.Files;
1011
import java.nio.file.Path;
1112
import java.util.Objects;
@@ -53,14 +54,14 @@ public static void main(String[] args) {
5354

5455
try {
5556
OnlineExperimentExample.run(experiment);
56-
} catch (IOException e) {
57+
} catch (Throwable e) {
5758
e.printStackTrace();
5859
} finally {
5960
experiment.end();
6061
}
6162
}
6263

63-
private static void run(OnlineExperiment experiment) throws IOException {
64+
private static void run(OnlineExperiment experiment) throws Exception {
6465
experiment.setExperimentName("Java-SDK 2.0.2");
6566
experiment.nextStep();
6667

@@ -81,16 +82,23 @@ private static void run(OnlineExperiment experiment) throws IOException {
8182
experiment.logParameter("batch_size", "500");
8283
experiment.logParameter("learning_rate", 12);
8384

85+
// upload assets
86+
//
8487
experiment.uploadAsset(getResourceFile(CHART_IMAGE_FILE), "amazing chart.png", false);
8588
experiment.uploadAsset(getResourceFile(MODEL_FILE), false,
8689
ExperimentContext.builder().withContext("train").build());
8790

8891
experiment.nextStep();
8992

90-
// upload assets from folder
93+
// upload asset files from folder
94+
//
9195
Path assetDir = copyResourcesToTmpDir();
9296
experiment.logAssetFolder(assetDir.toFile(), true, true);
9397

98+
// log remote assets
99+
//
100+
experiment.logRemoteAsset(new URI("s3://bucket/folder/dataCorpus.hd5"), "modelDataCorpus", false);
101+
94102
experiment.logOther("Parameter", 4);
95103

96104
System.out.println("Epoch 1/20");

comet-java-client/src/main/java/ml/comet/experiment/Experiment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* <p>Your {@link Experiment} will automatically track and collect many things and will also
2323
* allow you to manually report anything.
2424
*/
25-
public interface Experiment {
25+
public interface Experiment extends AutoCloseable {
2626

2727
/**
2828
* Get the experiment key returned by Comet.

comet-java-client/src/main/java/ml/comet/experiment/OnlineExperiment.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.io.File;
66
import java.io.IOException;
7+
import java.net.URI;
8+
import java.util.Map;
79

810
/**
911
* The {@code OnlineExperiment} should be used to asynchronously update data of your Comet.ml experiment.
@@ -15,7 +17,7 @@
1517
* <p>Also, it is possible to use {@link #setStep(long)}, {@link #setEpoch(long)},
1618
* and {@link #setContext(String)} which will bbe automatically associated with related logged data records.
1719
*/
18-
public interface OnlineExperiment extends Experiment, AutoCloseable {
20+
public interface OnlineExperiment extends Experiment {
1921

2022
/**
2123
* Turn on intercept of stdout and stderr and the logging of both in Comet.
@@ -141,4 +143,26 @@ public interface OnlineExperiment extends Experiment, AutoCloseable {
141143
void logAssetFolder(File folder, boolean logFilePath, boolean recursive);
142144

143145
void logAssetFolder(File folder, boolean logFilePath);
146+
147+
/**
148+
* Logs a Remote Asset identified by a {@link URI}. A Remote Asset is an asset but its content is not uploaded
149+
* and stored on Comet. Rather a link for its location is stored, so you can identify and distinguish
150+
* between two experiment using different version of a dataset stored somewhere else.
151+
*
152+
* @param uri the {@link URI} pointing to the remote asset location. There is no imposed format,
153+
* and it could be a private link.
154+
* @param fileName the optional "name" of the remote asset, could be a dataset name, a model file name.
155+
* @param overwrite if {@code true} will overwrite all existing assets with the same name.
156+
* @param metadata Some additional data to attach to the remote asset.
157+
* The dictionary values must be JSON compatible.
158+
* @param context the experiment context to be associated with the logged assets.
159+
*/
160+
void logRemoteAsset(URI uri, String fileName, boolean overwrite,
161+
Map<String, Object> metadata, ExperimentContext context);
162+
163+
void logRemoteAsset(URI uri, String fileName, boolean overwrite, Map<String, Object> metadata);
164+
165+
void logRemoteAsset(URI uri, String fileName, boolean overwrite);
166+
167+
void logRemoteAsset(URI uri, boolean overwrite);
144168
}

comet-java-client/src/main/java/ml/comet/experiment/context/ExperimentContext.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import lombok.NonNull;
55
import org.apache.commons.lang3.StringUtils;
66

7+
import java.util.Objects;
8+
79
/**
810
* Describes context of the {@link ml.comet.experiment.Experiment}.
911
*/
@@ -48,6 +50,17 @@ public ExperimentContext(long step) {
4850
this(step, 0);
4951
}
5052

53+
/**
54+
* The copy constructor.
55+
*
56+
* @param other the other {@link ExperimentContext} to be used as source.
57+
*/
58+
public ExperimentContext(ExperimentContext other) {
59+
this.step = other.step;
60+
this.epoch = other.epoch;
61+
this.context = other.context;
62+
}
63+
5164
/**
5265
* Merges not empty values from other context into this one.
5366
*
@@ -58,10 +71,10 @@ public void mergeFrom(@NonNull ExperimentContext other) {
5871
return;
5972
}
6073

61-
if (other.step != null) {
74+
if (Objects.nonNull(other.step)) {
6275
this.step = other.step;
6376
}
64-
if (other.epoch != null) {
77+
if (Objects.nonNull(other.epoch)) {
6578
this.epoch = other.epoch;
6679
}
6780
if (StringUtils.isNotBlank(other.context)) {

comet-java-client/src/main/java/ml/comet/experiment/impl/ApiExperimentImpl.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ private ApiExperimentImpl(
3838
if (logger != null) {
3939
this.logger = logger;
4040
}
41-
42-
// initialize this experiment
43-
this.init();
4441
}
4542

4643
@Override
@@ -77,6 +74,11 @@ public Optional<String> getExperimentLink() {
7774
}
7875
}
7976

77+
@Override
78+
public void close() {
79+
this.end();
80+
}
81+
8082
/**
8183
* Returns builder to create {@link Experiment} instance.
8284
*
@@ -136,11 +138,20 @@ public ApiExperiment build() {
136138
if (StringUtils.isBlank(this.apiKey)) {
137139
this.apiKey = COMET_API_KEY.getString();
138140
}
139-
return new ApiExperimentImpl(
141+
ApiExperimentImpl experiment = new ApiExperimentImpl(
140142
this.apiKey, this.experimentKey, this.logger,
141143
COMET_BASE_URL.getString(),
142144
COMET_MAX_AUTH_RETRIES.getInt(),
143145
COMET_TIMEOUT_CLEANING_SECONDS.getDuration());
146+
try {
147+
// initialize experiment
148+
experiment.init();
149+
} catch (Throwable ex) {
150+
// release hold resources and signal to user about failure
151+
experiment.end();
152+
throw ex;
153+
}
154+
return experiment;
144155
}
145156
}
146157
}

comet-java-client/src/main/java/ml/comet/experiment/impl/BaseExperiment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,12 @@ public void uploadAsset(@NonNull File file, @NonNull String fileName,
400400
}
401401

402402
@Override
403-
public void uploadAsset(File asset, String fileName, boolean overwrite, long step, long epoch) {
403+
public void uploadAsset(@NonNull File asset, String fileName, boolean overwrite, long step, long epoch) {
404404
this.uploadAsset(asset, fileName, overwrite, new ExperimentContext(step, epoch));
405405
}
406406

407407
@Override
408-
public void uploadAsset(File asset, boolean overwrite, ExperimentContext context) {
408+
public void uploadAsset(@NonNull File asset, boolean overwrite, @NonNull ExperimentContext context) {
409409
this.uploadAsset(asset, asset.getName(), overwrite, context);
410410
}
411411

0 commit comments

Comments
 (0)