Subsets Node
See UPDATE below...
Here is the official release for the node developed with Floris in this thread: http://support.nodebox.net/discussions/nodebox-2-3/7233-multi_diff_...
Subsets
Subsets randomly divide a list of items into a multi-list of proportionally-sized subsets. There are also options to override the random order and, for paths and geometries, output each set in a separate group instead of using a multi-list.
Subsets takes 5 inputs:
- Items. The list to be divided up. Can be anything (paths, geometries, strings, etc.)
- Proportions. A list of numbers that define how many subsets you want and the relative size of each one.
- Random Numbers. An optional list of numbers which can be used to shuffle the subsets in a more controlled way.
- Seed. A random seed. Ignored if random numbers are provided.
- Output as groups. If checked, will output a list of separate groups instead of a multi-list. Ignored if Items contain anything other than a path or geometry.
For proportions you can provide percentages or any other list of numbers. The number of proportions determines the number of subsets. The relative size of each subset is determined by dividing each proportion by the sum of all the proportions.
For example, if you set proportions to 50,30,20, 50% of the items will go into the first subset, 30% into the second subset, and 20% into the third subset. 5,3,2 would do the same thing.
Fractional proportion values are rounded to the nearest integer. If the subsets contain less than the number of items due to rounding, one extra item will be added to each subset starting with the first. If subsets contain more then the number of items, one item will be removed from each subset starting with the last subset. This ensures that the final distribution is as evenly balanced and close to the desired proportions as possible.
Random numbers is a list of numbers which can be used to shuffle the subsets in a more controlled way. It is optional. If you don't supply it, subsets will just shuffle the items as before using the seed.
But if you DO supply such a list, it will sort the items using that list. For example, you can use my noise node to generate Perlin noise values and feed that into the random numbers port. Subsets will make a table matching each item to a random number, sort by those numbers, then retrieve the items in this new order.
The supplied proportion list will then be applied to this sorted list as before. I find this works well with Perlin noise, but the system is very flexible; you can use any wacky system you want to shuffle the deck before dividing it into proportional subsets. For example, instead of supplying a truly random set of numbers for sorting, you can use a range node to supply 1, 2, 3. This has the same effect as counting off by 3s as they do in the army. Everyone in the group counts off 1, 2, 3, 1, 2, 3, etc., then all the 1s go first, the 2s go next, and the 3s go last.
If you don't check the groups option, or the items contain something that can't be grouped, subsets will output a multi-list. This is simply a table which includes all the items, each under a list number starting with 0. You can either filter this table to extract a particular subset, or use my multi_switch node. For more about multi-lists see:
http://support.nodebox.net/discussions/show-your-work/886-multi-lists
Thie attached demo is simple but kind of fun (see screenshot). I scatter some points in a triangle, divide them into three sets (2/1/1 proportions) and turn them into 3 colored lines with little shapes at each point. You can get quite a wild assortment of lines by changing the seeds in the scatter or subsets nodes or changing the number of points.
Note that checking the Output as groups box doesn't matter in this case, since points cannot be grouped.
I also include a switch that lets you see another variation of subsets (change index of switch1 form 0 to 1). In this second variation I sort the scattered point list by X so they are arranged from left to right. I then supply 1,2,3 for the ransom numbers to force settings to divide the points into sets in order instead of randomly. Finally I change the proportions to 1,1,1 so that there are still 3 subsets but now each has the same number of points. Throw the switch to see the effect on the three colored lines.
Enjoy! And thanks again to Floris for suggesting this node and helping me improve it.
-
subsets_screenshot.png 532 KB
- subsets_demo.ndbx.zip 49.1 KB
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
Support Staff 1 Posted by john on 10 Apr, 2025 10:16 PM
UPDATE
After sleeping on this, I decided to make a small improvement to this node. Subsets was originally created to randomly divide a list into subsets. But that's really just a special case. Sometimes you may just want to divide a list evenly into subsets with no randomization.
So I made the following changes:
Strictly speaking, the option to subdivide the list without shuffling was already there. You can achieve the same effect by supplying "1,1" as your shuffle order (or "Random numbers"). You need to supply at least two numbers to override the normal shuffle mechanism, and by making them all the same the list is not shuffled. But this is not very intuitive. A separate "Don't shuffle" option is much clearer.
Updated demo attached. I added an additional option on the switch. Option 0 and 1 are as they were before. Option 2 is just like option 0 but with the "Don't shuffle" option checked. You can see the result in the screenshot.
Please use this improved version of subsets from now on.