Skip to content

Commit 2680a35

Browse files
authored
Merge pull request #72 from comet-ml/CM-4552-COMET_EXPERIMENT_KEY
[CM-4552]: Allow COMET_EXPERIMENT_KEY as environment variable
2 parents 7e38515 + 5f91e95 commit 2680a35

File tree

9 files changed

+98
-7
lines changed

9 files changed

+98
-7
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.HashMap;
1212
import java.util.Map;
1313
import java.util.Objects;
14+
import java.util.Random;
1415

1516
import static java.lang.Math.log;
1617
import static ml.comet.examples.Utils.getResourceFile;
@@ -35,17 +36,24 @@ interface BaseExample {
3536

3637
static void generateCharts(OnlineExperiment experiment) {
3738
long currentStep = experiment.getStep();
39+
Random rd = new Random();
3840

3941
for (int i = 1; i < 15; i++) {
40-
experiment.logMetric("numMetric", 123 + i, currentStep + i, getUpdatedEpochValue(experiment));
42+
int value = (int) (Math.sin(i) * 20.0);
43+
experiment.logMetric("numMetricChart", value,
44+
currentStep + i, getUpdatedEpochValue(experiment));
4145
}
4246

4347
for (int i = 1; i < 15; i++) {
44-
experiment.logMetric("strMetric", "123" + i, currentStep + i, getUpdatedEpochValue(experiment));
48+
float value = rd.nextFloat() * 100;
49+
experiment.logMetric("strMetricChart", String.valueOf(value),
50+
currentStep + i, getUpdatedEpochValue(experiment));
4551
}
4652

4753
for (int i = 1; i < 15; i++) {
48-
experiment.logMetric("doubleMetric", 123.12d + i, currentStep + i, getUpdatedEpochValue(experiment));
54+
double value = Math.cos(i);
55+
experiment.logMetric("doubleMetricChart", 123.12d + value,
56+
currentStep + i, getUpdatedEpochValue(experiment));
4957
}
5058
}
5159

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ml.comet.examples;
2+
3+
import ml.comet.experiment.ExperimentBuilder;
4+
import ml.comet.experiment.OnlineExperiment;
5+
import ml.comet.experiment.impl.config.CometConfig;
6+
7+
import java.util.Objects;
8+
import java.util.Random;
9+
import java.util.UUID;
10+
11+
/**
12+
* Provides variety of examples of updating existing Comet experiment.
13+
*
14+
* <p>To run from command line execute the following at the root of this module:
15+
* <pre>
16+
* COMET_API_KEY=your_api_key \
17+
* COMET_EXPERIMENT_KEY=existing_experiment_key
18+
* mvn exec:java -Dexec.mainClass="ml.comet.examples.ExistingExperimentExample"
19+
* </pre>
20+
* Make sure to provide correct values above.
21+
*/
22+
public class ExistingExperimentExample {
23+
24+
/**
25+
* The main entry point to the example.
26+
*
27+
* @param args the command line arguments if any.
28+
*/
29+
public static void main(String[] args) throws Exception {
30+
// check that COMET_EXPERIMENT_KEY environment variable is set
31+
if (Objects.isNull(System.getenv(CometConfig.COMET_EXPERIMENT_KEY.getEnvironmentKey()))) {
32+
System.err.println("To run this experiment you should set COMET_EXPERIMENT_KEY environment variable "
33+
+ "with ID of existing Comet experiment."
34+
);
35+
System.exit(1);
36+
}
37+
38+
try (OnlineExperiment experiment = ExperimentBuilder.OnlineExperiment().build()) {
39+
runExample(experiment);
40+
}
41+
}
42+
43+
private static void runExample(OnlineExperiment experiment) {
44+
// log some continuous data
45+
Random rd = new Random();
46+
String metricName = UUID.randomUUID().toString();
47+
for (int i = 0; i < 100; i++) {
48+
experiment.logMetric(metricName, rd.nextDouble() * 20, i);
49+
}
50+
51+
// log single metric value
52+
experiment.logMetric(metricName, 20, 100);
53+
}
54+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ private static void run(OnlineExperiment experiment) throws Exception {
5656
experiment.nextStep();
5757

5858
//metric can be a number, string , or double
59-
experiment.logMetric("strMetric", "123");
59+
experiment.logMetric("strMetric", "123", 1);
6060
experiment.logMetric("numMetric", 123, 123, 4);
6161
experiment.nextEpoch();
62-
experiment.logMetric("doubleMetric", 123.5d);
62+
experiment.logMetric("doubleMetric", 123.5d, 1);
6363

6464
experiment.setEpoch(3);
6565

comet-java-client/src/main/java/ml/comet/experiment/exception/CometGeneralException.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public CometGeneralException(String message) {
2626

2727
/**
2828
* Constructs a new runtime exception with the specified detail message and
29-
* cause. <p>Note that the detail message associated with
30-
* {@code cause} is <i>not</i> automatically incorporated in
29+
* cause.
30+
*
31+
* <p>Note that the detail message associated with {@code cause} is <i>not</i> automatically incorporated in
3132
* this runtime exception's detail message.
3233
*
3334
* @param message the detail message (which is saved for later retrieval

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static ml.comet.experiment.impl.config.CometConfig.COMET_API_KEY;
1919
import static ml.comet.experiment.impl.config.CometConfig.COMET_BASE_URL;
20+
import static ml.comet.experiment.impl.config.CometConfig.COMET_EXPERIMENT_KEY;
2021
import static ml.comet.experiment.impl.config.CometConfig.COMET_MAX_AUTH_RETRIES;
2122
import static ml.comet.experiment.impl.config.CometConfig.COMET_PROJECT_NAME;
2223
import static ml.comet.experiment.impl.config.CometConfig.COMET_TIMEOUT_CLEANING_SECONDS;
@@ -173,6 +174,9 @@ public ApiExperiment build() {
173174
if (StringUtils.isBlank(this.workspace)) {
174175
this.workspace = COMET_WORKSPACE_NAME.getOptionalString().orElse(null);
175176
}
177+
if (StringUtils.isBlank(this.experimentKey)) {
178+
this.experimentKey = COMET_EXPERIMENT_KEY.getOptionalString().orElse(null);
179+
}
176180
ApiExperimentImpl experiment = new ApiExperimentImpl(
177181
this.apiKey, this.experimentKey, this.logger,
178182
COMET_BASE_URL.getString(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import static ml.comet.experiment.impl.config.CometConfig.COMET_API_KEY;
1313
import static ml.comet.experiment.impl.config.CometConfig.COMET_BASE_URL;
14+
import static ml.comet.experiment.impl.config.CometConfig.COMET_EXPERIMENT_KEY;
1415
import static ml.comet.experiment.impl.config.CometConfig.COMET_MAX_AUTH_RETRIES;
1516
import static ml.comet.experiment.impl.config.CometConfig.COMET_PROJECT_NAME;
1617
import static ml.comet.experiment.impl.config.CometConfig.COMET_TIMEOUT_CLEANING_SECONDS;
@@ -111,6 +112,9 @@ public OnlineExperimentImpl build() {
111112
if (StringUtils.isBlank(this.baseUrl)) {
112113
this.baseUrl = COMET_BASE_URL.getString();
113114
}
115+
if (StringUtils.isBlank(this.experimentKey)) {
116+
this.experimentKey = COMET_EXPERIMENT_KEY.getOptionalString().orElse(null);
117+
}
114118
if (this.maxAuthRetries == -1) {
115119
this.maxAuthRetries = COMET_MAX_AUTH_RETRIES.getInt();
116120
}

comet-java-client/src/main/java/ml/comet/experiment/impl/config/CometConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public final class CometConfig {
5050
*/
5151
public static final ConfigItem COMET_PROJECT_NAME =
5252
new ConfigItem("project", "COMET_PROJECT_NAME", instance);
53+
/**
54+
* The key of the existing Comet experiment.
55+
*/
56+
public static final ConfigItem COMET_EXPERIMENT_KEY =
57+
new ConfigItem("experimentKey", "COMET_EXPERIMENT_KEY", instance);
5358
/**
5459
* The Comet workspace name.
5560
*/

comet-java-client/src/test/java/ml/comet/experiment/impl/config/CometConfigTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,18 @@ public void testClearOverrideConfig() {
125125
assertNotNull(timeout);
126126
assertEquals(timeout.getSeconds(), 3600);
127127
}
128+
129+
@Test
130+
public void testReadAllConfigVariables() {
131+
CometConfig.applyConfigOverride(fullCometConfig);
132+
String defaultStringValue = "full";
133+
134+
assertEquals(defaultStringValue, CometConfig.COMET_API_KEY.getString());
135+
assertEquals(defaultStringValue, CometConfig.COMET_PROJECT_NAME.getString());
136+
assertEquals(defaultStringValue, CometConfig.COMET_EXPERIMENT_KEY.getString());
137+
assertEquals(defaultStringValue, CometConfig.COMET_WORKSPACE_NAME.getString());
138+
assertEquals("https://www.comet.ml", CometConfig.COMET_BASE_URL.getString());
139+
assertEquals(5, CometConfig.COMET_MAX_AUTH_RETRIES.getInt());
140+
assertEquals(60, CometConfig.COMET_TIMEOUT_CLEANING_SECONDS.getInt());
141+
}
128142
}

comet-java-client/src/test/resources/full-comet-config.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ comet {
44
apiKey = "full"
55
project = "full"
66
workspace = "full"
7+
experimentKey = "full"
78
cleaningTimeoutSeconds = 60
89
}

0 commit comments

Comments
 (0)