AHPTree class

class pyanp.ahptree.AHPTree(root_name='Goal', alt_names=None)[source]

Represents all of the data of an ahp tree.

__init__(root_name='Goal', alt_names=None)[source]

Creates a new AHPTree object

Parameters:
  • root_name – The name of the root node of the tree, defaults to Goal.
  • alt_names – The alts to start this tree with.
add_alt(alt_name: str) → None[source]

Adds an alternative to this tree and all of the nodes in the tree.

Parameters:alt_name – The name of the new alternative to add.
Returns:Nothing
Raises:ValueError – If an alternative already existed with the given name
add_child(childname: str, undername: str = None) → None[source]

Adds a child node of a given name under a node.

Parameters:
  • childname – The name of the child to add.
  • undername – The name of the node to add the child under
Returns:

Nothing

Raises:

ValueError – If undername was not a node, or if childname already existed as a node.

add_user(user: str) → None[source]

Adds the user to this AHPTree object. :param user: The name of the user to add :return: Nothing :raises ValueError: If the user already existed.

alt_direct(wrt: str, alt_name: str, val: float) → None[source]

Directly sets the alternative score under wrt node. See AHPTreeNode.alt_direct for more information as that is the function that does the hard work. :param wrt: The name of the wrt node. :param alt_name: The name of the alternative to direclty set. :param val: The new directly set value. :return: Nothing :raises ValueError: * If there is no alternative by that name * If the wrt node did not exist

alt_incon_std(username, wrt: str = None) → float[source]

Calcualtes the standard inconsistency score for the pairwise comparison of the alts for the given user

Parameters:
  • username – The string name/names of users to do the inconsistency for. If more than one user we average their pairwise comparison matrices and then calculate the incosnsitency of the result.
  • wrt – The name of the node to get the inconsistency around. If None, we use the root node.
Returns:

The standard Saaty inconsistency score.

alt_pwmatrix(username, wrt: str) → numpy.ndarray[source]

Gets the alternative pairwise comparison matrix for the alts under wrt, assuming the wrt node has the alternatives under it and they are pairwise compared.

Parameters:
  • username – The name/names of the users to get the pairwise comparison of.
  • wrt – The name of the wrt node, or the AHPTreeNode object.
Returns:

A numpy array of the pairwise comparison information. If more than one user specified in usernames param we take the average of the group.

altpw(username: str, wrt: str, row: str, col: str, val, createUnknownUser=True) → None[source]

Pairwise compares a alts for a given user.

Parameters:username – The name of the user to do the comparison for. If the user doesn’t exist, this will create

the user if createUnknownUser is True, otherwise it will raise an exception

Parameters:
  • wrt – The name of the wrt node.
  • row – The name of the row alt for the comparison, i.e. the dominant node.
  • col – The name of the column alt for the comparison, i.e. the recessive node.
  • val – The vote value
Returns:

Nothing

Raises:

ValueError – If wrt, row, or col node did not exist. Also if username did not exist and

createUnknownUsers is False.

get_node(nodename: str) → pyanp.ahptree.AHPTreeNode[source]
Parameters:nodename – The string name of the node to get. If None, we return the root node.

If nodename is actually an AHPTreeObject, we simply return that object.

Returns:The AHPTreeNode object corresponding to the node with the given name
Raises:KeyError – If no such node existed
get_nodes_hash() → dict[source]
Returns:

A dictionary of nodeName:nodeObject for all nodes in this tree.

global_priority(username=None, rvalSeries=None, undername: str = None, parentMultiplier=1.0) → pandas.core.series.Series[source]

Calculates and returns the global priorities of the nodes.

Parameters:
  • username – The name/names of the users to calculate for. None means the group average.
  • rvalSeries – If not None, add the results to that series
  • undername – If None, use the root node, otherwise a string for the name of the node to go under. Internally we also allow for AHPTreeNode’s to be passed in this way.
  • parentMultiplier – The value to multiply the child priorities by.
Returns:

The global priorities as a Series whose index is the node names, and values are the global priorities.

global_priority_table() → pandas.core.frame.DataFrame[source]

Calculates the global priorities for every user, and the group

