Synthetic data generation
Generation for synthetic datasets.
qim3d.generate.ParameterVisualizer
Interactive Jupyter widget for exploring synthetic data generation parameters.
Provides a Graphical User Interface (GUI) to tune the parameters of qim3d.generate.volume
in real-time. Users can adjust sliders for noise scale, threshold, and gamma while immediately
seeing the resulting 3D structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_shape
|
tuple
|
Determines the shape of the generate volume. This will not be updated when exploring parameters and must be determined when generating the visualizer. |
(128, 128, 128)
|
final_shape
|
tuple
|
Desired shape of the final volume. If unspecified, will assume same shape as base_shape. |
None
|
seed
|
int
|
Determines the seed for the volume generation. Enables the user to generate different volumes with the same parameters. |
0
|
hollow
|
int
|
Determines thickness of the hollowing operation. Volume is only hollowed if hollow>0. |
0
|
initial_config
|
dict
|
Dictionary that defines the starting parameters of the visualizer. Can be used if a specific setup is needed. The dictionary may contain the keywords: |
None
|
nsmin
|
float
|
Determines minimum value for the noise scale slider. |
0.0
|
nsmax
|
float
|
Determines maximum value for the noise scale slider. |
0.1
|
dsmin
|
float
|
Determines minimum value for the decay rate slider. |
0.1
|
dsmax
|
float
|
Determines maximum value for the decay rate slider. |
20.0
|
gsmin
|
float
|
Determines minimum value for the gamma slider. |
0.1
|
gsmax
|
float
|
Determines maximum value for the gamma slider. |
2.0
|
tsmin
|
float
|
Determines minimum value for the threshold slider. |
0.0
|
tsmax
|
float
|
Determines maximum value for the threshold slider. |
1.0
|
grid_visible
|
bool
|
Determines if the grid should be visible upon plot generation. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If either |
Accessing the current volume
The most recently generated 3D volume can be retrieved at any time using the .get_volume() method:
Source code in qim3d/generate/_generators.py
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 | |
qim3d.generate.ParameterVisualizer.get_volume
Extracts the generated volume from the widget's current state.
Allows you to retrieve the numpy array resulting from your interactive adjustments so you can use it in your pipeline (e.g., saving it or using it for training).
Returns:
| Name | Type | Description |
|---|---|---|
vol |
ndarray
|
The 3D volume currently visualized in the widget. |
Example
Source code in qim3d/generate/_generators.py
qim3d.generate.volume
volume(
base_shape=(128, 128, 128),
final_shape=None,
noise_scale=0.02,
noise_type='perlin',
decay_rate=10,
gamma=1,
threshold=0.5,
max_value=255,
shape=None,
tube_hole_ratio=0.5,
axis=0,
order=1,
dtype='uint8',
hollow=0,
seed=0,
)
Generates a synthetic 3D volume using structured Perlin noise.
Creates valid 3D morphological structures that resemble biological or material samples (e.g., cells, tissues, pores). By default, it generates a "blob-like" object, but it can also create specific geometric shapes like cylinders or tubes.
This function is ideal for:
- Benchmarking: Creating standard inputs for testing algorithms.
- Augmentation: Generating synthetic samples to train deep learning models.
- Simulation: Modeling physical structures with controlled noise properties.
Supported Shapes:
- Blob: (Default) amorphous, organic-looking structure.
- Cylinder: A solid cylindrical rod.
- Tube: A hollow cylinder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_shape
|
tuple
|
The resolution of the internal noise grid. Higher values create finer details but require more computation. |
(128, 128, 128)
|
final_shape
|
tuple
|
The final output resolution. If |
None
|
noise_scale
|
float
|
Controls the "zoom" of the noise texture. Smaller values = smooth, large features. Larger values = rough, high-frequency details. |
0.02
|
noise_type
|
str
|
The noise algorithm: |
'perlin'
|
decay_rate
|
float
|
Controls how quickly the object fades into the background at the edges. Higher values create sharper, distinct boundaries. |
10
|
gamma
|
float
|
Adjusts contrast. |
1
|
threshold
|
float
|
The cut-off value (0.0 to 1.0) defining the object's surface. Lower values make the object larger/fatter; higher values make it smaller/thinner. |
0.5
|
max_value
|
float
|
The maximum intensity value in the output array (e.g., 255 for 8-bit images). |
255
|
shape
|
str
|
Forces the volume into a geometric shape: |
None
|
tube_hole_ratio
|
float
|
Only for |
0.5
|
axis
|
int
|
The orientation axis (0, 1, or 2) for cylinders and tubes. |
0
|
order
|
int
|
Interpolation order when resizing to |
1
|
dtype
|
str
|
Output data type (e.g., |
'uint8'
|
hollow
|
int
|
If > 0, hollows out the blob by eroding the center, creating a shell of thickness |
0
|
seed
|
int
|
Random seed for reproducibility. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
vol |
ndarray
|
The generated 3D volume. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If either |
TypeError
|
If |
ValueError
|
If |
Example
Example:
import qim3d
# Generate synthetic blob
vol = qim3d.generate.volume(noise_scale = 0.02)
# Visualize 3D volume
qim3d.viz.volumetric(vol)
Example
import qim3d
# Generate tubular synthetic blob
vol = qim3d.generate.volume(base_shape = (200, 100, 100),
final_shape = (400,100,100),
noise_scale = 0.03,
threshold = 0.85,
decay_rate=20,
gamma=0.15,
shape = "tube",
tube_hole_ratio = 0.4,
)
# Visualize synthetic volume
qim3d.viz.volumetric(vol)
Example
import qim3d
# Generate tubular synthetic blob
vol = qim3d.generate.volume(base_shape = (200, 100, 100),
final_shape = (400, 100, 100),
noise_scale = 0.03,
gamma = 0.12,
threshold = 0.85,
shape = "tube",
)
# Visualize synthetic blob
qim3d.viz.volumetric(vol)
Source code in qim3d/generate/_generators.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | |
qim3d.generate.volume_collection
volume_collection(
n_volumes=15,
collection_shape=(200, 200, 200),
data=None,
positions=None,
shape_range=((40, 40, 40), (60, 60, 60)),
shape_magnification_range=(1.0, 1.0),
noise_type='perlin',
noise_range=(0.02, 0.03),
rotation_degree_range=(0, 360),
rotation_axes=None,
gamma_range=(0.9, 1),
value_range=(128, 255),
threshold_range=(0.5, 0.55),
decay_rate_range=(5, 10),
shape=None,
tube_hole_ratio=0.5,
axis=0,
verbose=False,
same_seed=False,
hollow=False,
seed=0,
dtype='uint8',
return_positions=False,
)
Generates a synthetic dataset of multiple non-overlapping volumes with ground truth labels.
This function creates a "collection" volume populated with multiple objects. It uses a collision detection algorithm to ensure objects do not overlap. Crucially, it returns a label mask where each object is identified by a unique integer ID, making this tool ideal for generating training data for Instance Segmentation or Object Detection models.
Objects can be generated synthetically (blobs, cylinders, tubes) with randomized properties,
or you can inject your own pre-existing 3D arrays using the data parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_volumes
|
int
|
Target number of volumes/objects to place in the collection. |
15
|
collection_shape
|
tuple
|
Dimensions of the final container volume (Z, Y, X). |
(200, 200, 200)
|
data
|
ndarray or list[ndarray]
|
Pre-defined 3D volume(s) to place into the collection. If provided, the function picks randomly from this list instead of generating new synthetic blobs. Useful for creating scenes with specific real-world objects. |
None
|
positions
|
list[tuple]
|
List of specific (z, y, x) center coordinates for placement. If |
None
|
shape_range
|
tuple[tuple]
|
Defines the size variance of generated objects. Format: |
((40, 40, 40), (60, 60, 60))
|
shape_magnification_range
|
tuple[float]
|
Range for random uniform scaling factors applied to the object shape. |
(1.0, 1.0)
|
noise_type
|
str
|
Algorithm for synthetic texture generation: |
'perlin'
|
noise_range
|
tuple[float]
|
Range for the noise scale parameter (roughness). |
(0.02, 0.03)
|
rotation_degree_range
|
tuple[int]
|
Range of rotation angles (in degrees) to apply to each object. |
(0, 360)
|
rotation_axes
|
list[tuple]
|
List of axis pairs to rotate around (e.g., |
None
|
gamma_range
|
tuple[float]
|
Range for gamma correction (contrast). |
(0.9, 1)
|
value_range
|
tuple[int]
|
Range for the maximum intensity value of the objects. |
(128, 255)
|
threshold_range
|
tuple[float]
|
Range for the threshold used to define the object surface/size. |
(0.5, 0.55)
|
decay_rate_range
|
tuple[float]
|
Range for the edge decay rate (fading at boundaries). |
(5, 10)
|
shape
|
str
|
Force a specific geometric shape: |
None
|
tube_hole_ratio
|
float
|
Ratio of the inner hole if |
0.5
|
axis
|
int
|
Orientation axis (0, 1, 2) if |
0
|
verbose
|
bool
|
If |
False
|
same_seed
|
bool
|
If |
False
|
hollow
|
bool
|
If |
False
|
seed
|
int
|
Global random seed for reproducibility. |
0
|
dtype
|
str
|
Data type of the output arrays. |
'uint8'
|
return_positions
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
volume_collection |
ndarray
|
The 3D volume containing all placed objects. |
labels |
ndarray
|
A 3D integer mask of the same shape, where 0 is background and 1..N are the object IDs. |
positions |
list[tuple]
|
(Only if |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
ValueError
|
If ranges or shapes are incorrectly defined. |
Example
import qim3d
# Generate synthetic collection of volumes
n_volumes = 15
volume_collection, labels = qim3d.generate.volume_collection(n_volumes=n_volumes)
# Visualize the collection
qim3d.viz.volumetric(volume_collection, grid_visible=True)
# Visualize labels
cmap = qim3d.viz.colormaps.segmentation(n_labels=n_volumes)
qim3d.viz.slicer(labels, colormap=cmap, max_value=n_volumes)
Collection of fiber-like structures
import qim3d
# Generate synthetic collection of cylindrical structures
volume_collection, labels = qim3d.generate.volume_collection(
n_volumes = 40,
collection_shape = (300, 150, 150),
shape_range = ((280, 10, 10), (290, 15, 15)),
noise_range = (0.06,0.09),
rotation_degree_range = (0,5),
threshold_range = (0.1,0.3),
gamma_range = (0.10, 0.20),
shape = "cylinder"
)
# Visualize the collection
qim3d.viz.volumetric(volume_collection)
Create a collection of tubular (hollow) structures
import qim3d
# Generate synthetic collection of tubular (hollow) structures
volume_collection, labels = qim3d.generate.volume_collection(
n_volumes = 10,
collection_shape = (200, 200, 200),
shape_range = ((185,35,35), (190,45,45)),
noise_range = (0.02, 0.03),
rotation_degree_range = (0,5),
threshold_range = (0.6, 0.7),
gamma_range = (0.1, 0.11),
shape = "tube",
tube_hole_ratio = 0.15,
)
# Visualize the collection
qim3d.viz.volumetric(volume_collection)
Using predefined volumes
import qim3d
# Generate two unique volumes to be used
volume_1 = qim3d.generate.volume(base_shape = (32,32,32), noise_scale = 0.0)
volume_2 = qim3d.generate.volume(base_shape = (32,32,32), noise_scale = 0.2)
# Generate collection from predefined volumes
volume_collection, labels = qim3d.generate.volume_collection(n_volumes = 30,
data = [volume_1, volume_2])
# Visualize
qim3d.viz.volumetric(volume_collection)
Source code in qim3d/generate/_aggregators.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 | |
qim3d.generate.background
background(
background_shape,
baseline_value=0,
min_noise_value=0,
max_noise_value=20,
generate_method='add',
apply_method=None,
seed=0,
dtype='uint8',
apply_to=None,
)
Generates a 3D noise field or adds synthetic background noise to an existing volume.
Unlike volume (which creates structures), this function generates unstructured uniform noise.
It is useful for simulating:
- Sensor Noise: Electronic noise or grain common in CT/microscopy scans.
- Imaging Artifacts: Low-contrast background variations.
- Data Augmentation: Making training data more robust by adding random interference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
background_shape
|
tuple
|
The shape of the noise volume to generate (Z, Y, X). |
required |
baseline_value
|
float
|
The constant base intensity level of the background. |
0
|
min_noise_value
|
float
|
The lower bound of the random noise distribution. |
0
|
max_noise_value
|
float
|
The upper bound of the random noise distribution. |
20
|
generate_method
|
str
|
How to combine the baseline with the noise: |
'add'
|
apply_method
|
str
|
If |
None
|
seed
|
int
|
Random seed for reproducibility. |
0
|
dtype
|
str
|
Output data type. |
'uint8'
|
apply_to
|
ndarray
|
An existing 3D volume. If provided, the noise is applied directly to this array
using |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
background |
ndarray
|
The noise volume (if |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
Example
Example
import qim3d
# Generate synthetic collection of volumes
volume_collection, labels = qim3d.generate.volume_collection(num_volumes = 15)
# Apply noise to the synthetic collection
noisy_collection = qim3d.generate.background(
background_shape = volume_collection.shape,
min_noise_value = 0,
max_noise_value = 20,
generate_method = 'add',
apply_method = 'add',
apply_to = volume_collection
)
qim3d.viz.volumetric(noisy_collection)
Example
import qim3d
# Generate synthetic collection of volumes
volume_collection, labels = qim3d.generate.volume_collection(num_volumes = 15)
# Apply noise to the synthetic collection
noisy_collection = qim3d.generate.background(
background_shape = volume_collection.shape,
baseline_value = 0,
min_noise_value = 0,
max_noise_value = 30,
generate_method = 'add',
apply_method = 'divide',
apply_to = volume_collection
)
qim3d.viz.volumetric(noisy_collection)
Example
import qim3d
# Generate synthetic collection of volumes
volume_collection, labels = qim3d.generate.volume_collection(num_volumes = 15)
# Apply noise to the synthetic collection
noisy_collection = qim3d.generate.background(
background_shape = (200, 200, 200),
baseline_value = 100,
min_noise_value = 0.8,
max_noise_value = 1.2,
generate_method = "multiply",
apply_method = "add",
apply_to = volume_collection
)
qim3d.viz.slices_grid(noisy_collection, num_slices=10, color_bar=True, color_bar_style="large")
Source code in qim3d/generate/_generators.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |