|
| 1 | +package ml.comet.examples; |
| 2 | + |
| 3 | +import ml.comet.experiment.CometApi; |
| 4 | +import ml.comet.experiment.ExperimentBuilder; |
| 5 | +import ml.comet.experiment.OnlineExperiment; |
| 6 | +import ml.comet.experiment.model.ExperimentMetadata; |
| 7 | +import ml.comet.experiment.model.Project; |
| 8 | +import ml.comet.experiment.model.Value; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.Optional; |
| 13 | + |
| 14 | +/** |
| 15 | + * Provides variety of examples of data logging using REST API client. |
| 16 | + * |
| 17 | + * <p>To run from command line execute the following at the root of this module: |
| 18 | + * <pre> |
| 19 | + * COMET_API_KEY=your_api_key \ |
| 20 | + * COMET_WORKSPACE_NAME=your_workspace \ |
| 21 | + * COMET_PROJECT_NAME=your_project_name \ |
| 22 | + * mvn exec:java -Dexec.mainClass="ml.comet.examples.ApiExamples" |
| 23 | + * </pre> |
| 24 | + * Make sure to provide correct values above. |
| 25 | + */ |
| 26 | +public class ApiExamples { |
| 27 | + static final String someExperimentName = "some-experiment-name"; |
| 28 | + |
| 29 | + /** |
| 30 | + * The main entry point to the example. |
| 31 | + * |
| 32 | + * @param args the command line arguments if any. |
| 33 | + */ |
| 34 | + public static void main(String[] args) throws Exception { |
| 35 | + ApiExamples.run(); |
| 36 | + } |
| 37 | + |
| 38 | + private static void run() throws Exception { |
| 39 | + // create test experiment |
| 40 | + // |
| 41 | + String projectName; |
| 42 | + String workspaceName; |
| 43 | + try (OnlineExperiment experiment = ExperimentBuilder |
| 44 | + .OnlineExperiment() |
| 45 | + .interceptStdout() |
| 46 | + .build()) { |
| 47 | + |
| 48 | + experiment.setExperimentName(someExperimentName); |
| 49 | + experiment.logMetric("some-metric", 10); |
| 50 | + |
| 51 | + projectName = experiment.getProjectName(); |
| 52 | + workspaceName = experiment.getWorkspaceName(); |
| 53 | + } |
| 54 | + |
| 55 | + // get test experiment by name |
| 56 | + // |
| 57 | + try (CometApi api = ExperimentBuilder.CometApi().build()) { |
| 58 | + // get project where experiment saved |
| 59 | + List<Project> projects = api.getAllProjects(workspaceName); |
| 60 | + Optional<Project> optionalProject = projects.stream() |
| 61 | + .filter(project -> project.getProjectName().equals(projectName)) |
| 62 | + .findAny(); |
| 63 | + if (!optionalProject.isPresent()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + Project project = optionalProject.get(); |
| 67 | + System.out.printf("Looking for experiments in project: %s\n---------\n", project); |
| 68 | + |
| 69 | + // list all experiments in the project and select the one we are looking for |
| 70 | + List<ExperimentMetadata> experiments = api.getAllExperiments(project.getProjectId()); |
| 71 | + Optional<ExperimentMetadata> experimentMetadata = experiments.stream() |
| 72 | + .peek(System.out::println) |
| 73 | + .filter(meta -> Objects.equals(meta.getExperimentName(), someExperimentName)) |
| 74 | + .findAny(); |
| 75 | + if (experimentMetadata.isPresent()) { |
| 76 | + displayExperiment(experimentMetadata.get()); |
| 77 | + } else { |
| 78 | + System.out.printf("Failed to find experiment with name: %s\n", someExperimentName); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static void displayExperiment(ExperimentMetadata experimentMetadata) throws Exception { |
| 84 | + try (OnlineExperiment experiment = ExperimentBuilder.OnlineExperiment() |
| 85 | + .withExistingExperimentKey(experimentMetadata.getExperimentKey()).build()) { |
| 86 | + |
| 87 | + System.out.printf("\nFound experiment: [%s] with key: %s\n", |
| 88 | + experimentMetadata.getExperimentName(), experimentMetadata.getExperimentKey()); |
| 89 | + |
| 90 | + List<Value> metrics = experiment.getMetrics(); |
| 91 | + System.out.println("Metrics:"); |
| 92 | + for (Value metric : metrics) { |
| 93 | + System.out.printf("\t%s : %s\n", metric.getName(), metric.getCurrent()); |
| 94 | + } |
| 95 | + |
| 96 | + System.out.println("===================="); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments