Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cpp/genetic_values/DiploidGeneticValue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ init_DiploidGeneticValue(py::module& m)
.def_property_readonly(
"shape",
[](const fwdpy11::DiploidGeneticValue& self) {
if (self.total_dim > 1 && self.total_dim != self.gvalues.size())
if (self.total_dim() > 1 && self.total_dim() != self.gvalues.size())
{
throw std::runtime_error("dimensionality mismatch");
}
return pybind11::make_tuple(self.total_dim);
return pybind11::make_tuple(self.total_dim());
},
R"delim(
Return the dimensions of the genetic values.
Expand Down
39 changes: 8 additions & 31 deletions cpp/genetic_values/PyDiploidGeneticValue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with fwdpy11. If not, see <http://www.gnu.org/licenses/>.
//
#include <pybind11/pybind11.h>
#include <fwdpy11/genetic_values/DiploidGeneticValue.hpp>
#include <fwdpy11/genetic_values/DiploidGeneticValueCalculation.hpp>
#include <fwdpy11/genetic_value_to_fitness/GeneticValueIsTrait.hpp>
#include <fwdpp/fitness_models.hpp>

Expand All @@ -43,13 +43,10 @@ struct PyDiploidGeneticValueData
}
};

class PyDiploidGeneticValue : public fwdpy11::DiploidGeneticValue
class PyDiploidGeneticValue : public fwdpy11::DiploidGeneticValueCalculation
{
public:
PyDiploidGeneticValue(std::size_t ndim,
const fwdpy11::GeneticValueToFitnessMap* gvalue_to_fitness_map,
const fwdpy11::GeneticValueNoise* noise)
: fwdpy11::DiploidGeneticValue(ndim, gvalue_to_fitness_map, noise)
PyDiploidGeneticValue() : fwdpy11::DiploidGeneticValueCalculation()
{
}
};
Expand All @@ -63,30 +60,13 @@ class PyDiploidGeneticValueTrampoline : public PyDiploidGeneticValue
calculate_gvalue(const fwdpy11::DiploidGeneticValueData input_data) override
{
PYBIND11_OVERLOAD_PURE(double, PyDiploidGeneticValue, calculate_gvalue,
PyDiploidGeneticValueData(input_data, this->gvalues));
}

double
genetic_value_to_fitness(
const fwdpy11::DiploidGeneticValueToFitnessData input_data) override
// NOTE: see https://pybind11.readthedocs.io/en/stable/advanced/classes.html#extended-trampoline-class-functionality
{
pybind11::gil_scoped_acquire gil; // Acquire the GIL while in this scope.
// Try to look up the overloaded method on the Python side.
pybind11::function overload
= pybind11::get_overload(this, "genetic_value_to_fitness");
if (overload)
{
auto obj = overload(input_data);
return obj.cast<double>();
}
return this->gv2w->operator()(input_data);
PyDiploidGeneticValueData(input_data));
}

void
update(const fwdpy11::DiploidPopulation& pop) override
{
PYBIND11_OVERLOAD_PURE(void, PyDiploidGeneticValue, update, pop);
PYBIND11_OVERLOAD_PURE(void, PyDiploidGeneticValueCalculation, update, pop);
}
};

