ANP structure module

Group enabled ANPNetwork class and supporting classes.

class pyanp.anp.ANPCluster(network, name: str)[source]

A cluster in an ANP object

Parameters:
  • network – The ANPNetowrk object this cluster is in.
  • name – The name of the cluster to create.
add_node(*nodes) → None[source]

Adds one or more nodes

Parameters:nodes – A vararg list of node names to add to this cluster. The names should all be strings.
Returns:Nonthing
cluster_connect(dest_cluster) → None[source]

Make a cluster->cluster connection from this node to the destination.

Parameters:dest_cluster – Either the ANPCluster object to connect to, or the name of the destination cluster.
Returns:
data_names(append_to=None)[source]

Used when exporting an Excel header for a network, for its data.

Parameters:append_to – If not None, append header strings to this list. Otherwise we create a new list to append to.
Returns:List of strings of comparison name headers. If append_to is not None, we return append_to with the new string headers appended.
is_node(node_name: str) → bool[source]

Does a node by that name exist in this cluster

Parameters:node_name – The name of the node to look for
Returns:True/False
nnodes() → int[source]
Returns:The number of nodes in this cluster.
node_names() → list[source]
Returns:List of the string names of the nodes in this cluster
node_obj(node_name)[source]

Get a node in this cluster.

Parameters:node_name – The node as either a string name, integer position, or simply the ANPObject, in which case there is nothing to do except return it.
Returns:ANPNode object. If it wasn’t found, None is returned.
node_objs() → list[source]
Returns:List of the ANPNode objects in this cluster.
set_prioritizer_type(prioritizer_class) → None[source]

Sets the cluster prioritizer type

Parameters:prioritizer_class – The new type
Returns:None
class pyanp.anp.ANPNetwork(create_alts_cluster=True)[source]

Represents an ANP prioritizer. Has clusters/nodes, comparisons, etc.

Parameters:create_alts_cluster – If True (which is the default) we start with a cluster that is the alternatives cluster. Otherwise the model starts empty.
add_alt(alt_name: str)[source]

Adds an alternative to the model: 1. Adds the altenrative to alts_cluster if not None 2. For each node with a subnetwork, we add the alternative to that subnetwork.

Parameters:alt_name – The name of the alternative to add
Returns:Nothing
add_cluster(*args) → pyanp.anp.ANPCluster[source]

Adds one or more clusters to a network

Parameters:args – Can be either a single string, or a list of strings
Returns:ANPCluster object or list of ANPCluster objects
add_node(cl, *nodes)[source]

Adds nodes to a cluster

Parameters:
  • cl – The cluster name or object
  • nodes – The name or names of the nodes
Returns:

Nothing

add_user(uname, ignore_dupe=False)[source]

Adds a user to the system

Parameters:uname – The name of the new user
Returns:Nothing

:raise ValueError If the user already existed

alt_names() → list[source]
Returns:List of alt names in this ANP model
cluster_incon_std_df(user_infos=None) → pandas.core.frame.DataFrame[source]
Parameters:user_infos – A list of users to do this for, if None is a part of this list, it means group average. If None, it defaults to None plus all users.
Returns:DataFrame whose columns are clusters, rows are users (as controlled by user_infos params) and the value is the inconsistency for the given user on the given comparison.
cluster_names() → list[source]
Returns:List of string names of the clusters
cluster_obj(cluster_info: Union[pyanp.anp.ANPCluster, str]) → pyanp.anp.ANPCluster[source]

Returns the cluster with given information

Parameters:cluster_info – Either the name of the cluster object to get or the cluster object, or its int position
Returns:The ANPCluster object
cluster_objs() → list[source]
Returns:List of ANPCluster objects in the network
cluster_prioritizer(wrtcluster=None)[source]

Gets the prioritizer for the clusters wrt a given cluster.

Parameters:wrtcluster – WRT cluster identifier as expected by cluster_obj() function. If None, then we return a dictionary indexed by cluster names and values are the prioritizers
Returns:THe prioritizer for that cluster, or a dictionary of all cluster prioritizers
data_names()[source]

Returns the column headers needed to fill in the data for this model

