The simple.models Namespace
The models submodule contains everything pertaining the primary object structure which keeps track of different models.
simple.models.AllModelClasses
module-attribute
AllModelClasses = {}
A dictionary containing all the avaliable model classes.
When a new model class is created subclassing ModelBase 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)
Initialise the dictionary and track attribute types.
Source code in simple/models.py
28 29 30 31 | |
simple.models.IsoRef
IsoRef(name, reference_models_=None, **hdf5_attrs)
Bases: ModelBase
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_valuesanddata_keysattributes. -
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
Create a model instance and populate hdf5_attrs.
Source code in simple/models.py
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 | |
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.ModelBase
ModelBase(name, reference_models_=None, **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_normalisationandstandard_normalisationmethods for more customisation.VALUES_KEYS_TO_ARRAY- IfTruea key array named<name>is automatically created upon model initialisation if attributes called.<name>_valuesand<name>_keysexits.
Create a model instance and populate hdf5_attrs.
Source code in simple/models.py
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 | |
ABUNDANCE_KEYARRAY
class-attribute
instance-attribute
ABUNDANCE_KEYARRAY = None
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
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. IfNonethe unit is assumed to be that of thedesired_unit_args. -
desired_unit–The unit the array should be converted to. If
Noneno conversion is made and the original array`is returned. -
attrname–The name of the attribute storing the array. Used for logging purposes.
Raises:
-
ValueError–If the array cannot be converted from
unittodesired_unit_args.
Source code in simple/models.py
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 | |
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
Nonethe 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
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 | |
get_array_labels
get_array_labels(name=None, latex=True)
Return default labels for a key array.
Source code in simple/models.py
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 | |
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
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 | |
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
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 | |
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
Trueand 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
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 | |
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
Trueif the attribute should be included when the model is saved as a hdf5 file.valuewill 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 ifnameexists andoverwriteisFalse.
Source code in simple/models.py
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 | |
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
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 | |
simple.models.ModelCollection
ModelCollection()
The main interface for working with a collection of models.
Create an empty model collection.
Source code in simple/models.py
201 202 203 204 | |
models
instance-attribute
models = []
refs
instance-attribute
refs = []
add_model
add_model(model)
Add model to the collection if it is not already present.
Source code in simple/models.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | |
add_ref
add_ref(ref)
Add reference ref to the collection if it is not already present.
Source code in simple/models.py
451 452 453 454 455 456 457 458 459 460 461 462 463 | |
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 model is returned instead.
Source code in simple/models.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
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 model is returned instead.
Source code in simple/models.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
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
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | |
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
Noneno subselection is made. -
convert_unit–Whether to convert units to the mole unit, as recommended, when creating the isolist.
-
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
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 | |
new_model
new_model(clsname, name, **attrs)
Create a new model and add it to the current collection.
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
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
new_ref
new_ref(clsname, name, **attrs)
Create a new reference model and add it to the current collection.
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 reference model.
Source code in simple/models.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
save
save(filename)
Save the current selection of models.
Parameters:
-
filename–Name of the file to be created.
Source code in simple/models.py
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 | |
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
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
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
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | |
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.
Source code in simple/models.py
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | |
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 _func models file
-
default_isolist–Isolist applied to loaded models from
dbfilename. -
convert_units(bool) –If
Trueand 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
Truea new file will be created even iffilenamealready 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
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 | |
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
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 | |
simple.models.load_models
load_models(*args, **kwargs)
Source code in simple/models.py
164 165 166 | |
simple.models.load_ppn
load_ppn(filename)
Load a key array from a ppn file.
Source code in simple/models.py
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 | |
simple.models.new_collection
new_collection()
Return an empty ModelCollection object.
Source code in simple/models.py
168 169 170 171 172 | |