pyslim/spatial.py
def population_size(ts, x_bins, y_bins, time_bins, stage='late', remembered_stage=None):
    '''
    Calculates the population size in each of the spatial bins defined by grid lines
    at ``x_bins`` and ``y_bins``, averaged over each of the time intervals separated by
    ``time_bins``. To obtain actual (census) sizes, the tree sequence must contain
    all individuals alive, e.g., from a SLiM simulation with all individuals
    permanently remembered.

    With ``nx``, ``ny`` and ``nt`` the number of bins in the ``x``, ``y`` and time directions
    (so, ``nx`` is one less than the length of ``x_bins``),
    returns a 3-d array with dimensions ``(nx, ny, nt)``.
    The ``(i,j,k)``th element of the array is the average number of
    individuals with ``x`` coordinate in the half-open interval ``[x_bins[i], x_bins[i + 1])``
    and ``y`` coordinate in the half-open interval ``[y_bins[i], y_bins[i + 1])``,
    averaged across all times in the half-open interval ``[time_bins[k], time_bins[k + 1])``.

    For integer endpoints of the time bins, this average is equivalent to
    recording the number of indivduals that are alive at each time in the time
    interval and have location in the relevant location bin, then taking the
    mean of these recorded population sizes.

    :param TreeSequence ts: The tree sequence to calculate population size from.
    :param array x_bins: The x-coordinates of the boundaries of the location bins.
    :param array y_bins: The y-coordinates of the boundaries of the location bins.
    :param array time_bins: The endpoints of the time bins.
    :param str stage: The stage in the SLiM life cycle that the endpoints of
        the time bins refer to (either "early" or "late"; defaults to "late").
    :param str remembered_stage: The stage in the SLiM life cycle during which
        individuals were Remembered (defaults to the stage the tree sequence was
        recorded at, stored in metadata).
    '''

    # TODO: make a helper function to do this
    if stage not in ("late", "early"):
        raise ValueError(f"Unknown stage '{stage}': "
                          "should be either 'early' or 'late'.")

    if remembered_stage is None:
        remembered_stage = ts.metadata['SLiM']['stage']

    if remembered_stage not in ("late", "early"):
        raise ValueError(f"Unknown remembered_stage '{remembered_stage}': "
                          "should be either 'early' or 'late'.")
    if remembered_stage != ts.metadata['SLiM']['stage']:
        warnings.warn(f"Provided remembered_stage '{remembered_stage}' does not"
                      " match the stage at which the tree sequence was saved"
                      f" ('{ts.metadata['SLiM']['stage']}'). This is not necessarily"
                      " an error, but mismatched stages will lead to inconsistencies:"
                      " make sure you know what you're doing.")

    # see individuals_alive_at for explanation
    if stage == "early" and ts.metadata['SLiM']['model_type'] == "WF":
        birth_offset = 1
    else:
        birth_offset = 0
    birth_times = ts.individual_times - birth_offset
    if (ts.metadata['SLiM']['model_type'] == "WF"
            or stage == remembered_stage):
        age_offset = 0
    else:
        if (remembered_stage == "early"
                and stage == "late"):
            age_offset = -1
        else:
            age_offset = 1
    ages = ts.individual_ages + age_offset
    death_times = birth_times - ages

    time_breaks = time_bins
    x_breaks = x_bins
    y_breaks = y_bins

    nxbins = len(x_breaks) - 1
    nybins = len(y_breaks) - 1
    ntbins = len(time_breaks) - 1
    popsize = np.empty((nxbins, nybins, ntbins))

    locations = ts.individual_locations

    for i in np.arange(nxbins):
        for j in np.arange(nybins):
            x0, x1 = x_breaks[i], x_breaks[i + 1]
            y0, y1 = y_breaks[j], y_breaks[j + 1]
            in_bin = _in_location_bin(locations, x0, x1, y0, y1)
            for k in np.arange(ntbins):
                t0, t1 = time_breaks[k], time_breaks[k + 1]
                popsize[i, j, k] = _average_time_alive(
                        birth_times[in_bin],
                        death_times[in_bin],
                        t0,
                        t1,
                )

    return popsize

    Calculates the population size in each of the spatial bins defined by grid lines
    at ``x_bins`` and ``y_bins``, averaged over each of the time intervals separated by
    ``time_bins``. To obtain actual (census) sizes, the tree sequence must contain
    all individuals alive, e.g., from a SLiM simulation with all individuals
    permanently remembered.

    With ``nx``, ``ny`` and ``nt`` the number of bins in the ``x``, ``y`` and time directions
    (so, ``nx`` is one less than the length of ``x_bins``),
    returns a 3-d array with dimensions ``(nx, ny, nt)``.
    The ``(i,j,k)``th element of the array is the average number of
    individuals with ``x`` coordinate in the half-open interval ``[x_bins[i], x_bins[i + 1])``
    and ``y`` coordinate in the half-open interval ``[y_bins[i], y_bins[i + 1])``,
    averaged across all times in the half-open interval ``[time_bins[k], time_bins[k + 1])``.

    For integer endpoints of the time bins, this average is equivalent to
    recording the number of indivduals that are alive at each time in the time
    interval and have location in the relevant location bin, then taking the
    mean of these recorded population sizes.

    :param TreeSequence ts: The tree sequence to calculate population size from.
    :param array x_bins: The x-coordinates of the boundaries of the location bins.
    :param array y_bins: The y-coordinates of the boundaries of the location bins.
    :param array time_bins: The endpoints of the time bins.
    :param str stage: The stage in the SLiM life cycle that the endpoints of
        the time bins refer to (either "early" or "late"; defaults to "late").
    :param str remembered_stage: The stage in the SLiM life cycle during which
        individuals were Remembered (defaults to the stage the tree sequence was
        recorded at, stored in metadata).
    