Returns:A list of strings that would be usable in excel for parsing headers
global_priority(username=None) → pandas.core.series.Series[source]
Parameters:username – If None, gets it for all users. Otherwise gets it for the user specified. It can also be a list of users, in which case we combine them, as per the theory.
Returns:The global priorities Series, index by node name
global_priority_df(user_infos=None) → pandas.core.frame.DataFrame[source]
Parameters:user_infos – A list of users to do this for, if None is a part of this list, it means group average. If None, it defaults to None plus all users.
Returns:The global priorities dataframe. Rows are the nodes and columns are the users. The first user/column is the Group Average
has_subnet() → bool[source]
Returns:True/False telling if some node had a subentwork
import_pw_series(series: pandas.core.series.Series) → None[source]

Takes in a well titled series of data, and pushes it into the right node’s prioritizer (or cluster). The name should be A vs B wrt C, where A, B, C are node or cluster names.

Parameters:series – The series of data for each user. Index is usernames. Values are the votes.
Returns:Nothing
import_rating_series(series: pandas.core.series.Series)[source]

Takes in a well titled series of data, and pushes it into the right node’s prioritizer as ratings (or cluster). Title should be A wrt B, where A and B are either both node names or both column names.

Parameters:series – The series of data for each user. Index is usernames. Values are the votes.
Returns:Nothing
invert_priority(p)[source]

Makes a copy of the list like element p, and inverts. The current standard inversion is 1-p. There could be others implemented later.

Parameters:p – The list like to invert
Returns:New list-like of same type as p, with inverted priorities
is_alt(altname) → bool[source]

Checks if an alternative exists

Parameters:altname – The alterantive name to look for
Returns:bool
is_user(uname) → bool[source]

Checks if a user exists

Parameters:uname – The name of the user to check for
Returns:bool
limit_matrix(username=None, as_df=False)[source]
Parameters:
  • username – If None, gets it for all users. Otherwise gets it for the user specified. It can also be a list of users, in which case we combine them, as per the theory.
  • as_df – If True, returns as a dataframe with index and column names as the names of the nodes in the network. Otherwise just returns the array.
Returns:

The limit supermatrix

nclusters() → int[source]
Returns:The number of clusters in the network.
nnodes(cluster=None) → int[source]

Returns the number of nodes in the network, or a cluster.

Parameters:cluster – If None, we return the number of nodes in the network. Otherwise this is the integer position, string name, or ANPCluster object of the cluster to get the node count within.
Returns:The count.
node_connect(src_node, dest_node)[source]

connects 2 nodes

Parameters:
  • src_node – Source node as prescribed by node_object() function
  • dest_node – Destination node as prescribed by node_object() function
Returns:

Nothing

node_connection_matrix(new_mat: numpy.ndarray = None)[source]

Returns the current node conneciton matrix if new_mat is None. Otherwise, for each item [row, col] in the matrix with a value of 1 we connect from node[row] to node[col].

Parameters:new_mat – The new node connection matrix. If None, we return the current one.
Returns:Current connection matrix.
node_connections() → numpy.ndarray[source]

Returns the node conneciton matrix for this network. :return: A numpy array of shape [nnode, nnodes] where item [row, col]

1 means there is a node connection from col -> row, and 0 means no connection.
node_incon_std_df(user_infos=None) → pandas.core.frame.DataFrame[source]
Parameters:user_infos – A list of users to do this for, if None is a part of this list, it means group average. If None, it defaults to None plus all users.
Returns:DataFrame whose columns are (node,cluster) pairs, rows are users (as controlled by user_infos params) and the value is the inconsistency for the given user on the given comparison.
node_invert(node, value=None)[source]

Either sets, or tells if a node is inverted

Parameters:
  • node – The node to do this on, as expected by node_obj() function
  • value – If None, we return the boolean about if this node is inverted. Otherwise specifies the new value.
Returns:

T/F if value=None, telling if the node is inverted. Otherwise returns nothing.

node_names(cluster=None) → list[source]

Returns a list of nodes in this network, organized by cluster

Parameters:cluster – If None, we get all nodes in network, else we get nodes in that cluster, otherwise format as specified by cluster_obj() function.
Returns:List of strs of node names
node_obj(node_name) → pyanp.anp.ANPNode[source]

Gets the ANPNode object of the node with the given name

Parameters:node_name – The name of the node to get, or it’s overall integer position, or the ANPNode object itself
Returns:The ANPNode if it exists, or None
node_objs() → list[source]

