httk.core package¶
Submodules¶
httk.core.basic module¶
Basic help functions

httk.core.basic.
micro_pyawk
(ioa, search, results=None, debug=False, debugfunc=None, postdebugfunc=None)[source]¶ Small awkmimicking search routine.
‘f’ is stream object to search through. ‘search’ is the “search program”, a list of lists/tuples with 3 elements; i.e., [[regex,test,run],[regex,test,run],...] ‘results’ is a an object that your search program will have access to for storing results.
Here regex is either as a Regex object, or a string that we compile into a Regex. test and run are callable objects.
This function goes through each line in filename, and if regex matches that line and test(results,line)==True (or test == None) we execute run(results,match), where match is the match object from running Regex.match.
The default results is an empty dictionary. Passing a results object let you interact with it in run() and test(). Hence, in many occasions it is thus clever to use results=self.
Returns: results
httk.core.citation module¶
Keep track of citation information for different parts of httk, so that this info can be printed out on program exit. Turn on either explicitly by calling httk.config.print_citations_at_exit() from your program, or implicitly for all software using httk by setting ‘auto_print_citations_at_exit=yes’ in httk.cfg
Right now this is mostly a proof of concept code, and was added in response to a concern that coauthors of the software would not get credit. We should extend this to add a facility to make it easier to track and acknowledge citations also of the data being used.
httk.core.code module¶

class
httk.core.code.
Code
(name, version)[source]¶ Bases:
httk.core.httkobject.HttkObject
Object for keeping track of httk data about a computer software or script
httk.core.computation module¶

class
httk.core.computation.
Computation
(computation_date, description, code, manifest_hash, signatures, keys, relpath, project_counter, added_date=None)[source]¶ Bases:
httk.core.httkobject.HttkObject
Object for keeping track of httk data about a specific computation run

added_date
¶

classmethod
create
(computation_date, description, code, manifest_hash, signatures, keys, project_counter, relpath, added_date=None)[source]¶ Create a Computation object.


class
httk.core.computation.
ComputationRelated
(main_computation, other_computation, relation)[source]¶ Bases:
httk.core.httkobject.HttkObject
Object for keeping track of httk data about a specific computation run

class
httk.core.computation.
Result
(computation)[source]¶ Bases:
httk.core.httkobject.HttkObject
Intended as a base class for results tables for computations
httk.core.crypto module¶
Provides a few central and very helpful functions for cryptographic hashes, etc.

httk.core.crypto.
generate_keys
(public_key_path, secret_key_path)[source]¶ Generates a public and a private key pair and stores them in respective files
httk.core.ed25519 module¶
httk.core.fracvector module¶

class
httk.core.fracvector.
FracScalar
(nom, denom)[source]¶ Bases:
httk.core.fracvector.FracVector
Represents the fractional number nom/denom. This is a subclass of FracVector with the purpose of making it clear when a scalar fracvector is needed/used.

class
httk.core.fracvector.
FracVector
(noms, denom=1)[source]¶ Bases:
object
FracVector is a general immutable Ndimensional vector (tensor) class for performing linear algebra with fractional numbers.
A FracVector consists of a multidimensional tuple of integer nominators, and a single shared integer denominator.
Since FracVectors are immutable, every operation on a FracVector returns a new FracVector with the result of the operation. A created FracVector never changes. Hence, they are safe to use as keys in dictionaries, to use in sets, etc.
Note: most methods returns FracVector results that are not simplified (i.e., the FracVector returned does not have the smallest possible integer denominator). To return a FracVector with the smallest possible denominator, just call FracVector.simplify() at the last step.

ceil
()[source]¶ Returns the integer that is equal to or just below the value stored in a scalar FracVector.

classmethod
chain_vecs
(vecs)[source]¶ Optimized chaining of FracVectors.
vecs: a list (or tuple) of fracvectors.
 Returns the same thing as
 FracVector.create(vecs,chain=True)
 i.e., removes outermost dimension and chain the subsequences. If input=[[1 2 3],[4,5,6]], then
 FracVector.chain(input) > [1,2,3,4,5,6]
but this method assumes all vectors share the same denominator (it raises an exception if this is not true)

