Reverse iterator of run.scan_history

Hello, I’m trying to use the wandb API to get the last step where some metric has been logged.
Currently, using run.scan_history, I need to scan through the whole history which is time consuming if I only need the last. I wonder is there a way to scan in reverse/backwards?
Thanks!

Hi @kaiwenw thanks for writing in! The last value logged with wandb.log is automatically set as the summary dictionary in a W&B Run. Therefore if you were interested only in the last value, it would be most efficient to access the run.summary dictionary instead. Would this work for you?

Hi @thanos-wandb , thanks for the response!
Regarding run.summary I have two issues:

  1. In some runs, run.summary is not populated with the last value, eg Weights & Biases. My code does not manually update run.summary so is this a bug?
  2. Even if run.summary is correct, I think it wouldn’t work for my case since I would like to get the last step or global_step where a specific key was logged; so I care about when the value was logged, not just the value itself. Which is why I think a reverse iterator would help since I can keep iterating until I find a step with that key set.

Hi @kaiwenw thanks for the update. In that case, it seems that indeed you will need to use the .scan_history to get all unsampled metrics. There’s one method applied to api.run that can give you the latest logged step, and you can then use this to iterate from that point the history metrics (which will be the last value). Please see a code snippet below:

api = wandb.Api()
run = api.run('entity/project/run-id')
print(run.lastHistoryStep)
for i in run.scan_history(keys=['_step', 'metric-name'], min_step=run.lastHistoryStep):
    print(i)

I hope this helps! Please let me know if you had any other question.

Hi @kaiwenw just checking in here to see if the above worked for you, and if you had any other questions? Thansk!

Hi @kaiwenw since we haven’t heard back from you, and the above code snippet should be the solution you’re looking for, I will go ahead and close this ticket. If you had any more questions, please let us know and we will be happy to reopen the converstation!

Thanks @thanos-wandb for the solution and sorry for the delay!
In your code snippet, run.lastHistoryStep returns the last step anything was logged, right?
So for example, supposeI have two metrics, metricA and metricB, and in some steps I only log metricA, or only log metricB.
I want to get the step number of the last time metricA was logged, but in fact metricB was logged last (so run.lastHistoryStep is the last step which I logged metricB). In this case, your code snippet would not actually return anything for metricA, right?

Thanks @thanos-wandb for the solution and sorry for the delay!
In your code snippet, run.lastHistoryStep returns the last step anything was logged, right?
So for example, supposeI have two metrics, metricA and metricB, and in some steps I only log metricA, or only log metricB.
I want to get the step number of the last time metricA was logged, but in fact metricB was logged last (so run.lastHistoryStep is the last step which I logged metricB). In this case, your code snippet would not actually return anything for metricA, right?

[Discourse post]

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.