Returns a list of ANPNodes in this network, organized by cluster

Returns:List of strs of node names
node_objs_with_subnet()[source]
Returns:List of ANPNode objects in this network that have v1 subnets
node_prioritizer(wrtnode=None, cluster=None)[source]

Gets the prioritizer for node->cluster connection

Parameters:
  • wrtnode – The node as understood by node_obj() function.
  • cluster – Cluster as understood by cluster_obj() function.
Returns:

If both wrtnode and cluster are specified, a single node prioritizer is returned for that comparison (or None if there was nothing there). Otherwise it returns a dictionary indexed by [wrtnode, cluster] and whose values are the prioritizers for that (only the non-None ones).

nusers() → int[source]
Returns:The number of users
priority(username=None, ptype: pyanp.prioritizer.PriorityType = None) → pandas.core.series.Series[source]

Synthesize and return the alternative scores

Parameters:
  • username – If None, gets it for all users. Otherwise gets it for the user specified. It can also be a list of users, in which case we combine them, as per the theory.
  • ptype – The priority type to use
Returns:

A pandas.Series indexed on alt names, values are the score

scaled_supermatrix(username=None, as_df=False) → numpy.ndarray[source]
Parameters:
  • username – If None, gets it for all users. Otherwise gets it for the user specified. It can also be a list of users, in which case we combine them, as per the theory.
  • as_df – If True, returns as a dataframe with index and column names as the names of the nodes in the network. Otherwise just returns the array.
Returns:

The scaled supermatrix

set_alts_cluster(new_cluster)[source]

Sets the new alternatives cluster

Parameters:new_cluster – Cluster specified as cluster_obj() expects.
Returns:Nothing
set_pairwise_from_supermatrix(mat, username='Imported')[source]

Sets up all pairwise comparisons from supermatrix

Parameters:mat – As numpy array
Returns:Nothing
structure_priority(username=None, ptype: pyanp.prioritizer.PriorityType = None, alt_names=None) → pandas.core.series.Series[source]
subnet(wrtnode)[source]

Makes wrtnode have a subnetwork if it did not already.

Parameters:wrtnode – The node to give a subnetwork to, or get the subnetwork of. Node specified as node_obj() function expects.
Returns:The ANPNetwork that is the subnet of this node
subnet_synthesize(username=None, ptype: pyanp.prioritizer.PriorityType = None)[source]

Does the standard V1 subnetowrk synthesis.

Parameters:username – The user/users to synthesize for. If None, we group synthesize across all. If a single user, we sythesize for that user across all. If it is a list, we synthesize for the group that is that list of users.
Returns:Nothing
synthesize_combine(priorities: pandas.core.series.Series, alt_scores: dict)[source]

Performs the actual sythesis step from anp v1 synthesis.

Parameters:
  • priorities – Priorities of the subnetworks
  • alt_scores – Alt scores as dictionary, keys are subnetwork names values are Series whose keys are alt names.
Returns:

Series whose keys are alt names, and whose values are the synthesized scores.

unscaled_supermatrix(username=None, as_df=False) → numpy.array[source]
Parameters:
  • username – If None, gets it for all users. Otherwise gets it for the user specified. It can also be a list of users, in which case we combine them, as per the theory.
  • as_df – If True, returns as a dataframe with index and column names as the names of the nodes in the network. Otherwise just returns the array.
Returns:

The unscaled supermatrix as a numpy.array of shape [nnode, nnodes]

user_names() → list[source]
Returns:List of names of the users
class pyanp.anp.ANPNode(network, cluster, name: str)[source]

A node inside a cluster, inside a netowrk. The basic building block of an ANP netowrk.

Parameters:
  • network – An ANPNetwork object that this node lives inside.
  • cluster – An ANPCluster object that this node lives inside.
  • name – The name of this node.
data_names(append_to=None)[source]

Used when exporting an Excel header for a network, for its data.

Parameters:append_to – If not None, append header strings to this list. Otherwise we create a new list to append to.
Returns:List of strings of comparison name headers. If append_to is not None, we return append_to with the new string headers appended.
get_node_prioritizer(dest_node, create=False, create_class=<class 'pyanp.pairwise.Pairwise'>, dest_is_cluster=False) → pyanp.prioritizer.Prioritizer[source]

