|
| 1 | +from attr import attrs, attrib |
| 2 | +import pandas |
| 3 | + |
| 4 | +import faculty # TODO: Avoid possible circular imports |
| 5 | + |
| 6 | + |
| 7 | +class QueryResult(object): |
| 8 | + def __init__(self, iterable): |
| 9 | + self.iterable = iterable |
| 10 | + |
| 11 | + def __iter__(self): |
| 12 | + return iter(self.iterable) |
| 13 | + |
| 14 | + |
| 15 | +class ExperimentRunQueryResult(QueryResult): |
| 16 | + def as_dataframe(self): |
| 17 | + records = [] |
| 18 | + for run in self: |
| 19 | + row = { |
| 20 | + "Experiment ID": run.experiment_id, |
| 21 | + "Run ID": run.id, |
| 22 | + "Status": run.status.value, |
| 23 | + "Started At": run.started_at, |
| 24 | + } |
| 25 | + for metric in run.metrics: |
| 26 | + row[metric.key] = row[metric.value] |
| 27 | + records.append(row) |
| 28 | + return pandas.DataFrame(records) |
| 29 | + |
| 30 | + |
| 31 | +@attrs |
| 32 | +class ExperimentRun(object): |
| 33 | + id = attrib() |
| 34 | + run_number = attrib() |
| 35 | + experiment_id = attrib() |
| 36 | + name = attrib() |
| 37 | + parent_run_id = attrib() |
| 38 | + artifact_location = attrib() |
| 39 | + status = attrib() |
| 40 | + started_at = attrib() |
| 41 | + ended_at = attrib() |
| 42 | + deleted_at = attrib() |
| 43 | + tags = attrib() |
| 44 | + params = attrib() |
| 45 | + metrics = attrib() |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def _from_client_model(cls, client_object): |
| 49 | + return cls(**client_object._asdict()) |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def query(cls, project_id, experiment_ids=None): |
| 53 | + def get_runs(): |
| 54 | + client = faculty.client("experiment") |
| 55 | + |
| 56 | + response = client.list_runs(project_id, experiment_ids) |
| 57 | + yield from map(cls._from_client_model, response.runs) |
| 58 | + |
| 59 | + while response.pagination.next is not None: |
| 60 | + response = client.list_runs( |
| 61 | + project_id, |
| 62 | + experiment_ids, |
| 63 | + start=response.pagination.next.start, |
| 64 | + limit=response.pagination.next.limit, |
| 65 | + ) |
| 66 | + yield from map(cls._from_client_model, response.runs) |
| 67 | + |
| 68 | + # Open question: |
| 69 | + # Should we evalutate the entire set of runs before returning the |
| 70 | + # result, or is it ok to have them lazily evaluated |
| 71 | + return ExperimentRunQueryResult(get_runs()) |
0 commit comments