Skip to content

Commit 030c124

Browse files
authored
Merge pull request #69 from comet-ml/CM-1579-log-text-type-asset
[CM-1579]: Log text type of the asset
2 parents 8347168 + b7097cb commit 030c124

File tree

17 files changed

+384
-64
lines changed

17 files changed

+384
-64
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ interface BaseExample {
2424
String GRAPH_JSON_FILE = "graph.json";
2525
String CODE_FILE = "code_sample.py";
2626

27+
Map<String, Object> SOME_METADATA = new HashMap<String, Object>() {{
28+
put("metaInt", 10);
29+
put("metaString", "test");
30+
put("metaBoolean", true);
31+
}};
32+
2733
static void generateCharts(OnlineExperiment experiment) {
2834
long currentStep = experiment.getStep();
2935

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ private static void run(OnlineExperiment experiment) throws Exception {
7171
experiment.logParameter("batch_size", "500");
7272
experiment.logParameter("learning_rate", 12);
7373

74+
experiment.logText("Sample text",
75+
ExperimentContext.builder()
76+
.withContext("train")
77+
.withStep(12).build(),
78+
SOME_METADATA);
79+
7480
// upload assets
7581
//
7682
experiment.uploadAsset(getResourceFile(CHART_IMAGE_FILE), "amazing chart.png", false);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.io.File;
1212
import java.util.List;
13+
import java.util.Map;
1314
import java.util.Optional;
1415

1516
/**
@@ -307,6 +308,20 @@ public interface Experiment extends AutoCloseable {
307308
*/
308309
void logCode(File file);
309310

311+
/**
312+
* Allows to log additional textual data associated with Comet Experiment.
313+
* These strings appear on the Text Tab in the Comet UI.
314+
*
315+
* @param text the text data to be logged.
316+
* @param context the current experiment context to be associated.
317+
* @param metadata the additional metadata for the text.
318+
*/
319+
void logText(String text, ExperimentContext context, Map<String, Object> metadata);
320+
321+
void logText(String text, ExperimentContext context);
322+
323+
void logText(String text);
324+
310325
/**
311326
* Upload an asset to be associated with the experiment, for example the trained weights of a neural net.
312327
* For running experiment updates current step to one from param!

comet-java-client/src/main/java/ml/comet/experiment/builder/ApiExperimentBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,20 @@ public interface ApiExperimentBuilder extends BaseCometBuilder<ApiExperiment> {
1414
* @return the builder configured with specified key of the previous experiment.
1515
*/
1616
ApiExperimentBuilder withExistingExperimentKey(String experimentKey);
17+
18+
/**
19+
* Set project name for the experiment.
20+
*
21+
* @param projectName The project under which the experiment should run
22+
* @return the builder configured with specified project name.
23+
*/
24+
ApiExperimentBuilder withProjectName(String projectName);
25+
26+
/**
27+
* Set workspace for the project.
28+
*
29+
* @param workspace The workspace under which the experiment should be run.
30+
* @return the builder configured with specified workspace name.
31+
*/
32+
ApiExperimentBuilder withWorkspace(String workspace);
1733
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,35 @@ public void mergeFrom(@NonNull ExperimentContext other) {
8282
}
8383
}
8484

85+
/**
86+
* Allows to check if this is empty context, i.e., has no value set.
87+
*
88+
* @return {@code true} if this is empty context.
89+
*/
90+
public boolean isEmpty() {
91+
return Objects.isNull(this.step) && Objects.isNull(this.epoch) && StringUtils.isBlank(this.context);
92+
}
93+
94+
/**
95+
* Indicates whether provided context object equals to this one.
96+
*
97+
* @param obj the instance of {@code ExperimentContext} to check.
98+
* @return {@code true} if provided context object equals to this one.
99+
*/
100+
public boolean equals(Object obj) {
101+
if (Objects.isNull(obj) || !(obj instanceof ExperimentContext)) {
102+
return false;
103+
}
104+
105+
if (this == obj) {
106+
return true;
107+
}
108+
ExperimentContext ctx = (ExperimentContext) obj;
109+
return Objects.equals(this.context, ctx.context)
110+
&& Objects.equals(this.epoch, ctx.epoch)
111+
&& Objects.equals(this.step, ctx.step);
112+
}
113+
85114
/**
86115
* The factory to return empty {@link ExperimentContext} instance.
87116
*

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import static ml.comet.experiment.impl.config.CometConfig.COMET_API_KEY;
1919
import static ml.comet.experiment.impl.config.CometConfig.COMET_BASE_URL;
2020
import static ml.comet.experiment.impl.config.CometConfig.COMET_MAX_AUTH_RETRIES;
21+
import static ml.comet.experiment.impl.config.CometConfig.COMET_PROJECT_NAME;
2122
import static ml.comet.experiment.impl.config.CometConfig.COMET_TIMEOUT_CLEANING_SECONDS;
23+
import static ml.comet.experiment.impl.config.CometConfig.COMET_WORKSPACE_NAME;
2224
import static ml.comet.experiment.impl.resources.LogMessages.EXPERIMENT_CLEANUP_PROMPT;
2325
import static ml.comet.experiment.impl.resources.LogMessages.getString;
2426

@@ -35,8 +37,10 @@ private ApiExperimentImpl(
3537
final Logger logger,
3638
final String baseUrl,
3739
int maxAuthRetries,
38-
final Duration cleaningTimeout) {
39-
super(apiKey, baseUrl, maxAuthRetries, experimentKey, cleaningTimeout);
40+
final Duration cleaningTimeout,
41+
final String projectName,
42+
final String workspaceName) {
43+
super(apiKey, baseUrl, maxAuthRetries, experimentKey, cleaningTimeout, projectName, workspaceName);
4044
if (logger != null) {
4145
this.logger = logger;
4246
}
@@ -45,6 +49,8 @@ private ApiExperimentImpl(
4549
@Override
4650
void init() {
4751
super.init();
52+
53+
registerExperiment();
4854
}
4955

5056
@Override
@@ -114,6 +120,8 @@ public static final class ApiExperimentBuilderImpl implements ApiExperimentBuild
114120
private String experimentKey;
115121
private String apiKey;
116122
private Logger logger;
123+
private String projectName;
124+
private String workspace;
117125

118126
private ApiExperimentBuilderImpl() {
119127
}
@@ -130,6 +138,18 @@ public ApiExperimentImpl.ApiExperimentBuilderImpl withApiKey(@NonNull final Stri
130138
return this;
131139
}
132140

141+
@Override
142+
public ApiExperimentImpl.ApiExperimentBuilderImpl withProjectName(@NonNull String projectName) {
143+
this.projectName = projectName;
144+
return this;
145+
}
146+
147+
@Override
148+
public ApiExperimentImpl.ApiExperimentBuilderImpl withWorkspace(@NonNull String workspace) {
149+
this.workspace = workspace;
150+
return this;
151+
}
152+
133153
@Override
134154
public ApiExperimentImpl.ApiExperimentBuilderImpl withLogger(@NonNull final Logger logger) {
135155
this.logger = logger;
@@ -147,11 +167,18 @@ public ApiExperiment build() {
147167
if (StringUtils.isBlank(this.apiKey)) {
148168
this.apiKey = COMET_API_KEY.getString();
149169
}
170+
if (StringUtils.isBlank(this.projectName)) {
171+
this.projectName = COMET_PROJECT_NAME.getOptionalString().orElse(null);
172+
}
173+
if (StringUtils.isBlank(this.workspace)) {
174+
this.workspace = COMET_WORKSPACE_NAME.getOptionalString().orElse(null);
175+
}
150176
ApiExperimentImpl experiment = new ApiExperimentImpl(
151177
this.apiKey, this.experimentKey, this.logger,
152178
COMET_BASE_URL.getString(),
153179
COMET_MAX_AUTH_RETRIES.getInt(),
154-
COMET_TIMEOUT_CLEANING_SECONDS.getDuration());
180+
COMET_TIMEOUT_CLEANING_SECONDS.getDuration(),
181+
this.projectName, this.workspace);
155182
try {
156183
// initialize experiment
157184
experiment.init();

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,16 @@
5757
import java.util.ArrayList;
5858
import java.util.Collection;
5959
import java.util.List;
60+
import java.util.Map;
6061
import java.util.Optional;
6162
import java.util.concurrent.CompletableFuture;
6263
import java.util.concurrent.ExecutionException;
6364

6465
import static java.util.Optional.empty;
6566
import static ml.comet.experiment.impl.asset.AssetType.ALL;
6667
import static ml.comet.experiment.impl.asset.AssetType.SOURCE_CODE;
68+
import static ml.comet.experiment.impl.asset.AssetType.TEXT_SAMPLE;
69+
import static ml.comet.experiment.impl.constants.Common.AUTOGENERATED_LOGICAL_PATH_HOLDER;
6770
import static ml.comet.experiment.impl.constants.SdkErrorCodes.artifactVersionStateNotClosed;
6871
import static ml.comet.experiment.impl.constants.SdkErrorCodes.artifactVersionStateNotClosedErrorOccurred;
6972
import static ml.comet.experiment.impl.constants.SdkErrorCodes.noArtifactFound;
@@ -137,14 +140,6 @@ abstract class BaseExperiment implements Experiment {
137140
*/
138141
protected abstract Logger getLogger();
139142

140-
BaseExperiment(@NonNull final String apiKey,
141-
@NonNull final String baseUrl,
142-
int maxAuthRetries,
143-
final String experimentKey,
144-
@NonNull final Duration cleaningTimeout) {
145-
this(apiKey, baseUrl, maxAuthRetries, experimentKey, cleaningTimeout, StringUtils.EMPTY, StringUtils.EMPTY);
146-
}
147-
148143
BaseExperiment(@NonNull final String apiKey,
149144
@NonNull final String baseUrl,
150145
int maxAuthRetries,
@@ -454,6 +449,27 @@ public void logCode(File file) {
454449
this.logCode(file, ExperimentContext.empty());
455450
}
456451

452+
@Override
453+
public void logText(String text, ExperimentContext context, Map<String, Object> metadata) {
454+
if (getLogger().isDebugEnabled()) {
455+
getLogger().debug("logging text {} with context {}", text, context);
456+
}
457+
AssetImpl asset = createAssetFromData(text.getBytes(StandardCharsets.UTF_8),
458+
AUTOGENERATED_LOGICAL_PATH_HOLDER, false,
459+
Optional.ofNullable(metadata), Optional.of(TEXT_SAMPLE.type()));
460+
this.logAsset(asset, context);
461+
}
462+
463+
@Override
464+
public void logText(String text, ExperimentContext context) {
465+
this.logText(text, context, null);
466+
}
467+
468+
@Override
469+
public void logText(String text) {
470+
this.logText(text, ExperimentContext.empty());
471+
}
472+
457473
@Override
458474
public void uploadAsset(@NonNull File file, @NonNull String logicalPath,
459475
boolean overwrite, @NonNull ExperimentContext context) {

0 commit comments

Comments
 (0)