glhmm.palm_functions#

glhmm.palm_functions.hcp2block(tmp, blocksfile=None, dz2sib=False, ids=None)[source]#

Convert HCP-style twin data into block structure.

Parameters:#

file (str): Path to the input CSV file containing twin data. blocksfile (str, optional): Path to save the resulting blocks as a CSV file. dz2sib (bool, optional): If True, handle non-monozygotic twins as siblings. Default is False. ids (list or array-like, optional): List of subject IDs to include. Default is None.

Returns:#

tuple: A tuple containing three elements:

tab (numpy.ndarray): A modified table of twin data. B (numpy.ndarray): Block structure representing relationships between subjects. famtype (numpy.ndarray): An array indicating the type of each family.

glhmm.palm_functions.is_single_value(variable)[source]#

Check if an array contains a singlevalue.

This function checks if an array contains a singlevalue.

Parameters: arr (numpy.ndarray or list): The array to be checked.

Returns: bool: True if the array contains a single value, False otherwise.

glhmm.palm_functions.lmaxflipnode(Ptree, ns)[source]#

Calculate the logarithm of the maximum number of sign-flips within a palm tree node.

This function calculates the logarithm of the maximum number of sign-flips within a palm tree node.

Parameters: Ptree (list or numpy.ndarray): The palm tree structure. ns (int): The current logarithm of sign-flips (initialized to 0).

Returns: int: The logarithm of the maximum number of sign-flips within the node.

glhmm.palm_functions.lmaxpermnode(Ptree, n_p)[source]#

Calculate the logarithm of the maximum number of permutations within a palm tree node.

This function calculates the logarithm of the maximum number of permutations within a palm tree node.

Parameters: Ptree (list or numpy.ndarray): The palm tree structure. n_p (int): The current logarithm of permutations (initialized to 0).

Returns: int: The logarithm of the maximum number of permutations within the node.

glhmm.palm_functions.lseq2np(S)[source]#

Calculate the logarithm of the number of permutations for a given sequence.

This function calculates the logarithm of the number of permutations for a given sequence.

Parameters: S (numpy.ndarray): The input sequence.

Returns: int: The logarithm of the number of permutations for the sequence.

glhmm.palm_functions.maketree(B, M, O, wholeblock, nosf)[source]#

Recursively construct a palm tree structure from input matrices.

This function builds a palm tree structure by recursively processing input matrices representing nodes in the palm tree.

Parameters: B (numpy.ndarray): The input matrix where each row represents a node in the palm tree (Block definitions). M (numpy.ndarray): The corresponding Design-matrix, which associates nodes in B with additional data. O (numpy.ndarray): Observation indices wholeblock (bool): A boolean indicating if the entire block is positive based on the first element of B. nosf (bool): A boolean indicating if there are no signflip this level

Returns: tuple: A tuple containing:

  • S (numpy.ndarray or float): The palm tree structure for this branch.

  • Ptree (numpy.ndarray or list): The palm tree structure

glhmm.palm_functions.maxflipnode(Ptree, ns)[source]#

Calculate the maximum number of sign-flips within a palm tree node.

This function recursively calculates the maximum number of sign-flips within a palm tree node.

Parameters: Ptree (list or numpy.ndarray): The palm tree structure. ns (int): The current number of sign-flips (initialized to 1).

Returns: int: The maximum number of sign-flips within the node.

glhmm.palm_functions.maxpermnode(Ptree, np)[source]#

Calculate the maximum number of permutations within a palm tree node.

This function recursively calculates the maximum number of permutations within a palm tree node.

Parameters: Ptree (list or numpy.ndarray): The palm tree structure. np (int): The current number of permutations (initialized to 1).

Returns: int: The maximum number of permutations within the node.

glhmm.palm_functions.palm_factorial(N=101)[source]#

Calculate logarithmically scaled factorials up to a given number.

This function precomputes logarithmically scaled factorials up to a specified number.

Parameters: N (int, optional): The maximum number for which to precompute factorials (defaults to 101).

Returns: numpy.ndarray: An array of precomputed logarithmically scaled factorials.

glhmm.palm_functions.palm_maxshuf(Ptree, stype='perms', uselog=False)[source]#

Calculate the maximum number of shufflings (permutations or sign-flips) for a given palm tree structure.

Parameters: Ptree (list or numpy.ndarray): The palm tree structure. stype (str, optional): The type of shuffling to calculate (‘perms’ for permutations by default). uselog (bool, optional): A flag indicating whether to calculate using logarithmic values (defaults to False).

Returns: int: The maximum number of shufflings (permutations or sign-flips) based on the specified criteria.

glhmm.palm_functions.palm_permtree(Ptree, nP, CMC=False, maxP=None)[source]#

Generate permutations of a given palm tree structure.

This function generates permutations of a palm tree structure represented by Ptree. Permutations are created by shuffling the branches of the palm tree. The number of permutations is controlled by the ‘nP’ parameter.

Parameters:#

Ptree (list or numpy.ndarray): The palm tree structure to be permuted. nP (int): The number of permutations to generate. CMC (bool, optional): Whether to use Conditional Monte Carlo (CMC) method for permutation.

Defaults to False.

maxP (int, optional): The maximum number of permutations allowed. If not provided, it is calculated automatically.

Returns:#

numpy.ndarray: An array representing the permutations. Each row corresponds to a permutation, with the first

column always representing the identity permutation.

