-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathApiExamples.java
124 lines (108 loc) · 4.96 KB
/
ApiExamples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package ml.comet.examples;
import ml.comet.experiment.CometApi;
import ml.comet.experiment.ExperimentBuilder;
import ml.comet.experiment.OnlineExperiment;
import ml.comet.experiment.model.ExperimentMetadata;
import ml.comet.experiment.model.Project;
import ml.comet.experiment.model.Value;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
/**
* Provides variety of examples of data logging using REST API client.
*
* <p>To run from command line execute the following at the root of this module:
* <pre>
* COMET_API_KEY=your_api_key \
* COMET_WORKSPACE_NAME=your_workspace \
* COMET_PROJECT_NAME=your_project_name \
* mvn exec:java -Dexec.mainClass="ml.comet.examples.ApiExamples"
* </pre>
* Make sure to provide correct values above.
*/
public class ApiExamples {
static final String randomExperimentName = UUID.randomUUID().toString();
/**
* The main entry point to the example.
*
* @param args the command line arguments if any.
*/
public static void main(String[] args) throws Exception {
ApiExamples.run();
}
private static void run() throws Exception {
// create test experiment
//
String experimentProjectName;
String experimentWorkspaceName;
String experimentKey;
try (OnlineExperiment experiment = ExperimentBuilder
.OnlineExperiment()
.interceptStdout()
.build()) {
experiment.setExperimentName(randomExperimentName);
experiment.logMetric("some-metric", 10);
experimentProjectName = experiment.getProjectName();
experimentWorkspaceName = experiment.getWorkspaceName();
experimentKey = experiment.getExperimentKey();
}
// Comet API usage examples
//
try (CometApi api = ExperimentBuilder.CometApi().build()) {
// get all workspaces available for the user
//
System.out.println("===== All user's workspaces =====");
List<String> availableWorkspaces = api.getAllWorkspaces();
availableWorkspaces.forEach(System.out::println);
System.out.println("=====\n");
// get all projects under particular workspace
//
String workspace = availableWorkspaces.get(0);
System.out.printf("===== Projects in workspace: '%s' =====\n", workspace);
List<Project> projects = api.getAllProjects(workspace);
projects.forEach(System.out::println);
System.out.println("=====\n");
// get all experiments under particular project
//
Project project = projects.get(0);
System.out.printf("===== First 10 Experiments in project: '%s' =====\n", project.getProjectName());
List<ExperimentMetadata> experiments = api.getAllExperiments(project.getProjectId());
experiments.stream()
.filter(experimentMetadata -> !Objects.isNull(experimentMetadata.getExperimentName()))
.limit(10)
.forEach(System.out::println);
System.out.println("=====\n");
// get experiment(-s) by name/regex (using the one we created above)
//
System.out.printf("===== Experiment(-s) by name/regex '%s' in workspace/project: '%s/%s' =====\n",
randomExperimentName, experimentWorkspaceName, experimentProjectName);
experiments = api.getExperiments(experimentWorkspaceName, experimentProjectName, randomExperimentName);
if (experiments.size() == 1) {
displayExperiment(experiments.get(0));
} else {
System.out.printf("*** No, or more than one experiment was found. Found: %d experiments.\n",
experiments.size());
}
System.out.println("=====\n");
// get experiment's meta-data by experiment key
//
System.out.printf("===== Experiment's metadata by experiment key: '%s' =====\n", experimentKey);
ExperimentMetadata metadata = api.getExperimentMetadata(experimentKey);
System.out.println(metadata);
System.out.println("=====\n");
}
}
private static void displayExperiment(ExperimentMetadata experimentMetadata) throws Exception {
try (OnlineExperiment experiment = ExperimentBuilder.OnlineExperiment()
.withExistingExperimentKey(experimentMetadata.getExperimentKey()).build()) {
System.out.printf("\nFound experiment: [%s] with key: %s\n",
experimentMetadata.getExperimentName(), experimentMetadata.getExperimentKey());
List<Value> metrics = experiment.getMetrics();
System.out.println("Metrics:");
for (Value metric : metrics) {
System.out.printf("\t%s : %s\n", metric.getName(), metric.getCurrent());
}
System.out.println("====================");
}
}
}