Expand Down Expand Up @@ -118,12 +98,9 @@ additive_effects(const fwdpy11::DiploidPopulation& pop,
void
init_PyDiploidGeneticValue(py::module& m)
{
py::class_<PyDiploidGeneticValue, fwdpy11::DiploidGeneticValue,
PyDiploidGeneticValueTrampoline>(m, "_PyDiploidGeneticValue")
.def(py::init<std::size_t, const fwdpy11::GeneticValueToFitnessMap*,
const fwdpy11::GeneticValueNoise*>(),
py::arg("ndim"), py::arg("genetic_value_to_fitness"),
py::arg("noise"));
py::class_<PyDiploidGeneticValue, fwdpy11::DiploidGeneticValueCalculation,
PyDiploidGeneticValueTrampoline>(m, "_PyDiploidGeneticValueCalculation")
.def(py::init<>());

py::class_<PyDiploidGeneticValueData>(m, "PyDiploidGeneticValueData",
py::buffer_protocol())
Expand Down
61 changes: 40 additions & 21 deletions fwdpy11/headers/fwdpy11/genetic_values/DiploidGeneticValue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
#ifndef FWDPY11_DIPLOID_GENETIC_VALUE_HPP__
#define FWDPY11_DIPLOID_GENETIC_VALUE_HPP__

#include <cstdint>
#include <stdexcept>
#include <vector>
#include <fwdpy11/rng.hpp>
#include <fwdpy11/types/DiploidPopulation.hpp>
#include <fwdpy11/genetic_values/DiploidGeneticValueCalculation.hpp>
#include <fwdpy11/genetic_value_to_fitness/GeneticValueToFitnessMap.hpp>
#include <fwdpy11/genetic_value_to_fitness/GeneticValueIsFitness.hpp>
#include <fwdpy11/genetic_value_noise/NoNoise.hpp>
Expand All @@ -49,56 +50,74 @@ namespace fwdpy11
}

public:
std::size_t total_dim;
std::vector<double> gvalues;
// Even though these are stored as shared_ptr,
// this class is non-copyable because its state
// may change over time via the various update
// functions.
std::shared_ptr<DiploidGeneticValueCalculation> model;
std::shared_ptr<GeneticValueToFitnessMap> gv2w;
std::shared_ptr<GeneticValueNoise> noise_fxn;

DiploidGeneticValue(std::size_t ndim, const GeneticValueToFitnessMap* gv2w_,
DiploidGeneticValue(const DiploidGeneticValueCalculation& model_,
const GeneticValueToFitnessMap* gv2w_,
const GeneticValueNoise* noise)
: total_dim(ndim), gvalues(total_dim, 0.),
: gvalues(model_.ndim(), 0.), model(model_.clone()),
gv2w{process_input<GeneticValueToFitnessMap, GeneticValueIsFitness,
std::size_t>(gv2w_, ndim)},
std::size_t>(gv2w_, model_.ndim())},
noise_fxn{process_input<GeneticValueNoise, NoNoise>(noise)}
{
if (model->ndim() != gv2w->ndim())
{
throw std::invalid_argument(
"GeneticValueToFitnessMap and DiploidGeneticValueCalculation "
"must have identical dimensions");
}
}

// The type is move-only
virtual ~DiploidGeneticValue() = default;
~DiploidGeneticValue() = default;
DiploidGeneticValue(const DiploidGeneticValue&) = delete;
DiploidGeneticValue(DiploidGeneticValue&&) = default;
DiploidGeneticValue& operator=(const DiploidGeneticValue&) = delete;
DiploidGeneticValue& operator=(DiploidGeneticValue&&) = default;

virtual double calculate_gvalue(const DiploidGeneticValueData data) = 0;
// virtual double calculate_gvalue(const DiploidGeneticValueData data) = 0;

virtual void update(const DiploidPopulation& pop) = 0;
void
update(const DiploidPopulation& pop)
{
this->model->update(pop);
this->noise_fxn->update(pop);
this->gv2w->update(pop);
}

// To be called from w/in a simulation
inline void
operator()(DiploidGeneticValueData data)
{
data.offspring_metadata.get().g = calculate_gvalue(data);
data.offspring_metadata.get().e = noise(DiploidGeneticValueNoiseData(data));
data.offspring_metadata.get().w = genetic_value_to_fitness(
DiploidGeneticValueToFitnessData(data, gvalues));
data.offspring_metadata.get().g = model->calculate_gvalue(data);
data.offspring_metadata.get().e
= noise_fxn->operator()(DiploidGeneticValueNoiseData(data));
data.offspring_metadata.get().w
= gv2w->operator()(DiploidGeneticValueToFitnessData(data, gvalues));
}

virtual double
genetic_value_to_fitness(const DiploidGeneticValueToFitnessData data)
{
return gv2w->operator()(data);
std::size_t total_dim() const {
return model->ndim();
}

virtual double
noise(const DiploidGeneticValueNoiseData data) const
{
return noise_fxn->operator()(data);
}
// virtual double
// genetic_value_to_fitness(const DiploidGeneticValueToFitnessData data)
// {
// return gv2w->operator()(data);
// }

// virtual double
// noise(const DiploidGeneticValueNoiseData data) const
// {
// return noise_fxn->operator()(data);
// }
};
} //namespace fwdpy11

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Copyright (C) 2025 Kevin Thornton <krthornt@uci.edu>
//
// This file is part of fwdpy11.
//
// fwdpy11 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// fwdpy11 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with fwdpy11. If not, see <http://www.gnu.org/licenses/>.
//

#pragma once

#include <fwdpy11/types/DiploidPopulation.hpp>
#include <fwdpy11/genetic_value_data/genetic_value_data.hpp>

namespace fwdpy11
{
struct DiploidGeneticValueCalculation
{
virtual ~DiploidGeneticValueCalculation() = default;

virtual double calculate_gvalue(const DiploidGeneticValueData data) = 0;
virtual void update(const DiploidPopulation& pop) = 0;
virtual std::size_t ndim() const = 0;
virtual std::shared_ptr<DiploidGeneticValueCalculation> clone() const = 0;
};
}
4 changes: 2 additions & 2 deletions lib/evolve_discrete_demes/evolvets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ evolve_with_tree_sequences(
}
pop.record_ancient_samples(individuals);
pop.update_ancient_sample_genetic_value_matrix(
individuals, genetics.gvalue[0]->total_dim);
individuals, genetics.gvalue[0]->total_dim());
track_ancestral_counts(individuals, &last_preserved_generation,
last_preserved_generation_counts, pop);
}
Expand Down Expand Up @@ -654,7 +654,7 @@ evolve_with_tree_sequences(
{
pop.record_ancient_samples(sr.samples);
pop.update_ancient_sample_genetic_value_matrix(
sr.samples, genetics.gvalue[0]->total_dim);
sr.samples, genetics.gvalue[0]->total_dim());
track_ancestral_counts(sr.samples, &last_preserved_generation,
last_preserved_generation_counts, pop);
// Finally, clear the input
Expand Down
Loading