def test_fs_invalid(): """Argument must be a string or a filesystem instance.""" with pytest.raises(ValueError): FSReporter(100) with pytest.raises(ValueError): FSReporter(None)
def local_reporter(folder: str, job_name: str): """Returns a reporter implementation that persists metrics on the local filesystem in jsonl format. """ local_path = fs.path.join(folder, job_name) return FSReporter(local_path).stepped()
def test_casfs_reader(tmpdir, casfs): """Check that we can persist metrics into a content-addressable store and get them back out. """ dir_path = str(tmpdir) with closing(FSReporter(dir_path).stepped()) as reporter: with closing(reporter.reader()) as reader: reporter.report_all(0, {"a": 1}) reporter.report_all(1, {"a": 2, "b": 3}) reporter.report_all(2, {"b": 4}) a_entries = [{"step": 0, "value": 1}, {"step": 1, "value": 2}] b_entries = [{"step": 1, "value": 3}, {"step": 2, "value": 4}] # Persist the whole metrics directory to the CAS. k is a key that # references the zipped metrics dir. # # If you store k.id (a string) in SQL, you can use it later to get access # to the metrics, as we'll see below. k = u.persist_to_cas_via_memory(casfs, dir_path) # This reader can read directly from the CasFS. with closing(CASReader(casfs, k.id)) as cas_reader: assert list(cas_reader.keys()) == list(reader.keys()) # Contents are all identical: assert reader.read_all(["a", "b"]) == cas_reader.read_all(["a", "b"]) # and the contents match what's expected: assert cas_reader.read_all(["a", "b"]) == { "a": a_entries, "b": b_entries }
def gcloud_reporter(prefix: str, job_name: str): """Returns a reporter which persists metrics in GCloud in jsonl format. """ if prefix.endswith("/"): prefix = prefix[:-1] cloud_path = f"{prefix}/{job_name}?strict=False" gcsfs = fs.open_fs(cloud_path) return FSReporter(gcsfs).stepped()
def gcloud_reporter(prefix: str, job_name: str): """Returns a reporter implementation that persists metrics in GCloud in jsonl format. NOTE this is almost identical to local_reporter... the only different is the strict=False option. """ if prefix.endswith("/"): prefix = prefix[:-1] cloud_path = f"{prefix}/{job_name}?strict=False" gcsfs = fs.open_fs(cloud_path) return FSReporter(gcsfs).stepped()
def main(g, t1=0.0, t2=1.0, n=8, alpha=0.5, steps=1000, step_size=1e-2, absolute_tolerance=1e-10, relative_tolerance=1e-10, log_path="notebooks/output/repl", reporter_outputs=20): """This seems to work up to n=7. import catenary.single as s from uv.fs.reader import FSReader reader = FSReader("notebooks/cake/face1") data=reader.read_all(["eigenvalue", "t2"]) s.plot_metrics(data, 2, 2) Some example calls: import catenary.search as s s.main(g=1.2, n=5, alpha=1, steps=1000) s.main(g=1.2, n=100, alpha=0.5, step_size=1e-2, steps=2000) s.main(g=1.2, alpha=0.6630955754113801, t2=0.48642791, n=1000, step_size=1e-5, absolute_tolerance=1e-8, relative_tolerance=1e-8, steps=100) """ # Build the reporter that we'll use to generate results. fs = FSReporter(log_path).stepped() logging = u.LoggingReporter(digits=10) quot, _ = divmod(steps, reporter_outputs) # reporter = fs.plus(logging).report_each_n(quot) reporter = logging.report_each_n(quot) loss_fn = partial(cj.jacfwd(min_eigenvalue, argnums=4), n, alpha, g, t1) stop_fn = u.either( u.max_step(steps), u.either(u.positive_loss, u.stop_below(relative_tolerance, absolute_tolerance))) return gradient_ascent(initial=t2, loss_grad_fn=loss_fn, stop_fn=stop_fn, optimizer=optimizer, reporter=reporter)
def test_fs_many(tmpdir, m): tmp = tmpdir.mkdir(str(uuid.uuid1())) dir_path = str(tmp) with closing(FSReporter(dir_path).stepped()) as reporter: with closing(reporter.reader()) as reader: reporter.report_all(0, m) # round-tripping keys expected = {} for k, v in m.items(): expected.setdefault(k, []).append({"step": 0, "value": v}) # check that the reader returns everything in the map written to the fs. assert reader.read_all(list(m.keys())) == expected # Check that the reader has all the goods! assert set(reader.keys()) == set(m.keys())
def test_fs_roundtrip(tmpdir): dir_path = str(tmpdir) with closing(FSReporter(dir_path).stepped()) as reporter: with closing(reporter.reader()) as reader: with closing(FSReader(dir_path)) as reader2: reporter.report_all(0, {"a": 1}) reporter.report_all(1, {"a": 2, "b": 3}) reporter.report_all(2, {"b": 4}) a_entries = [{"step": 0, "value": 1}, {"step": 1, "value": 2}] b_entries = [{"step": 1, "value": 3}, {"step": 2, "value": 4}] assert reader.read("a") == a_entries assert reader.read("b") == b_entries assert reader.read_all(["a", "b"]) == {"a": a_entries, "b": b_entries} # Opening a reader directly has the same effect as calling reader() on # the reporter. assert reader.read_all(["a", "b"]) == reader2.read_all(["a", "b"]) # Checking for a key that doesn't exist returns an empty list. assert reader.read("face") == []