For integer endpoints of the time bins, this average is equivalent to
recording the number of indivduals that are alive at each time in the time
interval and have location in the relevant location bin, then taking the
mean of these recorded population sizes.
For integer endpoints of the time bins this average is equivalent to recording the number of indivduals that are alive at each time in the time interval and have location in the relevant location bin then taking the mean of these recorded population sizes .
def population_size(ts, x_bins, y_bins, time_bins, stage='late', remembered_stage=None):
    if stage not in ("late", "early"):
        raise ValueError(f"Unknown stage '{stage}': "
                          "should be either 'early' or 'late'.")
    if remembered_stage is None:
        remembered_stage = ts.metadata['SLiM']['stage']
    if remembered_stage not in ("late", "early"):
        raise ValueError(f"Unknown remembered_stage '{remembered_stage}': "
                          "should be either 'early' or 'late'.")
    if remembered_stage != ts.metadata['SLiM']['stage']:
        warnings.warn(f"Provided remembered_stage '{remembered_stage}' does not"
                      " match the stage at which the tree sequence was saved"
                      f" ('{ts.metadata['SLiM']['stage']}'). This is not necessarily"
                      " an error, but mismatched stages will lead to inconsistencies:"
                      " make sure you know what you're doing.")
    if stage == "early" and ts.metadata['SLiM']['model_type'] == "WF":
        birth_offset = 1
    else:
        birth_offset = 0
    birth_times = ts.individual_times - birth_offset
    if (ts.metadata['SLiM']['model_type'] == "WF"
            or stage == remembered_stage):
        age_offset = 0
    else:
        if (remembered_stage == "early"
                and stage == "late"):
            age_offset = -1
        else:
            age_offset = 1
    ages = ts.individual_ages + age_offset
    death_times = birth_times - ages
    time_breaks = time_bins
    x_breaks = x_bins
    y_breaks = y_bins
    nxbins = len(x_breaks) - 1
    nybins = len(y_breaks) - 1
    ntbins = len(time_breaks) - 1
    popsize = np.empty((nxbins, nybins, ntbins))
    locations = ts.individual_locations
    for i in np.arange(nxbins):
        for j in np.arange(nybins):
            x0, x1 = x_breaks[i], x_breaks[i + 1]
            y0, y1 = y_breaks[j], y_breaks[j + 1]
            in_bin = _in_location_bin(locations, x0, x1, y0, y1)
            for k in np.arange(ntbins):
                t0, t1 = time_breaks[k], time_breaks[k + 1]
                popsize[i, j, k] = _average_time_alive(
                        birth_times[in_bin],
                        death_times[in_bin],
                        t0,
                        t1,
                )
    return popsize