Gets the node prioritizer for the other_node

Parameters:dest_node – The node as a int, str, or ANPNode object.
Returns:The prioritizer if it exists, or None
get_unscaled_column(username=None) → pandas.core.series.Series[source]

Returns the column in the unscaled supermatrix for this node.

Parameters:username – The user/users to do this for. Typical Prioritizer calculation usage, i.e. None means do for all group average.
Returns:A pandas series indexed by the node names.
is_node_cluster_connection(dest_cluster: str) → bool[source]

Is this node connected to a cluster.

Parameters:dest_cluster – The name of the cluster
Returns:True/False
is_node_node_connection(dest_node) → bool[source]

Checks if there is a node connection from this node to dest_node

Parameters:dest_node – The node as a int, str, or ANPNode object.
Returns:
node_connect(dest_node) → None[source]

‘ Make a node connection from this node to dest_node

Parameters:dest_node – The destination node as a str, int, or ANPNode. It can be a list of nodes, and then we will coonect each node from this node. The dest_node should be in any format accepted by ANPNetwork._get_node()
set_node_prioritizer_type(destNode, prioritizer_class)[source]

Sets the node prioritizer type

Parameters:
  • destNode – An ANPNode object, string, or integer location
  • prioritizer_class – The new type
Returns:

None

pyanp.anp.anp_from_dict(cluster_dict: dict) → pyanp.anp.ANPNetwork[source]

Creates an ANPNetwork from a dictionary whose keys are cluster names and whose values are list of node names in that cluster

Parameters:cluster_dict – Keys are cluster names. If the cluster name starts with *, that is the alternatives cluster (and the asterisk is removed from the name). The values are list of strings that are the names of the nodes in that network
Returns:The ANPNetwork with that structure
pyanp.anp.anp_from_excel(excel_fname: str) → pyanp.anp.ANPNetwork[source]

Parses an excel file to get an ANPNetwork

Parameters:excel_fname – The name of the excel file
Returns:The newly created ANPNetwork object
pyanp.anp.anp_from_scaled_supermatrix(supermatrix)[source]

Creates an ANPNetwork object from a scaled supermatrix. We do this by: 1. Parsing the supermatrix and using row sums across columns to figure out clusters 2. We use pairwise comparison objects for each node and cluster comparison 3. We use the ratio of the priorities from the supermatrix to get the votes

Parameters:supermatrix – The super matrix
Returns:
pyanp.anp.anp_manual_scales_from_excel(anp: pyanp.anp.ANPNetwork, excel_fname)[source]

Parses manual rating scales from an Excel file

Parameters:
  • anp – The model to put the scale values in.
  • excel_fname – The string file name of the excel file with the data
Returns:

Nothing

pyanp.anp.clean_name(name: str) → str[source]

Cleans up a string for usage by:

  1. stripping off begging and ending spaces
  2. All spaces convert to one space
  3. and

are treated like a space

param name:The string name to be cleaned
return:The cleaned name.
pyanp.anp.get_item(tbl: dict, key)[source]

Looks up an item in a dictionary by key first, assuming the key is in the dictionary. Otherwise, it checks if the key is an integer, and returns the item in that position.

Parameters:
  • tbl – The dictionary to look in
  • key – The key, or integer position to get the item of
Returns:

The item, or it not found, None

pyanp.anp.is_pw_col_name(col: str) → bool[source]

Checks to see if the name matches the naming convention for a pairwise comparison, i.e. A vs B wrt C

Parameters:col – The title of the column to check
Returns:T/F
pyanp.anp.is_rating_col_name(col: str) → bool[source]

Checks to see if the name matches the naming convention for a rating column of data, i.e. A wrt B

Parameters:col – The name of the column
Returns:T/F
pyanp.anp.sum_subnetwork_formula(priorities: pandas.core.series.Series, dict_of_series: dict)[source]

A function that takes the weighted sum of values. Used for synthesis.

Parameters:
  • priorities – Series whose index are the nodes with subnetworks and values are their weights.
  • dict_of_series – A dictionary whose keys are the same as the keys of priorities, i.e. the nodes with subnetworks. The values are Series whose keys are alternative names and values are the synthesized alternative scores under that subnetwork.
Returns: