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), default=None:
Path to save the resulting blocks as a CSV file.
- dz2sib (bool, optional), default=False:
If True, handle non-monozygotic twins as siblings.
- ids (list or array-like, optional), default=None:
List of subject IDs to include.
Returns:¶
- tuple
- A tuple containing three elements:
- tabnumpy.ndarray
A modified table of twin data.
- Bnumpy.ndarray
Block structure representing relationships between subjects.
- famtypenumpy.ndarray
An array indicating the type of each family.
- glhmm.palm_functions.is_single_value(variable)[source]¶
Check if an array contains a singlevalue.
Parameters:¶
- variable (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.
Parameters:¶
- Ptree (list or numpy.ndarray):
The palm tree structure.
- ns (int):
The current logarithm of sign-flips (initialized to 0).
Returns:¶
- ns (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.
Parameters:¶
- Ptree (list or numpy.ndarray):
The palm tree structure.
- n_p (int):
The current logarithm of permutations (initialized to 0).
Returns:¶
- n_p (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.
Parameters:¶
- S (numpy.ndarray):
The input sequence.
Returns:¶
- n_p (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 that is 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 signflips this level.
Returns:¶
- S (numpy.ndarray):
The palm tree structure for this branch.
- Ptree (list):
The palm tree structure.
- glhmm.palm_functions.maxflipnode(Ptree, ns)[source]¶
Calculate 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:¶
- ns (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.
Parameters:¶
- Ptree (list or numpy.ndarray):
The palm tree structure.
- np (int):
The current number of permutations.
Returns:¶
- n_p (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.
Parameters:¶
- N (int, optional), default=101:
The maximum number for which to precompute factorials.
Returns:¶
- lf (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), default=’perms:
The type of shuffling to calculate (‘perms’ for permutations by default).
- uselog (bool, optional), default=False:
A flag indicating whether to calculate using logarithmic values.
Returns:¶
- maxb (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 represented by Ptree. Permutations are created by shuffling the branches of the palm tree.
Parameters:¶
- Ptree (list or numpy.ndarray):
The palm tree structure to be permuted.
- nP (int):
The number of permutations to generate.
- CMC (bool, optional), default=False:
Whether to use Conditional Monte Carlo (CMC) method for permutation. Defaults to False.
- maxP (int, optional), default=None:
The maximum number of permutations allowed. If not provided, it is calculated automatically.
Returns:¶
- P (numpy.ndarray)
An array representing the permutations. Each row corresponds to a permutation, with the first column always representing the identity permutation.
Notes:¶
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), default=None:
The matrix of attributes, which is not typically required.
- nP (int), default=1000:
The number of permutations to generate.
- CMC (bool, optional), default=False:
A flag indicating whether to use the Conditional Monte Carlo method (CMC).
- EE (bool, optional), default=True:
A flag indicating whether to assume exchangeable errors, which allows permutation.
Returns:¶
- Pset (list):
A list containing the generated permutations.
- glhmm.palm_functions.palm_reindex(B, meth='fixleaves')[source]¶
Reindex a 2D numpy array preserving block structure based on different reindexing methods.
Parameters:¶
- B (numpy.ndarray):
The 2D input array to be reindexed.
- meth (str, optional), default=’fixleaves’:
- ‘fixleaves’: Reindexes the input array by preserving the order of unique values in the first column
and recursively reindexes the remaining columns. Suitable for hierarchical data.
- ‘continuous’: Reindexes the input array by assigning new values to elements in a continuous,
non-overlapping manner within each column. Useful for continuous data.
- ‘restart’: Reindexes the input array by restarting the numbering from 1 for each block of
unique values in the first column. Suitable for data with distinct segments or blocks.
- ‘mixed’: Combines both ‘fixleaves’ and ‘continuous’ reindexing methods. Reindexes the first columns
using ‘fixleaves’ and the remaining columns using ‘continuous’, creating a mixed scheme.
Returns:¶
- Br (numpy.ndarray):
The reindexed array, preserving the block structure based on the chosen method.
- 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), default=False:
A flag indicating whether to use the Conditional Monte Carlo method (CMC).
- EE (bool, optional), default=True:
A flag indicating whether to assume exchangeable errors, which allows permutation.
Returns:¶
- Pset (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. 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:¶
- P (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 by shuffling its branches.
Parameters:¶
- Ptree_perm (list or numpy.ndarray):
The palm tree structure to be permuted.
Returns:¶
- Ptree_perm (list):
The randomly permuted palm tree structure.
- glhmm.palm_functions.renumber(B)[source]¶
Renumber 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:¶
- Br (numpy.ndarray):
The renumbered array, where elements are renumbered within blocks.
- addcol (bool):
A boolean indicating whether a column was added during renumbering.