Skip to content

Commit 0c5cc34

Browse files
authored
Merge pull request #39 from comet-ml/CM-1592-log-asset-folder
[CM-1592] Support for logging of assets folder
2 parents 8e231d4 + 17c4dee commit 0c5cc34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2380
-725
lines changed

.github/linters/comet-checkstyle.xml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
<property name="fileNamePattern" value="module\-info\.java$"/>
3030
</module>
3131
<!-- https://checkstyle.org/config_filters.html#SuppressionFilter -->
32-
<module name="SuppressionFilter">
33-
<property name="file" value="${org.checkstyle.google.suppressionfilter.config}"
34-
default="checkstyle-suppressions.xml" />
35-
<property name="optional" value="true"/>
36-
</module>
32+
<!-- <module name="SuppressionFilter">-->
33+
<!-- <property name="file" value="${org.checkstyle.google.suppressionfilter.config}"-->
34+
<!-- default="checkstyle-suppressions.xml" />-->
35+
<!-- <property name="optional" value="true"/>-->
36+
<!-- </module>-->
3737

3838
<!-- Checks for whitespace -->
3939
<!-- See http://checkstyle.org/config_whitespace.html -->
@@ -47,7 +47,15 @@
4747
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
4848
</module>
4949

50+
<module name="SuppressWarningsFilter"/>
51+
5052
<module name="TreeWalker">
53+
<module name="SuppressionCommentFilter">
54+
<property name="offCommentFormat" value="@cs-\: ([\w\|]+)"/>
55+
<property name="checkFormat" value="$1"/>
56+
</module>
57+
<module name="SuppressWarningsHolder" />
58+
5159
<module name="OuterTypeFilename"/>
5260
<module name="IllegalTokenText">
5361
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>

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

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
import ml.comet.experiment.ExperimentBuilder;
44
import ml.comet.experiment.OnlineExperiment;
5+
import ml.comet.experiment.context.ExperimentContext;
6+
import org.apache.commons.io.file.PathUtils;
57

68
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.util.Objects;
712