Returns:A dataframe whose columns are “Group” for the total group average, and then each user name. The rows are the node names, and values are the global priority for the given node and user.
incon_std(username, wrt: str = None) → float[source]

Calcualtes the standard inconsistency score for the pairwise comparison of the children nodes for the given user

Parameters:
  • username – The string name/names of users to do the inconsistency for. If more than one user we average their pairwise comparison matrices and then calculate the incosnsitency of the result.
  • wrt – The name of the node to get the inconsistency around. If None, we use the root node.
Returns:

The standard Saaty inconsistency score.

incon_std_series(username: str) → pandas.core.series.Series[source]

Calculates the inconsistency for all wrt nodes for a user / user group. See AHPTree.incon_std() for details about the actual calculation.

Parameters:username – The name/names of the user to calculate the inconsistency for.
Returns:A pandas.Series whose index is wrt node names, and whose values are the inconsistency of the given user(s) on that comparison.
incond_std_table() → pandas.core.frame.DataFrame[source]

Calculates the inconsistency for all users and wrt nodes in this tree.

Returns:A pandas.DataFrame whose columns are users (first column is called “Group” and is for the group average) and whose rows are wrt nodes. The values are the inconsistencies for the given user on the give wrt node’s pairwise comparison.
isalt(name: str) → bool[source]

Tells if the given alternative name is an alternative in this tree. :param name: The name of the alternative to check. :return: True if the alternative is in the list of alts for this tree, false otherwise.

nalts()[source]
Returns:

The number of alternatives in this tree.

node_pwmatrix(username, wrt: str) → numpy.ndarray[source]

Gets the pairwise comparison matrix for the nodes under wrt.

Parameters:
  • username – The name/names of the users to get the pairwise comparison of.
  • wrt – The name of the wrt node, or the AHPTreeNode object.
Returns:

A numpy array of the pairwise comparison information. If more than one user specified in usernames param we take the average of the group.

nodenames(undername: str = None, rval=None) → list[source]

Name of all nodes under the given node, including that node.

Parameters:undername – The name of the node to get all nodes under, but only if underNode is not set.

It can also be an AHPTreeNode, but that is really for internal use only

Parameters:rval – If not
Returns:The node names as a list
nodepw(username: str, wrt: str, row: str, col: str, val, createUnknownUser=True) → None[source]

Pairwise compares a nodes for a given user.

Parameters:username – The name of the user to do the comparison for. If the user doesn’t exist, this will create

the user if createUnknownUser is True, otherwise it will raise an exception

Parameters:
  • wrt – The name of the wrt node.
  • row – The name of the row node for the comparison, i.e. the dominant node.
  • col – The name of the column node for the comparison, i.e. the recessive node.
  • val – The vote value
Returns:

Nothing

Raises:

ValueError – If wrt, row, or col node did not exist. Also if username did not exist and

createUnknownUsers is False.

nodes(undername: str = None, rval=None)[source]

Returns the AHPTreeNode objects under the given node, including that node

Parameters:
  • undername – The string name of the node to get the nodes under. It can also be an AHPTreeNode object as well. If None it means the root node.
  • rval – If not None, it should be a list to add the AHPTreeNode’s to.
Returns:

The list of AHPTreeNode objects under the given node.

priority(username=None, ptype: pyanp.prioritizer.PriorityType = None) → pandas.core.series.Series[source]

Calculates the scores of the alternatives. Calls AHPTree.synthesize() first to calculate. :param username: The name (or list of names) of the user (users) to synthensize. If username is None, we calculate for the group. :param ptype: Do we want to rescale the priorities to add to 1 (normalize), or so that the largest value is a 1 (idealize), or just leave them unscaled (Raw). :return: The alternative scores, which is a pd.Series whose index is alternative names, and values are the scores.

priority_table() → pandas.core.frame.DataFrame[source]
Returns:A dataframe whose columns are “Group” for the total group average, and then each user name.

The rows are the alternative names, and the values are the alternative scores for each user.

synthesize(username=None) → None[source]

Does ahp tree synthesis to calculate the alternative scores wrt to all nodes in the tree.

Parameters:username – The name/names of the user/users to synthesize wrt. If None, that means do the full group average.
Returns:

Nothing

usernames() → list[source]
Returns:

The names of the users in this tree.