classmethod
create
(noms, denom=None, simplify=True, chain=False)[source]¶ Create a FracVector from various types of sequences.
Simplest use:
FracVector.create(some_kind_of_sequence)
where ‘some_kind_of_sequence’ can be any nested list or tuple of objects that can be used in the constructor of the Python Fraction class (also works with strings!). If any object found while traveling the items has a .to_fractions() method, it will be called and is expected to return a fraction or list or tuple of fractions.
Optional parameters:
 Invocation with denominator: FracVector.create(nominators,denominator) nominators is any sequence, and denominator a common denominator to divide all nominators with
 simplify: boolean, return a FracVector with the smallest possible denominator.
 chain: boolean, remove outermost dimension and chain the subsequences. I.e., if input=[[1 2 3],[4,5,6]], then FracVector.create(input) > [1,2,3,4,5,6]
Relevant: FracVector itself implements .to_fractions(), and hence, the same constructor allows stacking several FracVector objects like this:
vertical_fracvector = FracVector([[fracvector1],[fracvector2]]) horizontal_fracvector = FracVector([fracvector1,fracvector2],chain=True)

cross
(other)[source]¶ Returns the vector cross product of the 3element 1D vector with the 3element 1D vector ‘other’, i.e., A x B.

dim
¶ This property returns a tuple with the dimensionality of each dimension of the FracVector (the noms are assumed to be a nested list of rectangular shape).

dot
(other)[source]¶ Returns the vector dot product of the 1D vector with the 1D vector ‘other’, i.e., A . B or A cdot B. The same as A * B.T().

floor
()[source]¶ Returns the integer that is equal to or just below the value stored in a scalar FracVector.

classmethod
from_floats
(l, resolution=4294967296)[source]¶ Create a FracVector from a (nested) list or tuple of floats. You can convert a numpy array with this method if you use A.tolist()
resolution: the resolution used for interpreting the given floating point numbers. Default is 2^32.

classmethod
from_tuple
(t)[source]¶ Return a FracVector created from the tuple representation: (denom, ...noms...), returned by the to_tuple() method.

limit_denominator
(max_denom=1000000000)[source]¶ Returns a FracVector of reduced resolution.
resolution: each element in the returned FracVector is the closest numerical approximation that can is allowed by a fraction with maximally this denominator. Note: since all elements must be put on a common denominator, the result may have a larger denominator than max_denom

max
()[source]¶ Return the maximum element across all dimensions in the FracVector. max(fracvector) works for a 1D vector.

metric_product
(vecA, vecB)[source]¶  Returns the result of the metric product using the present square FracVector as the metric matrix. The same as
 vecA*self*vecB.T().

min
()[source]¶ Return the minimum element across all dimensions in the FracVector. max(fracvector) works for a 1D vector.

mul
(other)[source]¶ Returns the result of multiplying the vector with ‘other’ using matrix multiplication.
Note that for two 1D FracVectors, A.dot(B) is not the same as A.mul(B), but rather: A.mul(B.T()).

nargmax
()[source]¶ Return a list of indices of all maximum elements across all dimensions in the FracVector.

nargmin
()[source]¶ Return a list of indices for all minimum elements across all dimensions in the FracVector.

static
nested_map
(op, *ls)¶ Map an operator over a nested tuple. (i.e., the same as the builtin map(), but works recursively on a nested tuple)

static
nested_map_fractions
(op, *ls)¶ Map an operator over a nested tuple, but checks every element for a method to_fractions() and uses this to further convert objects into tuples of Fraction.

nom
¶ Returns the integer nominator of a scalar FracVector.

normalize_half
()[source]¶ Add/remove an integer +/N to each element to place it in the range [1/2,1/2)
 This is useful to find the shortest vector C between two points A, B in a space with periodic boundary conditions [0,1):
 C = (AB).normalize_half()

classmethod
random
(dims, minnom=100, maxnom=100, denom=100)[source]¶ Create a zero matrix with the given dimensions

classmethod
set_common_denom
(A, B)[source]¶ Used internally to combine two different FracVectors.
Returns a tuple (A2,B2,denom) where A2 is numerically equal to A, and B2 is numerically equal to B, but A2 and B2 are both set on the same shared denominator ‘denom’ which is the product of the denominator of A and B.

set_denominator
(set_denom=1000000000)[source]¶ Returns a FracVector of reduced resolution where every element is the closest numerical approximation using this denominator.

