Utility Functions
Source code in GPyEDS/utils.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
Function to transform cluster or decomposition results into a displayable image. It adds 'nan' to all pixels where no data exists.
Parameters
values : 1D ndarray List of values to be transformed into 2D ndarray. mask : 2D ndarray (bianry mask) Mask showing location of values in dataset.
Returns
new_array : 2D ndarray Transformed image showing the values passed in their respective locations.
Source code in GPyEDS/utils.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
Function to stack lists of 2D numpy arrays to a 3D stack.
Parameters
array_list : list of 2D ndarray list of 2D ndarray objects to stack into 3D ndarray
Returns
stack : 3D ndarray Stacked ndarray object.
Source code in GPyEDS/utils.py
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 | |
Opposite/reverse of list2stack function.
Parameters
stack : 3D ndarray stacked array of 2D maps to be broken up into list of 2D ndarray.
Returns
array_list : list of 2D ndarray broken up list of 2D ndarray maps.
Source code in GPyEDS/utils.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
Wrapper for the skimage implementation of the Gaussian filter function. This implementation takes into account the different phases present and limits the smoothing to each phase only - avoiding the creation of artifical 'mixels' upon smoothing. Useful for noisy datasets, but beware of drawbacks/limitations.
Parameters
conc : 2/3D ndarray or list of 2D ndarray Concentration map(s) to smoothe. mask : binary mask 2/3D ndarray or list of 2D ndarrays. Binary mask showing the positions of the phases present in the dataset. std : int, optional Standard deviation of Gaussian kernel used for smoothing. The default is 5. list_return : bool, optional If True, filtered maps are returned as a list. The default is False so result is returned as a 3D stack of ndarray.
Returns
filtered_maps: 3D ndarray stack or list of 2D ndarray Result of the smoothing operations.
Source code in GPyEDS/utils.py
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 | |
Function to perform mean normalisation on the dataset passed to it.
Input
feature (numpy array) - features to be normalised return_params (boolean, optional) - set True if parameters used for mean normalisation are to be returned for each feature
Returns
norm (numpy array) - mean normalised features params (list of numpy arrays) - only returned if set to True above; list of parameters used for the mean normalisation as derived from the features (ie. mean, min and max).
Source code in GPyEDS/utils.py
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 | |
Creates a list of masks from a label array passed to it.
Parameters
label_array : 2D numpy array Array containing phase labels assigned to each pixel. values : list of ints/labels, optional The labels to be masked from the label array. The default is None.
Returns
masks : list of 2D numpy arrays Colection of phase masks generated.
Source code in GPyEDS/utils.py
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 | |
Build 3D numpy conc_map from pandas dataframe.
Input
data (pd dataframe)
pandas dataframe to be transformed to numpy data matrix.
shape (list) (optional)
desired shape of the resulting array; if not given
one will be generated using the data and shape of dataframe.
Return
conc_map (3D numpy array) the resulting data matrix of shape either given or calculated. data_mask (2D numpy array) mask showing where data exists in conc_map (binary mask)
Source code in GPyEDS/utils.py
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 | |
Function to plot the results of the decomposition function. It may be called directly or through the clustering function itself.
Input
scores (3D numpy array) - array containing the scores assigned to every pixel with a separate image for every component. comps (2D numpy array) - list of component compositions ordered identical to score maps. plot_return (bool) - (optional) if True the matplotlib figure and axis objects are returned; default is False elements (list) - (optional) x tick labels used for plotting cluster center compositions. shape (list) - (optional) if scores matrix passed is 2D, this is the shape used to plot it on; default is to reshape first dimension as close to square as possible, keeping the second dimension unchanged (not recommended)
Return (if set True)
fig - matplotlib figure object for plot. ax - matplotlib axis object for plot.
Source code in GPyEDS/utils.py
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 | |
Function to perform the selected decomposition algorithm on the data passed. Ideal for use in the initial data exploration steps.
Input
data (either 2D or 3D numpy array) - the dataset to be decomposed. n_components (int) - number of components to be kept after decomposition; default is 2. method (str) - choose one of ["pca", "nmf"] as the algorithm to be used; default is pca. tol (float) - tolerance value to be used for NMF decomposition; default is 0.05. plot (bool) - Make True if results are to be plotted; default is false. plot_return (bool) - optional, if plot=true, make True to return fig and ax objects; default is false. elements (list/array) - optional, used when plotting results only; default is None. data_mask -
Return
scores (3D numpy array) - scores relating each element in the original dataset to the components found. Third dimension equal to n_components. components (2D numpy array [n_components, n_features]) - Components found to describe the dataset; ordered by the fraction of dataset variance described by each component. fig, ax (single matplotlib objects) - only if both plot and plot_return are set True.
Source code in GPyEDS/utils.py
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 | |