Note: - If ‘CMC’ is False and ‘nP’ is greater than ‘maxP’ / 2, a warning message is displayed, as it may take a

considerable amount of time to find non-repeated permutations.

  • The function utilizes the ‘pickperm’ and ‘randomperm’ helper functions for the permutation process.

glhmm.palm_functions.palm_quickperms(EB, M=None, nP=1000, CMC=False, EE=True)[source]#

Generate a set of permutations for a given input matrix using palm methods.

Parameters:#

EB (numpy.ndarray): Block structure representing relationships between subjects. M (numpy.ndarray, optional): The matrix of attributes, which is not typically required.

Defaults to None.

nP (int): The number of permutations to generate. CMC (bool, optional): A flag indicating whether to use the Conditional Monte Carlo method (CMC).

Defaults to False.

EE (bool, optional): A flag indicating whether to assume exchangeable errors, which allows permutation.

Defaults to True.

Returns:#

list: A list containing the generated permutations.

glhmm.palm_functions.palm_reindex(B, meth='fixleaves')[source]#

Reindex a 2D numpy array using different procedures while preserving block structure.

This function reorders the elements of a 2D numpy array B by applying one of several reindexing methods. The primary goal of reindexing is to assign new values to elements in such a way that they are organized in a desired order or structure.

Parameters: B (numpy.ndarray): The 2D input array to be reindexed. meth (str, optional): The reindexing method to be applied. It can take one of the following values:

  • ‘fixleaves’: This method reindexes the input array by preserving the order of unique values in the first column and recursively reindexes the remaining columns. It is well-suited for hierarchical data where the first column represents levels or leaves.

  • ‘continuous’: This method reindexes the input array by assigning new values to elements in a continuous, non-overlapping manner within each column. It is useful for continuous data or when preserving the order of unique values is not a requirement.

  • ‘restart’: This method reindexes the input array by restarting the numbering from 1 for each block of unique values in the first column. It is suitable for data that naturally breaks into distinct segments or blocks.

  • ‘mixed’: This method combines both the ‘fixleaves’ and ‘continuous’ reindexing methods. It reindexes the first columns using ‘fixleaves’ and the remaining columns using ‘continuous’, creating a mixed reindexing scheme.

Returns: numpy.ndarray: The reindexed array, preserving the block structure based on the chosen method.

Raises: ValueError: If the meth parameter is not one of the valid reindexing methods.

glhmm.palm_functions.palm_shuftree(Ptree, nP, CMC=False, EE=True)[source]#

Generate a set of shufflings (permutations or sign-flips) for a given palm tree structure.

Parameters:#

Ptree (list): The palm tree structure. nP (int): The number of permutations to generate.

CMC (bool, optional): A flag indicating whether to use the Conditional Monte Carlo method (CMC).

Defaults to False.

EE (bool, optional): A flag indicating whether to assume exchangeable errors, which allows permutation.

Defaults to True.

Returns:#

list: A list containing the generated shufflings (permutations).

glhmm.palm_functions.palm_tree(B, M=None)[source]#

Construct a palm tree structure from an input matrix B and an optional design-matrix M.

The palm tree represents a hierarchical structure where each node can have three branches: - The left branch contains data elements. - The middle branch represents special features (if any). - The right branch contains nested structures.

Parameters: B (numpy.ndarray): The input matrix where each row represents the Multi-level block definitions of the PALM tree. M (numpy.ndarray, optional): An optional Design-matrix that associates each node in B with additional data.

Defaults to None.

Returns: list: A list containing three elements:

  • Ptree[0] (numpy.ndarray or list): The left branch of the palm tree, containing data elements.

  • Ptree[1] (numpy.ndarray, list, or empty list): The middle branch of the palm tree, representing

    special features (if any).

  • Ptree[2] (numpy.ndarray or list): The right branch of the palm tree, containing nested structures.

glhmm.palm_functions.pickperm(Ptree, P)[source]#

Extract a permutation from a palm tree structure.

This function extracts a permutation from a given palm tree structure. It does not perform the permutation but returns the indices representing the already permuted tree.

Parameters:#

Ptree (list or numpy.ndarray): The palm tree structure. P (numpy.ndarray): The current state of the permutation.

Returns:#

numpy.ndarray: An array of indices representing the permutation of the palm tree structure.

glhmm.palm_functions.randomperm(Ptree_perm)[source]#

Create a random permutation of a palm tree structure.

This function generates a random permutation of a given palm tree structure by shuffling its branches.

Parameters:#

Ptree_perm (list or numpy.ndarray): The palm tree structure to be permuted.

Returns:#

list: The randomly permuted palm tree structure.

glhmm.palm_functions.renumber(B)[source]#

Renumber the elements in a 2D numpy array B, preserving their order within distinct blocks.

This function renumbers the elements in the input array B based on distinct values in its first column. Each distinct value represents a block, and the elements within each block are renumbered sequentially, while preserving the relative order of elements within each block.

Parameters: B (numpy.ndarray): The 2D input array to be renumbered.

Returns: tuple: A tuple containing:

  • Br (numpy.ndarray): The renumbered array, where elements are renumbered within blocks.

  • addcol (bool): A boolean indicating whether a column was added during renumbering.

glhmm.palm_functions.seq2np(S)[source]#

Calculate the number of permutations for a given sequence.

This function calculates the number of permutations for a given sequence.

Parameters: S (numpy.ndarray): The input sequence.

Returns: int: The number of permutations for the sequence.