def population_size ( ts , x_bins , y_bins , time_bins , stage = 'late' , remembered_stage = None ) : if stage not in ( "late" , "early" ) : raise ValueError ( f"Unknown stage '{stage}': " "should be either 'early' or 'late'." ) if remembered_stage is None : remembered_stage = ts . metadata [ 'SLiM' ] [ 'stage' ] if remembered_stage not in ( "late" , "early" ) : raise ValueError ( f"Unknown remembered_stage '{remembered_stage}': " "should be either 'early' or 'late'." ) if remembered_stage != ts . metadata [ 'SLiM' ] [ 'stage' ] : warnings . warn ( f"Provided remembered_stage '{remembered_stage}' does not" " match the stage at which the tree sequence was saved" f" ('{ts.metadata['SLiM']['stage']}'). This is not necessarily" " an error, but mismatched stages will lead to inconsistencies:" " make sure you know what you're doing." ) if stage == "early" and ts . metadata [ 'SLiM' ] [ 'model_type' ] == "WF" : birth_offset = 1 else : birth_offset = 0 birth_times = ts . individual_times - birth_offset if ( ts . metadata [ 'SLiM' ] [ 'model_type' ] == "WF" or stage == remembered_stage ) : age_offset = 0 else : if ( remembered_stage == "early" and stage == "late" ) : age_offset = - 1 else : age_offset = 1 ages = ts . individual_ages + age_offset death_times = birth_times - ages time_breaks = time_bins x_breaks = x_bins y_breaks = y_bins nxbins = len ( x_breaks ) - 1 nybins = len ( y_breaks ) - 1 ntbins = len ( time_breaks ) - 1 popsize = np . empty ( ( nxbins , nybins , ntbins ) ) locations = ts . individual_locations for i in np . arange ( nxbins ) : for j in np . arange ( nybins ) : x0 , x1 = x_breaks [ i ] , x_breaks [ i + 1 ] y0 , y1 = y_breaks [ j ] , y_breaks [ j + 1 ] in_bin = _in_location_bin ( locations , x0 , x1 , y0 , y1 ) for k in np . arange ( ntbins ) : t0 , t1 = time_breaks [ k ] , time_breaks [ k + 1 ] popsize [ i , j , k ] = _average_time_alive ( birth_times [ in_bin ] , death_times [ in_bin ] , t0 , t1 , ) return popsize
Calculates the population size in each of the spatial bins defined by grid lines
at ``x_bins`` and ``y_bins``, averaged over each of the time intervals separated by
``time_bins``.
Calculates the population size in each of the spatial bins defined by grid lines at ` ` x_bins ` ` and ` ` y_bins ` ` averaged over each of the time intervals separated by ` ` time_bins ` ` .
'''
    Calculates the population size in each of the spatial bins defined by grid lines
    at ``x_bins`` and ``y_bins``, averaged over each of the time intervals separated by
    ``time_bins``. To obtain actual (census) sizes, the tree sequence must contain
    all individuals alive, e.g., from a SLiM simulation with all individuals
    permanently remembered.

    With ``nx``, ``ny`` and ``nt`` the number of bins in the ``x``, ``y`` and time directions
    (so, ``nx`` is one less than the length of ``x_bins``),
    returns a 3-d array with dimensions ``(nx, ny, nt)``.
    The ``(i,j,k)``th element of the array is the average number of
    individuals with ``x`` coordinate in the half-open interval ``[x_bins[i], x_bins[i + 1])``
    and ``y`` coordinate in the half-open interval ``[y_bins[i], y_bins[i + 1])``,
    averaged across all times in the half-open interval ``[time_bins[k], time_bins[k + 1])``.

    For integer endpoints of the time bins, this average is equivalent to
    recording the number of indivduals that are alive at each time in the time
    interval and have location in the relevant location bin, then taking the
    mean of these recorded population sizes.

    :param TreeSequence ts: The tree sequence to calculate population size from.
    :param array x_bins: The x-coordinates of the boundaries of the location bins.
    :param array y_bins: The y-coordinates of the boundaries of the location bins.
    :param array time_bins: The endpoints of the time bins.
    :param str stage: The stage in the SLiM life cycle that the endpoints of
        the time bins refer to (either "early" or "late"; defaults to "late").
    :param str remembered_stage: The stage in the SLiM life cycle during which
        individuals were Remembered (defaults to the stage the tree sequence was
        recorded at, stored in metadata).
    ''' # TODO: make a helper function to do this # see individuals_alive_at for explanation
ts
The tree sequence to calculate population size from.
The tree sequence to calculate population size from .
x_bins
The x-coordinates of the boundaries of the location bins.
The x - coordinates of the boundaries of the location bins .
y_bins
The y-coordinates of the boundaries of the location bins.
The y - coordinates of the boundaries of the location bins .
time_bins
The endpoints of the time bins.
The endpoints of the time bins .
stage
The stage in the SLiM life cycle that the endpoints of
the time bins refer to (either "early" or "late"; defaults to "late").
The stage in the SLiM life cycle that the endpoints of the time bins refer to ( either " early " or " late " ; defaults to " late " ) .
"late")
remembered_stage
The stage in the SLiM life cycle during which
individuals were Remembered (defaults to the stage the tree sequence was
recorded at, stored in metadata).
The stage in the SLiM life cycle during which individuals were Remembered ( defaults to the stage the tree sequence was recorded at stored in metadata ) .
the stage the tree sequence was
recorded at, stored in metadata)