Adding iteration functionality to ItemizedResource

Hi everyone,

I’m new to the forum and have a suggestion I’d like to discuss. I’ve been working with ItemizedResources quite a bit lately, and I believe adding an iter() and associated method would improve their usability and efficiency.

In my current projects, I often need to iterate through the items in an ItemizedResource. Right now, this requires manual logic loops, which can be cumbersome and less efficient than a built-in iterator.

Before I dive deeper into a formal proposal, I wanted to get the community’s thoughts on this idea. Specifically, I’m interested in:

  • Potential drawbacks: Are there any reasons why adding iter() might be problematic or undesirable?
  • Desired features: If we were to implement this, what specific features or functionalities should the iterator include? (e.g., filtering, sorting, etc.)

I’m looking forward to hearing your feedback and exploring this possibility further!

3 Likes

there’s already the ItemizedResource.traverse method that could fit your needs. If you’re missing anything, please make a PR :slight_smile:

I find the use of iterators for programmatic sample management very powerful.
But regarding drawbacks to using an iterator directly associated with ItemizedResource:
It only provides a solution for every ItemizedResource instance.

It’s unlikely that you’ll only use e.g. a single Plate for an assay.
If you have 3 Plates then, in this scenario, you have to manage 3 separate iterators… that’s not efficient.

You could circumvent this issue by concatenating/chaining the iterators, e.g.:

import itertools

# Example iterators
iter1 = iter([1, 2, 3])
iter2 = iter([4, 5, 6])
iter3 = iter([7, 8, 9])

# Concatenate the iterators
concatenated_iter = itertools.chain(iter1, iter2, iter3)

# Iterate over the concatenated iterator
for value in concatenated_iter:
    print(value)

But what if you want to - as you eluted to - sort the wells you want to iterate over first?
(e.g. process all plates’ columns first before moving to the next column… very useful)

I prefer sorting the children of plates first, then creating an iterator over the sorted list.
This is quite specific to the automated protocols you’re running, and therefore is likely to be defined specifically :slight_smile:

for traversing tip spots in multiple tip racks i added this add tip spot generators by rickwierenga · Pull Request #256 · PyLabRobot/pylabrobot · GitHub, maybe we can have something similar for Wells, or even use the same code

1 Like

Thanks for the feedback! The tip spot generators largely resolves the issue that was making my code a bit more complex than it felt like it needed to be, I think it might be good to generalize the use though, particularly for mapping from a TipRack to sets of plates, that could be very powerful.