pynrm package

Submodules

pynrm.Pedigree module

class pynrm.Pedigree.Pedigree(data=None, male_size=500, female_size=500)

Bases: object

Holds pedigree data.

Given a dataframe, validates and converts it to the form that Simulator class expects. If data not provided, randomly generates initial generation of 1000 animals with 500 males and females each.

data

A dataframe where each row contains information of an individual animal including generation, sire id, dam id, genetic value or estimated breeding value (EBV), and sex. Index of the row represents the id of each animal. Columns for ids of sire and dam have nullable integer datatype for when the information is unknown.

get_avg_ebv(gen)

Returns the average EBV of a generation.

Average EBV is computed across the generation.

Parameters:

gen – An integer indicating the generation number.

Returns:

A float that corresponds to the average EBV of the given generation.

pynrm.Simulator module

class pynrm.Simulator.Simulator(pedigree, male_k, female_k, h, w)

Bases: object

Simulates reproductive cycles.

Performs livestock breeding simulations that provide fine-grained control.

pedigree

An instance of Pedigree class holding the recorded ancestry data.

male_k

An integer count of males to select on each reproduction.

female_k

An integer count of females to select on each reproduction.

h

A float indicating heritability value.

w

A float indicating penalization weight for inbreeding while reproduction.

gen

An integer indicating latest generation number starting from 0. Increments after each reproduction.

export_to_csv(filename)

Exports generated pedigree data as csv.

Writes pedigree data that have been generated in a Simulator instance to a comma-separated values (csv) file.

Parameters:

filename – A name of the path object or file-like object to export to.

get_adjusted_ebv(candidate, already_selected)

Computes adjusted EBV of the candidate.

EBV is adjusted to alleviate high inbreeding when selecting top k animals. This is calculated by solving the average relationship between the candidate and already selected animals. EBV of the candidate is then penalized for the average relationship with the user-defined weight of the simulator.

Parameters:
  • candidate – An integer indicating the candidate id.

  • already_selected – A list of already selected animals.

Returns:

A float that corresponds to the adjusted EBV of the candidate.

get_ebv(sire, dam)

Randomly generates EBV of an individual animal.

EBV is computed using heritability, EBV and inbreeding coefficients of sire and dam. It first solves the square root of heritability and average of inbreeding coefficients of sire and dam derived from NRM. Then, it calculates some values, multiplies by random normal deviates, and adds them to the average of EBV of sire and dam.

Parameters:
  • sire – An integer indicating the sire id.

  • dam – An integer indicating the dam id.

Returns:

A float that corresponds to the randomly generated EBV.

get_top_k(top_k, candidates, k)

Selects top k animals

Chooses top k animals recursively by picking the candidate with highest adjusted EBV on each iteration until there are k animals selected.

Parameters:
  • top_k – A list of already selected animals.

  • candidates – A list of all remaining candidates excluding the already selected animals.

  • k – Number of animals to select.

Returns:

A list of all selected animals so far. This is a list that contains already selected animals and the newly selected animal from this iteration.

plot_ebv_by_gen()

Plot average EBV by generation.

Average EBV by generation is calculated and displayed as a basic line graph. All generations that have been generated in a Simulator instance is displayed.

plot_inbreeding_by_gen()

Plot average inbreeding coefficients by generation.

Average inbreeding coefficients by generation is calculated and displayed as a basic line graph. All generations that have been generated in a Simulator instance is displayed.

reproduce()

Produces the next generation of animals

Selects males and females (male_k and female_k animals each) to reproduce and generates new animals. It also updates the latest generation number and pedigree of the simulator to reflect the new reproductive cycle.

Returns:

A dataframe consisting all animals before reproduction and newly bred animals.

pynrm.nrm module

pynrm.nrm.get_avg_inbreeding(pedigree, gen)

Returns the average inbreeding rate of a generation.

Average inbreeding rate is calculated across the generation.

Parameters:

gen – An integer indicating the generation number.

Returns:

A float that corresponds to the average inbreeding rate of the given generation.

pynrm.nrm.get_nrm(pedigree, i, j)

Calculates the numerator relationship matrix (NRM) value.

Recursively computes the (i, j) value of NRM using information available from the pedigree provided.

Parameters:
  • pedigree – An instance of Pedigree class holding the recorded ancestry data.

  • i – An integer indicating the row.

  • j – An integer indicating the column.

Returns:

A float that corresponds to the (i, j) value of NRM.

Module contents