813
import static ml.comet.examples.Utils.getResourceFile;
914
import static ml.comet.examples.Utils.readResourceToString;
@@ -22,21 +27,19 @@
2227
*/
2328
public class OnlineExperimentExample {
2429

30+
private static final String CHART_IMAGE_FILE = "chart.png";
31+
private static final String MODEL_FILE = "model.hd5";
32+
private static final String HTML_REPORT_FILE = "report.html";
33+
private static final String GRAPH_JSON_FILE = "graph.json";
34+
private static final String CODE_FILE = "code_sample.py";
35+
2536
/**
2637
* The main entry point to the example.
2738
*
2839
* @param args the command line arguments if any.
2940
*/
3041
public static void main(String[] args) {
31-
OnlineExperimentExample main = new OnlineExperimentExample();
32-
try {
33-
main.run();
34-
} catch (IOException e) {
35-
e.printStackTrace();
36-
}
37-
}
3842

39-
private void run() throws IOException {
4043
//this will take configs from /comet-java-sdk/comet-examples/src/main/resources/application.conf
4144
//be sure you have set up apiKey, project, workspace in defaults.conf before you start!
4245

@@ -48,6 +51,16 @@ private void run() throws IOException {
4851
//you can use a default builder or just inject params
4952
//OnlineExperiment experiment = ExperimentBuilder.OnlineExperiment().builder();
5053

54+
try {
55+
OnlineExperimentExample.run(experiment);
56+
} catch (IOException e) {
57+
e.printStackTrace();
58+
} finally {
59+
experiment.end();
60+
}
61+
}
62+
63+
private static void run(OnlineExperiment experiment) throws IOException {
5164
experiment.setExperimentName("Java-SDK 2.0.2");
5265
experiment.nextStep();
5366

@@ -68,20 +81,33 @@ private void run() throws IOException {
6881
experiment.logParameter("batch_size", "500");
6982
experiment.logParameter("learning_rate", 12);
7083

71-
experiment.uploadAsset(getResourceFile("chart.png"), "amazing chart.png", false);
72-
experiment.uploadAsset(getResourceFile("model.hd5"), false);
84+
experiment.uploadAsset(getResourceFile(CHART_IMAGE_FILE), "amazing chart.png", false);
85+
experiment.uploadAsset(getResourceFile(MODEL_FILE), false,
86+
ExperimentContext.builder().withContext("train").build());
87+
88+
experiment.nextStep();
89+
90+
// upload assets from folder
91+
Path assetDir = copyResourcesToTmpDir();
92+
experiment.logAssetFolder(assetDir.toFile(), true, true);
7393

7494
experiment.logOther("Parameter", 4);
7595

7696
System.out.println("Epoch 1/20");
7797
System.out.println("- loss: 0.7858 - acc: 0.7759 - val_loss: 0.3416 - val_acc: 0.9026");
7898

79-
experiment.logGraph(readResourceToString("graph.json"));
99+
experiment.logGraph(readResourceToString(GRAPH_JSON_FILE));
100+
101+
experiment.logCode(getResourceFile(CODE_FILE),
102+
ExperimentContext.builder().withContext("test").build());
80103

81104
System.out.println("===== Experiment completed ====");
82105

83106
// will close connection, if not called connection will close on jvm exit
84107
experiment.end();
108+
109+
// remove tmp directory
110+
PathUtils.deleteDirectory(assetDir);
85111
}
86112

87113
private static void generateCharts(OnlineExperiment experiment) {
@@ -108,4 +134,20 @@ private static long getUpdatedEpochValue(OnlineExperiment experiment) {
108134
return experiment.getEpoch() + experiment.getStep() / 5;
109135
}
110136

137+
private static Path copyResourcesToTmpDir() throws IOException {
138+
Path root = Files.createTempDirectory("onlineExperimentExample");
139+
PathUtils.copyFileToDirectory(
140+
Objects.requireNonNull(getResourceFile(CHART_IMAGE_FILE)).toPath(), root);
141+
PathUtils.copyFileToDirectory(
142+
Objects.requireNonNull(getResourceFile(MODEL_FILE)).toPath(), root);
143+
Files.createTempFile(root, "empty_file", ".txt");
144+
145+
Path subDir = Files.createTempDirectory(root, "subDir");
146+
PathUtils.copyFileToDirectory(
147+
Objects.requireNonNull(getResourceFile(HTML_REPORT_FILE)).toPath(), subDir);
148+
PathUtils.copyFileToDirectory(
149+
Objects.requireNonNull(getResourceFile(GRAPH_JSON_FILE)).toPath(), subDir);
150+
151+
return root;
152+
}
111153
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# some code goes here

comet-java-client/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
<artifactId>commons-lang3</artifactId>
5050
<version>3.12.0</version>
5151
</dependency>
52+
<dependency>
53+
<groupId>commons-io</groupId>
54+
<artifactId>commons-io</artifactId>
55+
<version>2.11.0</version>
56+
</dependency>
5257
<dependency>
5358
<groupId>com.typesafe</groupId>
5459
<artifactId>config</artifactId>
Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
package ml.comet.experiment;
22

3-
import lombok.NonNull;
4-
import lombok.experimental.UtilityClass;
5-
import ml.comet.experiment.builder.ApiExperimentBuilder;
6-
import ml.comet.experiment.impl.ApiExperimentImpl;
7-
83
/**
9-
* This is stub to support backward compatibility.
10-
*
11-
* @deprecated It would be replaced in the future with new experiment creation API.
4+
* The {@code ApiExperiment} can be used to synchronously read/update data of your Comet.ml experiment.
125
*/
13-
@UtilityClass
14-
public final class ApiExperiment {
15-
/**
16-
* Returns builder to create {@link Experiment} instance.
17-
*
18-
* @param experimentKey the unique identifier of the existing experiment.
19-
* @return the initialized ApiExperiment instance.
20-
*/
21-
public static ApiExperimentBuilder builder(@NonNull final String experimentKey) {
22-
return ApiExperimentImpl.builder(experimentKey);
23-
}
6+
public interface ApiExperiment extends Experiment {
247
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ml.comet.experiment;
2+
3+
import lombok.NonNull;
4+
import lombok.experimental.UtilityClass;
5+
import ml.comet.experiment.builder.ApiExperimentBuilder;
6+
7+
/**
8+
* This is stub to support backward compatibility.
9+
*
10+
* @deprecated It would be replaced in the future with new experiment creation API.
11+
*/
12+
@UtilityClass
13+
public final class ApiExperimentImpl {
14+
/**
15+
* Returns builder to create {@link Experiment} instance.
16+
*
17+
* @param experimentKey the unique identifier of the existing experiment.
18+
* @return the initialized ApiExperiment instance.
19+
*/
20+
public static ApiExperimentBuilder builder(@NonNull final String experimentKey) {
21+
return ml.comet.experiment.impl.ApiExperimentImpl.builder(experimentKey);
22+
}
23+
}

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

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ml.comet.experiment;
22

3-
import ml.comet.experiment.impl.constants.AssetType;
3+
import ml.comet.experiment.context.ExperimentContext;
4+
import ml.comet.experiment.impl.asset.AssetType;
45
import ml.comet.experiment.model.ExperimentAssetLink;
56
import ml.comet.experiment.model.ExperimentMetadataRest;
67
import ml.comet.experiment.model.GitMetadata;
@@ -140,11 +141,22 @@ public interface Experiment {
140141
/**
141142
* Send logs to Comet.
142143
*
143-
* @param line Text to be logged
144-
* @param offset Offset describes the place for current text to be inserted
145-
* @param stderr the flag to indicate if this is StdErr message.
144+
* @param line Text to be logged
145+
* @param offset Offset describes the place for current text to be inserted
146+
* @param stderr the flag to indicate if this is StdErr message.
147+
* @param context the context to be associated with the parameter.
146148
*/
147-
void logLine(String line, long offset, boolean stderr);
149+
void logLine(String line, long offset, boolean stderr, String context);
150+
151+
/**
152+
* Logs a metric with Comet. For running experiment updates current step to one from param!
153+
* Metrics are generally values that change from step to step.
154+
*
155+
* @param metricName The name for the metric to be logged
156+
* @param metricValue The new value for the metric. If the values for a metric are plottable we will plot them.
157+
* @param context the context to be associated with the metric.
158+
*/
159+
void logMetric(String metricName, Object metricValue, ExperimentContext context);
148160

149161
/**
150162
* Logs a metric with Comet. For running experiment updates current step to one from param!
@@ -153,18 +165,27 @@ public interface Experiment {
153165
* @param metricName The name for the metric to be logged
154166
* @param metricValue The new value for the metric. If the values for a metric are plottable we will plot them
155167
* @param step The current step for this metric, this will set the given step for this experiment
156-
* @param epoch The current epoch for this metric, this will set the given epoch for this experiment
168+
* @param epoch The current epoch for this metric, this will set the given epoch for this experiment.
157169
*/
158170
void logMetric(String metricName, Object metricValue, long step, long epoch);
159171

172+
/**
173+
* Logs a param with Comet. For running experiment updates current step to one from param!
174+
* Params should be set at the start of the experiment.
175+
*
176+
* @param parameterName The name of the param being logged
177+
* @param paramValue The value for the param being logged
178+
* @param context the context to be associated with the parameter.
179+
*/
180+
void logParameter(String parameterName, Object paramValue, ExperimentContext context);
160181

161182
/**
162183
* Logs a param with Comet. For running experiment updates current step to one from param!
163184
* Params should be set at the start of the experiment.
164185
*
165186
* @param parameterName The name of the param being logged
166187
* @param paramValue The value for the param being logged
167-
* @param step The current step for this metric, this will set the given step for this experiment
188+
* @param step The current step for this metric, this will set the given step for this experiment.
168189
*/
169190
void logParameter(String parameterName, Object paramValue, long step);
170191

@@ -214,6 +235,15 @@ public interface Experiment {
214235
*/
215236
void logEndTime(long endTimeMillis);
216237

238+
/**
239+
* Allows you to report code for the experiment.
240+
*
241+
* @param code Code to be sent to Comet
242+
* @param fileName Name of source file to be displayed on UI 'code' tab
243+
* @param context the context to be associated with the asset.
244+
*/
245+
void logCode(String code, String fileName, ExperimentContext context);
246+
217247
/**
218248
* Allows you to report code for the experiment.
219249
*
@@ -222,6 +252,14 @@ public interface Experiment {
222252
*/
223253
void logCode(String code, String fileName);
224254

255+
/**
256+
* Allows you to report code for the experiment.
257+
*
258+
* @param file Asset with source code to be sent
259+
* @param context the context to be associated with the asset.
260+
*/
261+
void logCode(File file, ExperimentContext context);
262+
225263
/**
226264
* Allows you to report code for the experiment.
227265
*
@@ -236,11 +274,41 @@ public interface Experiment {
236274
* @param asset The asset to be stored
237275
* @param fileName The file name under which the asset should be stored in Comet. E.g. "someFile.txt"
238276
* @param overwrite Whether to overwrite files of the same name in Comet
239-
* @param step the step to be associated with asset
240-
* @param epoch the epoch to be associated with asset
277+
* @param context the context to be associated with the asset.
278+
*/
279+
void uploadAsset(File asset, String fileName, boolean overwrite, ExperimentContext context);
280+
281+
/**
282+
* Upload an asset to be associated with the experiment, for example the trained weights of a neural net.
283+
* For running experiment updates current step to one from param!
284+
*
285+
* @param asset The asset to be stored
286+
* @param fileName The file name under which the asset should be stored in Comet. E.g. "someFile.txt"
287+
* @param overwrite Whether to overwrite files of the same name in Comet
288+
* @param step the step to be associated with the asset
289+
* @param epoch the epoch to be associated with the asset
241290
*/
242291
void uploadAsset(File asset, String fileName, boolean overwrite, long step, long epoch);
243292

293+
/**
294+
* Upload an asset to be associated with the experiment, for example the trained weights of a neural net.
295+
* For running experiment updates current step to one from param!
296+
*
297+
* @param asset The file asset to be stored. The name of the file will be used as assets identifier on Comet.
298+
* @param overwrite Whether to overwrite files of the same name in Comet
299+
* @param context the context to be associated with the asset.
300+
*/
301+
void uploadAsset(File asset, boolean overwrite, ExperimentContext context);
302+
303+
/**
304+
* Upload an asset to be associated with the experiment, for example the trained weights of a neural net.
305+
* For running experiment updates current step to one from param!
306+
*
307+
* @param asset The file asset to be stored. The name of the file will be used as assets identifier on Comet.
308+
* @param overwrite Whether to overwrite files of the same name in Comet
309+
* @param step the step to be associated with the asset
310+
* @param epoch the epoch to be associated with the asset
311+
*/
244312
void uploadAsset(File asset, boolean overwrite, long step, long epoch);
245313

246314
/**

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package ml.comet.experiment;
22

33
import lombok.experimental.UtilityClass;
4+
import ml.comet.experiment.builder.ApiExperimentBuilder;
45
import ml.comet.experiment.builder.OnlineExperimentBuilder;
6+
import ml.comet.experiment.impl.ApiExperimentImpl;
57
import ml.comet.experiment.impl.OnlineExperimentImpl;
68

79
/**
@@ -23,7 +25,19 @@ public class ExperimentBuilder {
2325
*
2426
* @return the instance of the {@link OnlineExperimentBuilder}.
2527
*/
28+
@SuppressWarnings({"MethodName"})
2629
public static OnlineExperimentBuilder OnlineExperiment() {
2730
return OnlineExperimentImpl.builder();
2831
}
32+
33+
/**
34+
* The factory to create instance of the {@link ApiExperimentBuilder} which can be used
35+
* to configure and create fully initialized instance of the {@link ApiExperiment}.
36+
*
37+
* @return the initialized instance of the {@link ApiExperimentBuilder}.
38+
*/
39+
@SuppressWarnings({"MethodName"})
40+
public static ApiExperimentBuilder ApiExperiment() {
41+
return ApiExperimentImpl.builder();
42+
}
2943
}

0 commit comments

Comments
 (0)