Reference
Various functions for multidimensional cluster generation in Python.
Note that:
clugen()
is the main function of the pyclugen package, and possibly the only function most users will need.- Functions which accept
rng
as the last parameter are stochastic. Thus, in order to obtain the same result on separate invocations of these functions, pass them an instance of same pseudo-random numberGenerator
initialized with the same seed.
Clusters ¶
Bases: NamedTuple
Read-only container for results returned by clugen()
.
The symbols presented in the instances variable below have the following meanings:
- \(n\) : Number of dimensions.
- \(p\) : Number of points.
- \(c\) : Number of clusters.
Source code in pyclugen/main.py
angles
instance-attribute
¶
Vector of size \(c\) with the angles between the cluster-supporting lines and the main direction.
centers
instance-attribute
¶
\(c \times n\) matrix with the coordinates of the cluster centers.
clusters
instance-attribute
¶
Vector of size \(p\) indicating the cluster each point in points
belongs to.
directions
instance-attribute
¶
\(c \times n\) matrix with the direction of each cluster-supporting line.
lengths
instance-attribute
¶
Vector of size \(c\) with the lengths of the cluster-supporting lines.
points
instance-attribute
¶
\(p \times n\) matrix containing the generated points for all clusters.
projections
instance-attribute
¶
\(p \times n\) matrix with the point projections on the cluster-supporting lines.
sizes
instance-attribute
¶
Vector of size \(c\) with the number of points in each cluster.
angle_btw ¶
Angle between two \(n\)-dimensional vectors.
Typically, the angle between two vectors v1
and v2
can be obtained with:
However, this approach is numerically unstable. The version provided here is numerically stable and based on the AngleBetweenVectors Julia package by Jeffrey Sarnoff (MIT license), implementing an algorithm provided by Prof. W. Kahan in these notes (see page 15).
Examples:
>>> from numpy import array, degrees
>>> from pyclugen import angle_btw
>>> v1 = array([1.0, 1.0, 1.0, 1.0])
>>> v2 = array([1.0, 0.0, 0.0, 0.0])
>>> float(degrees(angle_btw(v1, v2)))
60.00000000000001
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v1 |
NDArray
|
First vector. |
required |
v2 |
NDArray
|
Second vector. |
required |
Returns:
Type | Description |
---|---|
float
|
Angle between |
Source code in pyclugen/helper.py
angle_deltas ¶
Get angles between average cluster direction and cluster-supporting lines.
Determine the angles between the average cluster direction and the
cluster-supporting lines. These angles are obtained from a wrapped normal
distribution ( \(\mu=0\), \(\sigma=\)angle_disp
) with support in the interval
\(\left[-\pi/2,\pi/2\right]\). Note this is different from the standard
wrapped normal distribution, the support of which is given by the interval
\(\left[-\pi,\pi\right]\).
Examples:
>>> from pyclugen import angle_deltas
>>> from numpy import degrees, pi
>>> from numpy.random import Generator, PCG64
>>> prng = Generator(PCG64(123))
>>> a_rad = angle_deltas(4, pi/8, rng=prng) # Angle dispersion of 22.5 degrees
>>> a_rad
array([-0.38842705, -0.14442948, 0.50576707, 0.07617358])
>>> degrees(a_rad) # Show angle deltas in degrees
array([-22.25523038, -8.27519966, 28.97831838, 4.36442443])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_clusters |
int
|
Number of clusters. |
required |
angle_disp |
float
|
Angle dispersion, in radians. |
required |
rng |
Generator
|
Optional pseudo-random number generator. |
_default_rng
|
Returns:
Type | Description |
---|---|
NDArray
|
Angles between the average cluster direction and the cluster-supporting lines, given in radians in the interval \(\left[-\pi/2,\pi/2\right]\). |
Source code in pyclugen/module.py
clucenters ¶
clucenters(
num_clusters: int,
clu_sep: NDArray,
clu_offset: NDArray,
rng: Generator = _default_rng,
) -> NDArray
Determine cluster centers using the uniform distribution.
The number of clusters (num_clusters
) and the average cluster separation
(clu_sep
) are taken into account.
More specifically, let \(c=\)num_clusters
, \(\mathbf{s}=\)clu_sep.reshape(-1,1)
,
\(\mathbf{o}=\)clu_offset.reshape(-1,1)
, \(n=\)clu_sep.size
(i.e., number of
dimensions). Cluster centers are obtained according to the following equation:
where \(\mathbf{C}\) is the \(c \times n\) matrix of cluster centers, \(\mathbf{U}\) is an \(c \times n\) matrix of random values drawn from the uniform distribution between -0.5 and 0.5, and \(\mathbf{1}\) is an \(c \times 1\) vector with all entries equal to 1.
Examples:
>>> from pyclugen import clucenters
>>> from numpy import array
>>> from numpy.random import Generator, PCG64
>>> prng = Generator(PCG64(123))
>>> clucenters(3, array([30,10]), array([-50,50]), rng=prng)
array([[-33.58833231, 36.61463056],
[-75.16761145, 40.53115432],
[-79.1684689 , 59.3628352 ]])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_clusters |
int
|
Number of clusters. |
required |
clu_sep |
NDArray
|
Average cluster separation ( \(n \times 1\) vector). |
required |
clu_offset |
NDArray
|
Cluster offsets ( \(n \times 1\) vector). |
required |
rng |
Generator
|
Optional pseudo-random number generator. |
_default_rng
|
Returns:
Type | Description |
---|---|
NDArray
|
A \(c \times n\) matrix containing the cluster centers. |
Source code in pyclugen/module.py
clugen ¶
clugen(
num_dims: int,
num_clusters: int,
num_points: int,
direction: ArrayLike,
angle_disp: float,
cluster_sep: ArrayLike,
llength: float,
llength_disp: float,
lateral_disp: float,
allow_empty: bool = False,
cluster_offset: Optional[ArrayLike] = None,
proj_dist_fn: str | Callable[[float, int, Generator], NDArray] = "norm",
point_dist_fn: (
str
| Callable[
[NDArray, float, float, NDArray, NDArray, Generator], NDArray
]
) = "n-1",
clusizes_fn: (
Callable[[int, int, bool, Generator], NDArray] | ArrayLike
) = clusizes,
clucenters_fn: (
Callable[[int, NDArray, NDArray, Generator], NDArray] | ArrayLike
) = clucenters,
llengths_fn: (
Callable[[int, float, float, Generator], NDArray] | ArrayLike
) = llengths,
angle_deltas_fn: (
Callable[[int, float, Generator], NDArray] | ArrayLike
) = angle_deltas,
rng: int | Generator = _default_rng,
) -> Clusters
Generate multidimensional clusters.
Tip
This is the main function of the pyclugen package, and possibly the only function most users will need.
Examples:¶
>>> import matplotlib.pyplot as plt
>>> from pyclugen import clugen
>>> from numpy import pi
>>> out = clugen(2, 5, 10000, [1, 0.5], pi/16, [10, 40], 10, 1, 2, rng=321)
>>> out.centers # What are the cluster centers?
array([[ 20.02876212, 36.59611434],
[-15.60290734, -26.52169579],
[ 23.09775166, 91.66309916],
[ -5.76816015, 54.9775074 ],
[ -4.64224681, 78.40990876]])
>>> plt.scatter(out.points[:,0],
... out.points[:,1],
... c=out.clusters) # doctest: +SKIP
>>> plt.show() # doctest: +SKIP
Note
In the descriptions below, the terms "average" and "dispersion" refer to measures of central tendency and statistical dispersion, respectively. Their exact meaning depends on several optional arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_dims |
int
|
Number of dimensions. |
required |
num_clusters |
int
|
Number of clusters to generate. |
required |
num_points |
int
|
Total number of points to generate. |
required |
direction |
ArrayLike
|
Average direction of the cluster-supporting lines. Can be a
vector of length |
required |
angle_disp |
float
|
Angle dispersion of cluster-supporting lines (radians). |
required |
cluster_sep |
ArrayLike
|
Average cluster separation in each dimension (vector of size
|
required |
llength |
float
|
Average length of cluster-supporting lines. |
required |
llength_disp |
float
|
Length dispersion of cluster-supporting lines. |
required |
lateral_disp |
float
|
Cluster lateral dispersion, i.e., dispersion of points from their projection on the cluster-supporting line. |
required |
allow_empty |
bool
|
Allow empty clusters? |
False
|
cluster_offset |
Optional[ArrayLike]
|
Offset to add to all cluster centers (vector of size |
None
|
proj_dist_fn |
str | Callable[[float, int, Generator], NDArray]
|
Distribution of point projections along cluster-supporting lines, with three possible values:
|
'norm'
|
point_dist_fn |
str | Callable[[NDArray, float, float, NDArray, NDArray, Generator], NDArray]
|
Controls how the final points are created from their projections on the cluster-supporting lines, with three possible values:
|
'n-1'
|
clusizes_fn |
Callable[[int, int, bool, Generator], NDArray] | ArrayLike
|
Distribution of cluster sizes. By default, cluster sizes are
determined by the |
clusizes
|
clucenters_fn |
Callable[[int, NDArray, NDArray, Generator], NDArray] | ArrayLike
|
Distribution of cluster centers. By default, cluster centers
are determined by the |
clucenters
|
llengths_fn |
Callable[[int, float, float, Generator], NDArray] | ArrayLike
|
Distribution of line lengths. By default, the lengths of
cluster-supporting lines are determined by the
|
llengths
|
angle_deltas_fn |
Callable[[int, float, Generator], NDArray] | ArrayLike
|
Distribution of line angle differences with respect to
|
angle_deltas
|
rng |
int | Generator
|
The seed for the random number generator or an instance of
|
_default_rng
|
Returns:
Type | Description |
---|---|
Clusters
|
The generated clusters and associated information in the form of a
|
Source code in pyclugen/main.py
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 226 227 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 |
|
clumerge ¶
clumerge(
*data: NamedTuple | Mapping[str, ArrayLike],
fields: tuple[str, ...] = ("points", "clusters"),
clusters_field: str | None = "clusters"
) -> dict[str, NDArray]
Merges the fields (specified in fields
) of two or more data
sets.
Merges the fields (specified in fields
) of two or more data
sets (named
tuples or dictionaries). The fields to be merged need to have the same
number of columns. The corresponding merged field will contain the rows of
the fields to be merged, and will have a common supertype.
The clusters_field
parameter specifies a field containing integers that
identify the cluster to which the respective points belongs to. If
clusters_field
is specified (by default it's specified as "clusters"
),
cluster assignments in individual datasets will be updated in the merged
dataset so that clusters are considered separate. This parameter can be set
to None
, in which case no field will be considered as a special cluster
assignments field.
This function can be used to merge data sets generated with the
clugen()
function, by default merging the
points
and clusters
fields in those data sets. It also works with
arbitrary data by specifying alternative fields in the fields
parameter.
It can be used, for example, to merge third-party data with
clugen()
-generated data.
Examples:
>>> from pyclugen import clugen, clumerge
>>> data1 = clugen(2, 5, 1000, [1, 1], 0.01, [20, 20], 14, 1.2, 1.5);
>>> data2 = clugen(2, 3, 450, [0.8, -0.3], 0, [25, 21], 6, 0.4, 3.5);
>>> data3 = clugen(2, 2, 600, [0, -0.7], 0.2, [15, 10], 1, 0.1, 5.2);
>>> data_merged = clumerge(data1, data2, data3)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*data |
NamedTuple | Mapping[str, ArrayLike]
|
One or more cluster data sets whose |
()
|
fields |
tuple[str, ...]
|
Fields to be merged, which must exist in the data set given in
|
('points', 'clusters')
|
clusters_field |
str | None
|
Field containing the integer cluster labels. If specified, cluster assignments in individual datasets will be updated in the merged dataset so that clusters are considered separate. |
'clusters'
|
Returns:
Type | Description |
---|---|
dict[str, NDArray]
|
A dictionary, where keys correspond to field names, and values to the merged numerical arrays. |
Source code in pyclugen/main.py
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 |
|
clupoints_n ¶
clupoints_n(
projs: NDArray,
lat_disp: float,
line_len: float,
clu_dir: NDArray,
clu_ctr: NDArray,
rng: Generator = _default_rng,
) -> NDArray
Generate points from their \(n\)-D projections on a cluster-supporting line.
Each point is placed around its projection using the normal distribution
( \(\mu=0\), \(σ=\)lat_disp
).
This function's main intended use is by the clugen()
function, generating the final points when the point_dist_fn
parameter is
set to "n"
.
Examples:
>>> from pyclugen import clupoints_n, points_on_line
>>> from numpy import array, linspace
>>> from numpy.random import Generator, PCG64
>>> prng = Generator(PCG64(123))
>>> projs = points_on_line(array([5,5]), # Get 5 point projections
... array([1,0]), # on a 2D line
... linspace(-4,4,5))
>>> projs
array([[1., 5.],
[3., 5.],
[5., 5.],
[7., 5.],
[9., 5.]])
>>> clupoints_n(projs, 0.5, 1.0, array([1,0]), array([0,0]), rng=prng)
array([[0.50543932, 4.81610667],
[3.64396263, 5.09698721],
[5.46011545, 5.2885519 ],
[6.68176818, 5.27097611],
[8.84170227, 4.83880544]])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
projs |
NDArray
|
Point projections on the cluster-supporting line ( \(p \times n\) matrix). |
required |
lat_disp |
float
|
Standard deviation for the normal distribution, i.e., cluster lateral dispersion. |
required |
line_len |
float
|
Length of cluster-supporting line (ignored). |
required |
clu_dir |
NDArray
|
Direction of the cluster-supporting line. |
required |
clu_ctr |
NDArray
|
Center position of the cluster-supporting line (ignored). |
required |
rng |
Generator
|
Optional pseudo-random number generator. |
_default_rng
|
Returns:
Type | Description |
---|---|
NDArray
|
Generated points ( \(p \times n\) matrix). |
Source code in pyclugen/module.py
clupoints_n_1 ¶
clupoints_n_1(
projs: NDArray,
lat_disp: float,
line_len: float,
clu_dir: NDArray,
clu_ctr: NDArray,
rng: Generator = _default_rng,
) -> NDArray
Generate points from their \(n\)-D projections on a cluster-supporting line.
Each point is placed on a hyperplane orthogonal to that line and centered at
the point's projection, using the normal distribution ( \(\mu=0\),
\(σ=\)lat_disp
).
This function's main intended use is by the clugen()
function, generating the final points when the point_dist_fn
parameter is
set to "n-1"
.
Examples:
>>> from pyclugen import clupoints_n_1, points_on_line
>>> from numpy import array, linspace
>>> from numpy.random import Generator, PCG64
>>> prng = Generator(PCG64(123))
>>> projs = points_on_line(array([5,5]), # Get 5 point projections
... array([1,0]), # on a 2D line
... linspace(-4,4,5))
>>> projs
array([[1., 5.],
[3., 5.],
[5., 5.],
[7., 5.],
[9., 5.]])
>>> clupoints_n_1(projs, 0.5, 1.0, array([1,0]), array([0,0]), rng=prng)
array([[1. , 5.49456068],
[3. , 5.18389333],
[5. , 5.64396263],
[7. , 5.09698721],
[9. , 5.46011545]])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
projs |
NDArray
|
Point projections on the cluster-supporting line ( \(p \times n\) matrix). |
required |
lat_disp |
float
|
Standard deviation for the normal distribution, i.e., cluster lateral dispersion. |
required |
line_len |
float
|
Length of cluster-supporting line (ignored). |
required |
clu_dir |
NDArray
|
Direction of the cluster-supporting line. |
required |
clu_ctr |
NDArray
|
Center position of the cluster-supporting line (ignored). |
required |
rng |
Generator
|
Optional pseudo-random number generator. |
_default_rng
|
Returns:
Type | Description |
---|---|
NDArray
|
Generated points ( \(p \times n\) matrix). |
Source code in pyclugen/module.py
clupoints_n_1_template ¶
clupoints_n_1_template(
projs: NDArray,
lat_disp: float,
clu_dir: NDArray,
dist_fn: Callable[[int, float, Generator], NDArray],
rng: Generator = _default_rng,
) -> NDArray
Create \(p\) points from their \(n\)-D projections on a cluster-supporting line.
Each point is placed on a hyperplane orthogonal to that line and centered at
the point's projection. The function specified in dist_fn
is used to perform
the actual placement.
This function is used internally by
clupoints_n_1()
and may be useful for
constructing user-defined final point placement strategies for the point_dist_fn
parameter of the main clugen()
function.
Examples:
>>> from numpy import array, zeros
>>> from numpy.random import Generator, PCG64
>>> from pyclugen import clupoints_n_1_template, points_on_line
>>> ctr = zeros(2)
>>> dir = array([1, 0])
>>> pdist = array([-0.5, -0.2, 0.1, 0.3])
>>> rng = Generator(PCG64(123))
>>> proj = points_on_line(ctr, dir, pdist)
>>> clupoints_n_1_template(proj, 0, dir, lambda p, l, r: r.random(p), rng=rng)
array([[-0.5 , 0.68235186],
[-0.2 , -0.05382102],
[ 0.1 , 0.22035987],
[ 0.3 , -0.18437181]])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
projs |
NDArray
|
Point projections on the cluster-supporting line ( \(p \times n\) matrix). |
required |
lat_disp |
float
|
Dispersion of points from their projection. |
required |
clu_dir |
NDArray
|
Direction of the cluster-supporting line (unit vector). |
required |
dist_fn |
Callable[[int, float, Generator], NDArray]
|
Function to place points on a second line, orthogonal to the first.
The functions accepts as parameters the number of points in the current
cluster, the |
required |
rng |
Generator
|
An optional pseudo-random number generator for reproducible executions. |
_default_rng
|
Returns:
Type | Description |
---|---|
NDArray
|
Generated points ( \(p \times n\) matrix). |
Source code in pyclugen/helper.py
clusizes ¶
clusizes(
num_clusters: int,
num_points: int,
allow_empty: bool,
rng: Generator = _default_rng,
) -> NDArray
Determine cluster sizes, i.e., the number of points in each cluster.
Cluster sizes are determined using the normal distribution (
\(\mu=\)num_points
\(/\)num_clusters
, \(\sigma=\mu/3\)), and then
assuring that the final cluster sizes add up to num_points
via the
fix_num_points()
function.
Examples:
>>> from numpy.random import Generator, PCG64
>>> from pyclugen import clusizes
>>> prng = Generator(PCG64(123))
>>> sizes = clusizes(4, 1000, True, rng=prng)
>>> sizes
array([166, 217, 354, 263])
>>> int(sum(sizes))
1000
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_clusters |
int
|
Number of clusters. |
required |
num_points |
int
|
Total number of points. |
required |
allow_empty |
bool
|
Allow empty clusters? |
required |
rng |
Generator
|
Optional pseudo-random number generator. |
_default_rng
|
Returns:
Type | Description |
---|---|
NDArray
|
Number of points in each cluster (vector of size |
Source code in pyclugen/module.py
fix_empty ¶
Certifies that, given enough points, no clusters are left empty.
This is done by removing a point from the largest cluster and adding it to an
empty cluster while there are empty clusters. If the total number of points is
smaller than the number of clusters (or if the allow_empty
parameter is set
to true
), this function does nothing.
This function is used internally by clusizes()
and might be useful for custom cluster sizing implementations given as the
clusizes_fn
parameter of the main clugen()
function.
Note that the array is changed in-place.
Examples:
>>> from numpy import array
>>> from pyclugen import fix_empty
>>> clusters = array([3, 4, 5, 0, 0])
>>> fix_empty(clusters)
array([3, 3, 4, 1, 1])
>>> clusters # Verify that the array was changed in-place
array([3, 3, 4, 1, 1])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clu_num_points |
NDArray
|
Number of points in each cluster (vector of size \(c\)), where \(c\) is the number of clusters. |
required |
allow_empty |
bool
|
Allow empty clusters? |
False
|
Returns:
Type | Description |
---|---|
NDArray
|
Number of points in each cluster, after being fixed by this function (vector
of size \(c\), which is the same reference than |
Source code in pyclugen/helper.py
fix_num_points ¶
Certifies that the values in the clu_num_points
array add up to num_points
.
If this is not the case, the clu_num_points
array is modified in-place,
incrementing the value corresponding to the smallest cluster while
sum(clu_num_points) < num_points
, or decrementing the value corresponding to
the largest cluster while sum(clu_num_points) > num_points
.
This function is used internally by clusizes()
and might be useful for custom cluster sizing implementations given as the
clusizes_fn
parameter of the main clugen()
function.
Examples:
>>> from numpy import array
>>> from pyclugen import fix_num_points
>>> clusters = array([1, 6, 3]) # 10 total points
>>> fix_num_points(clusters, 12) # But we want 12 total points
array([3, 6, 3])
>>> clusters # Verify that the array was changed in-place
array([3, 6, 3])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clu_num_points |
NDArray
|
Number of points in each cluster (vector of size \(c\)), where \(c\) is the number of clusters. |
required |
num_points |
int
|
The expected total number of points. |
required |
Returns:
Type | Description |
---|---|
NDArray
|
Number of points in each cluster, after being fixed by this function (vector
of size \(c\), which is the same reference than |
Source code in pyclugen/helper.py
llengths ¶
llengths(
num_clusters: int,
llength: float,
llength_disp: float,
rng: Generator = _default_rng,
) -> NDArray
Determine length of cluster-supporting lines.
Line lengths are determined using the folded normal distribution (
\(\mu=\)llength
, \(\sigma=\)llength_disp
).
Examples:
>>> from numpy.random import Generator, MT19937
>>> from pyclugen import llengths
>>> prng = Generator(MT19937(123))
>>> llengths(4, 20, 3.5, rng=prng)
array([19.50968733, 19.92482858, 25.99013804, 18.58029672])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_clusters |
int
|
Number of clusters. |
required |
llength |
float
|
Average line length. |
required |
llength_disp |
float
|
Line length dispersion. |
required |
rng |
Generator
|
Optional pseudo-random number generator. |
_default_rng
|
Returns:
Type | Description |
---|---|
NDArray
|
Lengths of cluster-supporting lines (vector of size |
Source code in pyclugen/module.py
points_on_line ¶
Determine coordinates of points on a line.
Determine coordinates of points on a line with center
and direction
,
based on the distances from the center given in dist_center
.
This works by using the vector formulation of the line equation assuming
direction
is a \(n\)-dimensional unit vector. In other words, considering
\(\mathbf{d}=\)direction.reshape(-1,1)
( \(n \times 1\) vector),
\(\mathbf{c}=\)center.reshape(-1,1)
( \(n \times 1\) vector), and
\(\mathbf{w}=\) dist_center.reshape(-1,1)
( \(p \times 1\) vector),
the coordinates of points on the line are given by:
where \(\mathbf{P}\) is the \(p \times n\) matrix of point coordinates on the line, and \(\mathbf{1}\) is a \(p \times 1\) vector with all entries equal to 1.
Examples:
>>> from pyclugen import points_on_line
>>> from numpy import array, linspace
>>> points_on_line(array([5., 5.]),
... array([1., 0.]),
... linspace(-4, 4, 5)) # 2D, 5 points
array([[1., 5.],
[3., 5.],
[5., 5.],
[7., 5.],
[9., 5.]])
>>> points_on_line(array([-2, 0, 0., 2]),
... array([0., 0, -1, 0]),
... array([10, -10])) # 4D, 2 points
array([[ -2., 0., -10., 2.],
[ -2., 0., 10., 2.]])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
center |
NDArray
|
Center of the line ( \(n\)-component vector). |
required |
direction |
NDArray
|
Line direction ( \(n\)-component unit vector). |
required |
dist_center |
NDArray
|
Distance of each point to the center of the line ( \(p\)-component vector, where \(p\) is the number of points). |
required |
Returns:
Type | Description |
---|---|
NDArray
|
Coordinates of points on the specified line ( \(p \times n\) matrix). |
Source code in pyclugen/core.py
rand_ortho_vector ¶
Get a random unit vector orthogonal to u
.
Note that u
is expected to be a unit vector itself.
Examples:
>>> from pyclugen import rand_ortho_vector
>>> from numpy import isclose, dot
>>> from numpy.linalg import norm
>>> from numpy.random import Generator, PCG64
>>> rng = Generator(PCG64(123))
>>> r = rng.random(3) # Get a random vector with 3 components (3D)
>>> r = r / norm(r) # Normalize it
>>> r_ort = rand_ortho_vector(r, rng=rng) # Get random unit vector orth. to r
>>> r_ort
array([-0.1982903 , -0.61401512, 0.76398062])
>>> bool(isclose(dot(r, r_ort), 0)) # Check that vectors are orthogonal
True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
u |
NDArray
|
Unit vector with \(n\) components. |
required |
rng |
Generator
|
Optional pseudo-random number generator. |
_default_rng
|
Returns:
Type | Description |
---|---|
NDArray
|
A random unit vector with \(n\) components orthogonal to |
Source code in pyclugen/core.py
rand_unit_vector ¶
Get a random unit vector with num_dims
components.
Examples:
>>> from pyclugen import rand_unit_vector
>>> rand_unit_vector(4)
array([ 0.48653889, 0.50753862, 0.05711487, -0.70881757])
>>> from pyclugen import rand_unit_vector
>>> from numpy.random import Generator, PCG64
>>> rng = Generator(PCG64(123))
>>> rand_unit_vector(2, rng=rng) # Reproducible
array([ 0.3783202 , -0.92567479])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_dims |
int
|
Number of components in vector (i.e. vector size). |
required |
rng |
Generator
|
Optional pseudo-random number generator. |
_default_rng
|
Returns:
Type | Description |
---|---|
NDArray
|
A random unit vector with |
Source code in pyclugen/core.py
rand_vector_at_angle ¶
Get a random unit vector which is at angle
radians of vector u
.
Note that u
is expected to be a unit vector itself.
Examples:
>>> from pyclugen import rand_vector_at_angle
>>> from numpy import arccos, array, degrees, pi, dot
>>> from numpy.linalg import norm
>>> from numpy.random import Generator, PCG64
>>> rng = Generator(PCG64(123))
>>> u = array([ 1.0, 0, 0.5, -0.5 ]) # Define a 4D vector
>>> u = u / norm(u) # Normalize the vector
>>> v = rand_vector_at_angle(u, pi/4, rng=rng) # Get a vector at 45 degrees
>>> v
array([ 0.633066 , -0.50953554, -0.10693823, -0.57285705])
>>> float(degrees(arccos(dot(u, v) / norm(u) * norm(v)))) # u-v angle
45.0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
u |
NDArray
|
Unit vector with \(n\) components. |
required |
angle |
float
|
Angle in radians. |
required |
rng |
Generator
|
Optional pseudo-random number generator. |
_default_rng
|
Returns:
Type | Description |
---|---|
NDArray
|
Random unit vector with \(n\) components which is at |