simple.models
simple.models.AllModelClasses
module-attribute
AllModelClasses = {}
A dictionary containing all the avaliable model classes.
When a new model class is created subclassing ModelTemplate
it is automatically
added to this dictionary.
simple.models.HDF5Dict
HDF5Dict(*args, **kwargs)
Bases: NamedDict
A subclass of NamedDict where all values are passed to asarray before being added to the dictionary.
All contents on this dictionary should be compatiable with HDF5 files.
Examples:
>>> nd = simple.utils.NamedDict({'a': 1, 'b': 2, 'c': 3})
>>> nd.a
array(1)
Source code in simple/utils.py
99 100 |
|
get
get(key, value, default=None)
Source code in simple/models.py
31 32 33 34 35 |
|
simple.models.IsoRef
IsoRef(collection, name, **hdf5_attrs)
Bases: ModelTemplate
Model specifically for storing reference isotope values.
Attributes:
-
type
(str
) –The type of data stored in the model. Required at initialisation
-
citation
(str
) –A citation for the data. Required at initialisation
-
data
–A key array containing the data. Is created upon model initiation from the
data_values
anddata_keys
attributes. -
data_values
–A 2dim array containing the data. Required at initialisation
-
data_keys
–Keys for the second dimension of
data_values
. Required at initialisation -
data_unit
–Unit for the data. Required at initialisation
Source code in simple/models.py
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 |
|
ISREF
class-attribute
instance-attribute
ISREF = True
REPR_ATTRS
class-attribute
instance-attribute
REPR_ATTRS = ['name', 'type']
REQUIRED_ATTRS
class-attribute
instance-attribute
REQUIRED_ATTRS = ['type', 'citation', 'data_values', 'data_keys', 'data_unit']
simple.models.ModelCollection
ModelCollection()
The main interface for working with a collection of models.
Source code in simple/models.py
120 121 122 |
|
models
instance-attribute
models = {}
refs
instance-attribute
refs = {}
copy
copy()
Returns a new collection containing a shallow copy of all the models in the current collection.
Source code in simple/models.py
344 345 346 347 348 349 350 351 352 |
|
get_model
get_model(name, attr=None)
Returns the model with the given name.
If attr
is given then the value of that attribute from the named model is returned instead.
Source code in simple/models.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
get_ref
get_ref(name, attr=None)
Returns the reference model with the given name.
If attr
is given then the value of that attribute from the named model is returned instead.
Source code in simple/models.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
internal_normalisation
internal_normalisation(normrat, *, isotopes=None, enrichment_factor=1, relative_enrichment=True, convert_unit=True, attrname='intnorm', method='largest_offset', **method_kwargs)
Internally normalise the appropriate data of the model. See internal_normalisation for a description of the procedure and a description of the arguments.
The result of the normalisation will be saved to each model under the normname
attribute.
Raises:
-
NotImplementedError
–Raised if the data to be normalised has not been specified for this model class.
Source code in simple/models.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
|
load_file
load_file(filename, isolist=None, convert_unit=True, where=None, **where_kwargs)
Add models from file to the current collection.
Note existing models with the same name one of the loaded models will be overwritten.
Parameters:
-
filename
–Name of the file to load.
-
isolist
–Isolist applied to loaded models. If
None
no subselection is made. -
where
–String evaluation used to select which models to load.
-
**where_kwargs
–Additional keyword arguments used together with
where
.
Source code in simple/models.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
new_model
new_model(clsname, name, **attrs)
Create a new model and add it to the current collection.
Note if a model already exists called name
it will be overwritten.
Parameters:
-
clsname
–The name of the model class to be created.
-
name
–Name of the new model.
-
**attrs
–Attributes to be added to the new model.
Returns:
-
–
The newly created model.
Source code in simple/models.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
save
save(filename)
Save the current selection of models.
Parameters:
-
filename
–Name of the file to be created.
Source code in simple/models.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
select_isolist
select_isolist(isolist=None)
Used to create a subselection of data from each model.
Note The original array may be overwritten with the new subselection.
Parameters:
-
isolist
–Either a list of isotopes to be selected or a dictionary consisting of the
Raises:
-
NotImplementedError
–Raised if this method has not been implemented for a model class.
Source code in simple/models.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
simple_normalisation
simple_normalisation(*args, **kwargs)
Deprecated Method. Use standard_normalisation
instead.
Source code in simple/models.py
391 392 393 394 395 396 |
|
standard_normalisation
standard_normalisation(normiso, enrichment_factor=1, relative_enrichment=True, convert_unit=True, attrname='stdnorm')
Normalise the appropriate data of the model. See [simple_normalisation][simple.norm.simple_normalisation] for a description of the procedure and a description of the arguments.
The result of the normalisation will be saved to each model under the normname
attribute.
Raises:
-
NotImplementedError
–Raised if the data to be normalised has not been specified for this model class.
Source code in simple/models.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
|
where
where(where, **where_kwargs)
Returns a copy of the collection containing only the models which match the where
argument.
Use &
or |
to combine multiple evaluations. To evaluate an attribute of each model put a
dot before the name e.g. .mass == 15
. To use one of the where_kwargs
values put the name
of the kwarg within pointy brackets e.g. .mass == {mass_kwarg}
.
The available operators for evaluations are ==
, !=
, >
, >=
, <
, <=
,
IN
, and NOT IN
.
Note that a shallow copy of the matching models is returned.
Parameters:
-
where
–A string with the evaluation to perform for each model.
-
**where_kwargs
–Arguments used for the evaluation.
Returns:
-
–
bool
Source code in simple/models.py
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 |
|
simple.models.ModelTemplate
ModelTemplate(collection, name, **hdf5_attrs)
This class can be subclassed to create new model classes.
Once subclassed the new model will automatically be available through ModelCollection.new_model
.
There are a number of class attributes that can be set to determine behaviour of the new class:
REQUIRED_ATTRS
- A list of attributes that must be supplied when creating the class. An exception will be raised if any of these attributes are missing.REPR_ATTRS
- A list of the attributes that values will be shown in the repr.ABUNDANCE_KEYARRAY
- The name of a key array containing the abundances that should be normalised. Alternatively you can subclass theinternal_normalisation
andstandard_normalisation
methods for more customisation.VALUES_KEYS_TO_ARRAY
- IfTrue
a key array named<name>
is automatically created upon model initialisation if attributes called.<name>_values
and<name>_keys
exits.ISREF
- Should beTrue
for models specifically for storing reference values. These will be stored in theModelCollection.refs
dictionary rather thanModelCollection.models
dictionary.
Source code in simple/models.py
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 |
|
ABUNDANCE_KEYARRAY
class-attribute
instance-attribute
ABUNDANCE_KEYARRAY = None
ISREF
class-attribute
instance-attribute
ISREF = False
REPR_ATTRS
class-attribute
instance-attribute
REPR_ATTRS = ['name']
REQUIRED_ATTRS
class-attribute
instance-attribute
REQUIRED_ATTRS = []
VALUES_KEYS_TO_ARRAY
class-attribute
instance-attribute
VALUES_KEYS_TO_ARRAY = True
change_name
change_name(name)
Change the name of the current model to name
.
Note if another model already exists with this name in the collection it will be replaced with this model.
Source code in simple/models.py
541 542 543 544 545 546 547 548 549 |
|
convert_array
convert_array(array, unit, desired_unit, *, attrname='')
Return a copy of the array converted to the desired unit.
Supported units are the mole and mass units. Converting between the mass and mole* units is done by dividing/multiplying the values by the mass number.
Always return a copy of array
even if not conversion takes place.
Parameters:
-
array
–A key array.
-
unit
–The current unit of the data in
array
. IfNone
the unit is assumed to be that of the -
desired_unit
–The unit the array should be converted to. If
None
no conversion is made
Raises:
-
ValueError
–If the array cannot be converted from
unit
todesired_unit_args
.
Returns:
-
–
A copy of
array
with the desired unit.
Source code in simple/models.py
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 |
|
copy_to
copy_to(collection)
Create a shallow copy of the current model in collection
.
Returns:
-
–
The new model
Source code in simple/models.py
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
|
get_array
get_array(name=None, desired_unit=None)
Returns a copy of the named array with the desired unit.
Parameters:
-
name
–Name of the array to return. If
None
the default abundance array is returned. -
desired_unit
–The desired unit of the returned array.
Returns:
-
–
A copy of the array with the desired unit.
Source code in simple/models.py
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 |
|
get_array_labels
get_array_labels(name=None, latex=True)
Source code in simple/models.py
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 |
|
get_mask
get_mask(mask, shape=None, **mask_attrs)
Returns a selection mask for an array with shape
.
This function is used by plotting functions to plot only a sub selction of the data. The mask string
can an integer representing an index, a slice or a condition that generates a mask. Use &
or |
to combine multiple indexes and/or conditions.
Supplied attributes can be accesed by putting a dot infront of the name, e.g. .data > 1
. The available
operators for mask conditions are ==
, !=
, >
, >=
, <
, <=
.
The result of the mask evaluation must be broadcastable with shape
. If it is not an all False
mask is
returned.
Note
- It is not possible to mix &
and |
seperators. Doing so will raise an exception.
- Any text not precceded by a dot will be evaluated as text. Text on its own will always be evaluated
as False
.
- An empty string will be evaluated as True
Parameters:
-
mask
–String or object that will be evaluated to create a mask.
-
shape
–Shape of the returned mask. If omitted the shape of the default abundance array is used.
-
**mask_attrs
–Attributes to be used during the evaluation.
Examples:
>>> a = np.array([0,1,2,3,4])
>>> model.get_mask('3', a.shape)
array([False, False, False, True, False])
>>> model.get_mask('1:3', a.shape)
array([False, True, True, False, False])
>>> model.get_mask('.data >= 1 & .data < 3', a.shape, data=a)
array([False, True, True, False, False])
>>> model.get_mask('.data >= 1 | .data > 3', a.shape, data=a)
rray([True, True, False, False, True])
Returns:
-
–
A boolean numpy array with
shape
.
Source code in simple/models.py
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 |
|
get_ref
get_ref(name, attr=None)
Returns the reference model from the parent collection with the given name.
If attr
is given then the value of that attribute from the named model is returned instead.
Source code in simple/models.py
533 534 535 536 537 538 539 |
|
internal_normalisation
internal_normalisation(normrat, *, isotopes=None, enrichment_factor=1, relative_enrichment=True, convert_unit=True, attrname='intnorm', method='largest_offset', **method_kwargs)
Internally normalise the appropriate data of the model. See internal_normalisation for a description of the procedure and a description of the arguments.
The result of the normalisation will be saved under the normname
attribute.
Raises:
-
NotImplementedError
–Raised if the data to be normalised has not been specified for this model class.
Source code in simple/models.py
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 |
|
select_isolist
select_isolist(isolist, convert_unit=True)
Used to create a subselection of the data used for normalisation in the current model.
Notes
The original array will be overwritten with the new subselection.
If <name>_values
and <name>_keys
exist they will also be overwritten with the
result of the new selection.
Parameters:
-
isolist
–Either a list of isotopes to be selected or a dictionary consisting of the final isotope mapped to a list of isotopes to be added together for this isotope.
-
convert_unit
–If
True
and data is stored in a mass unit all values will be divided by the mass number of the isotope before summing values together. The final value is then multiplied by the mass number of the output isotope.
Raises:
-
NotImplementedError
–Raised if the data to be normalised has not been specified for this model class.
Source code in simple/models.py
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 |
|
setattr
setattr(name, value, hdf5_compatible=False, overwrite=False)
Set the value of a model attribute.
Parameters:
-
name
–Name of the attribute
-
value
–The value of the attribute
-
hdf5_compatible
–Should be
True
if the attribute should be included when the model is saved as a hdf5 file.value
will be automatically converted to a hdf5 compatible value. An exception may be raised if this is not possible. -
overwrite
–Overwrite any existing attribute called
name
. An exception is raised ifname
exists andoverwrite
isFalse
.
Source code in simple/models.py
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 |
|
standard_normalisation
standard_normalisation(normiso, isotopes=None, *, enrichment_factor=1, relative_enrichment=True, convert_unit=True, attrname='stdnorm', dilution_factor=None)
Normalise the appropriate data of the model. See standard_normalisation for a description of the procedure and a description of the arguments.
The result of the normalisation will be saved under the normname
attribute.
Raises:
-
NotImplementedError
–Raised if the data to be normalised has not been specified for this model class.
Source code in simple/models.py
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 |
|
simple.models.load_collection
load_collection(filename, dbfilename=None, *, default_isolist=None, convert_unit=True, overwrite=False, where=None, **where_kwargs)
Loads a selection of models from a file.
If that file does not exist it will create the file from the specified models file. Only when doing this
is the default_isolist
applied. If filename
already exits the assumption is it has the correct isolist.
*Notes
The entire file will be read into memory. This might be an issue if reading very large files. The hdf5 are compressed so will be significantly larger when stored in memory.
When reading the database file to create a subselection of the data using default_isolist
, the subselection is
made when each model is loaded which reduces the amount of memory used.
Parameters:
-
filename
(str
) –Name of the file to load or create.
-
dbfilename
(str
, default:None
) –Name of the raw models file
-
default_isolist
–Isolist applied to loaded models from
dbfilename
. -
convert_units
(bool
) –If
True
and data is stored in a mass unit all values will be divided by the mass number of the isotope before summing values together. The final value is then multiplied by the mass number of the output isotope. -
overwrite
(bool
, default:False
) –If
True
a new file will be created even iffilename
already exists. -
where
(str
, default:None
) –Used to select which models to load.
-
**where_kwargs
–Keyword arguments used in combination with
where
.
Returns:
-
–
A ModelCollection object containing all the loaded models.
Source code in simple/models.py
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 |
|
simple.models.load_csv_h
load_csv_h(filename)
Returns a key array from a csv file where the first from is the columns keys and the remaining rows contain the data.
Source code in simple/models.py
910 911 912 913 914 915 916 917 918 919 |
|
simple.models.load_models
load_models(*args, **kwargs)
Source code in simple/models.py
87 88 89 |
|
simple.models.load_ppn
load_ppn(filename)
Load a key array from a ppn file.
Source code in simple/models.py
922 923 924 925 926 927 928 929 930 931 932 933 |
|
simple.models.new_collection
new_collection()
Return an empty ModelCollection object.
Source code in simple/models.py
91 92 93 94 95 |
|