simplify
()[source]¶ Returns a reduced FracVector. I.e., each element has the same numerical value but the new FracVector represents them using the smallest possible shared denominator.


httk.core.fracvector.
nested_map_fractions_list
(op, *ls)[source]¶ Map an operator over a nested list, but checks every element for a method to_fractions() and uses this to further convert objects into lists of Fraction.

httk.core.fracvector.
nested_map_fractions_tuple
(op, *ls)[source]¶ Map an operator over a nested tuple, but checks every element for a method to_fractions() and uses this to further convert objects into tuples of Fraction.

httk.core.fracvector.
nested_map_list
(op, *ls)[source]¶ Map an operator over a nested list. (i.e., the same as the builtin map(), but works recursively on a nested list)

httk.core.fracvector.
nested_map_tuple
(op, *ls)[source]¶ Map an operator over a nested tuple. (i.e., the same as the builtin map(), but works recursively on a nested tuple)

httk.core.fracvector.
nested_reduce
(op, l, initializer=None)[source]¶ Same as builtin reduce, but operates on a nested tuple/list/sequence.

httk.core.fracvector.
nested_reduce_fractions
(op, l, initializer=None)[source]¶ Same as builtin reduce, but operates on a nested tuple/list/sequence. Also checks every element for a method to_fractions() and uses this to further convert such elements to lists of fractions.

httk.core.fracvector.
nested_reduce_levels
(op, l, level=1, initializer=None)[source]¶ Same as builtin reduce, but operates on a nested tuple/list/sequence.

httk.core.fracvector.
tuple_eye
(dims, onepos=0)[source]¶ Create a matrix with the given dimensions and 1 on the diagonal

httk.core.fracvector.
tuple_index
(dims, uppidx=())[source]¶ Create a nested tuple where every element is a tuple indicating the position of that tuple

httk.core.fracvector.
tuple_random
(dims, minval, maxval)[source]¶ Create a nested tuple with the given dimensions filled with random numbers between minval and maxval
httk.core.geometry module¶
Basic geometry helper functions

httk.core.geometry.
hull_z
(points, zs)[source]¶ points: a list of points=(x,y,..) with zs= a list of z values; a convex halfhull is constructed over negative zvalues
returns data on the following format.:
{ 'hull_points': indices in points list for points that make up the convex hull, 'interior_points':indices for points in the interior, 'interior_zs':interior_zs 'zs_on_hull': hull z values for each point (for points on the hull, the value of the hull if this point is excluded) 'closest_points': list of best linear combination of other points for each point 'closest_weights': weights of best linear combination of other points for each point }
where hull_points and interior_points are lists of the points on the hull and inside the hull. and
hull_zs is a list of zvalues that the hull would have at that point, had this point not been included. interior_zs is a list of zvalues that the hull has at the interior points.

httk.core.geometry.
is_any_part_of_cube_inside_cell
(cell, midpoint, side)[source]¶ Checks if any part of a cube is inside the cell spanned by the vectors in cell

httk.core.geometry.
is_point_inside_cell
(cell, point)[source]¶ Checks if a given triplevector is inside the cell given by the basis matrix in cell

httk.core.geometry.
is_point_inside_tetra
(tetra, point)[source]¶ Checks if a point is inside the tretrahedra spanned by the coordinates in tetra

httk.core.geometry.
simplex_le_solver
(a, b, c)[source]¶ Minimizie func = a[0]*x + a[1]*y + a[2]*z + ... With constraints:
b[0,0]x + b[0,1]y + b[0,2]z + ... <= c[0] b[1,0]x + b[1,1]y + b[1,2]z + ... <= c[1] ... x,y,z, ... >= 0
Algorithm adapted from ‘taw9’, http://taw9.hubpages.com/hub/SimplexAlgorithminPython
httk.core.httkobject module¶
httk.core.ioadapters module¶

class
httk.core.ioadapters.
IoAdapterFileAppender
(f, name=None)[source]¶ Bases:
object
Io adapter for access to data as a python file object

class
httk.core.ioadapters.
IoAdapterFileReader
(f, name=None, deletefilename=None, close=False)[source]¶ Bases:
object
Io adapter for easy handling of io.

class
httk.core.ioadapters.
IoAdapterFileWriter
(f, name=None, close=False)[source]¶ Bases:
object
Io adapter for access to data as a python file object

class
httk.core.ioadapters.
IoAdapterFilename
(filename, name=None, deletefilename=None)[source]¶ Bases:
object
Universal io adapter, helps handling the passing of filenames, files, and strings to functions that deal with io

class
httk.core.ioadapters.
IoAdapterString
(string=None, name=None)[source]¶ Bases:
object
Universal io adapter, helps handling the passing of filenames, files, and strings to functions that deal with io

string
¶

httk.core.mutablefracvector module¶

class
httk.core.mutablefracvector.
MutableFracVector
(noms, denom)[source]¶ Bases:
httk.core.fracvector.FracVector
Same as FracVector, only, this version allow assignment of elements, e.g.,
mfracvec[2,7] = 5
and, e.g.,
mfracvec[:,7] = [1,2,3,4]
Other than this, the FracVector methods exist and do the same, i.e., they return copies of the fracvector, rather than modifying it.
However, methods have also been added named with set_* prefixes which performs mutating operations, e.g.,
A.set_T()
replaces A with its own transpose, whereas
A.T()
just returns a new MutableFracVector that is the transpose of A, leaving A unmodified.

invalidate
()[source]¶ Internal method to call when MutableFracVector is changed in such a way that cached properties are invalidated (e.g., _dim)

static
nested_inmap
(op, *ls)¶ Like inmap, but work for nested lists

static
nested_map
(op, *ls)¶ Map an operator over a nested list. (i.e., the same as the builtin map(), but works recursively on a nested list)

static
nested_map_fractions
(op, *ls)¶ Map an operator over a nested list, but checks every element for a method to_fractions() and uses this to further convert objects into lists of Fraction.

set_normalize_half
()[source]¶ Add/remove an integer +/N to each element to place it in the range [1/2,1/2)
 This is useful to find the shortest vector C between two points A, B in a space with periodic boundary conditions [0,1):
 C = (AB).normalize_half()

set_set_denominator
(resolution=1000000000)[source]¶ Changes MutableFracVector; reduces resolution.
resolution is the new denominator, each element becomes the closest numerical approximation using this denominator.


httk.core.mutablefracvector.
inmap
(f, x)[source]¶ Like builtin map, but work on a list and replace the elements in the list with the result of the mapping.

httk.core.mutablefracvector.
list_set_slice
(l, key, values)[source]¶  Given:
 l = list, key = python slice (i.e., what you get to __setitem__ when you write A[3:2]=[2,5]) values = a list of values,
change the elements specified by the slice in key to those given by values.
httk.core.project module¶

class
httk.core.project.
Project
(name, description, project_key, keys)[source]¶ Bases:
httk.core.httkobject.HttkObject
httk.core.reference module¶

class
httk.core.reference.
Author
(last_name, given_names)[source]¶ Bases:
httk.core.httkobject.HttkObject
Object for keeping track of tags for other objects

class
httk.core.reference.
Reference
(ref, authors=None, authorsstr=None, journal=None, volume=None, firstpage=None, lastpage=None, year=None, publisher=None, publisher_extra=None)[source]¶ Bases:
httk.core.httkobject.HttkObject
A reference citation
httk.core.signature module¶
httk.core.template module¶

httk.core.template.
apply_template
(template, output, envglobals=None, envlocals=None)[source]¶ Simple Python template engine.
The file ‘template’ is turned into a new file ‘output’ replacing the following: $name > the value of the variable ‘name’ in the scope provided by locals and globals. $(python statement) > result of evaluating the python statment. ${some python code} > text on stdout from running that python code.
Note: it is safe for the code inside the template to load the file it eventually will replace.

httk.core.template.
apply_templates
(inputpath, outpath, template_suffixes='template', envglobals=None, envlocals=None, mkdir=True)[source]¶ Apply one or a series of templates throughout directory tree.
template_suffixes: string or list of strings that are the suffixes of templates that are to be applied. name: subdirectory in which to apply the template, defaults to last subrun created, or ‘.’ if no subrun have been created.
Module contents¶
httk core module
Basic utilities and data definitions that are used throughout the httk code.
 A few of the most important components:
 fracvector: our general matrix object used to allow exact representation of arrays to allow, e.g., exact matching
 of coordinates to existing structures in the database.
ioadapters: our classes for generic handling of IO to files, streams, etc.
structure: our basic definition of a “structure of atoms”