Skip to content

Commit ea88ec9

Browse files
committed
add __repr__s to processess and adjacencies
1 parent 83d5acb commit ea88ec9

2 files changed

Lines changed: 95 additions & 6 deletions

File tree

python/src/processes.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#include <format>
12
#include <random>
23

34
#include <nanobind/nanobind.h>
5+
#include <nanobind/stl/string.h>
46

57
#include <reticula/processes.hpp>
68

@@ -11,6 +13,12 @@ void define_processes(nanobind::module_& m) {
1113
nanobind::init<double, double>(), nanobind::arg("exponent"),
1214
nanobind::arg("mean"),
1315
nanobind::call_guard<nanobind::gil_scoped_release>())
16+
.def(
17+
"__repr__",
18+
[](const power_law& pl) {
19+
return std::format(
20+
"reticula.power_law(exponent={}, mean={})", pl.exponent(), pl.mean());
21+
})
1422
.def(
1523
"__call__", [](const power_law& pl, std::mt19937_64& g) { return pl(g); },
1624
nanobind::arg("generator"),
@@ -30,6 +38,13 @@ void define_processes(nanobind::module_& m) {
3038
nanobind::init<double, double>(), nanobind::arg("exponent"),
3139
nanobind::arg("mean"),
3240
nanobind::call_guard<nanobind::gil_scoped_release>())
41+
.def(
42+
"__repr__",
43+
[](const residual_power_law& rpl) {
44+
return std::format(
45+
"reticula.residual_power_law(exponent={}, mean={})", rpl.exponent(),
46+
rpl.mean());
47+
})
3348
.def(
3449
"__call__",
3550
[](const residual_power_law& rpl, std::mt19937_64& g) { return rpl(g); },
@@ -52,6 +67,14 @@ void define_processes(nanobind::module_& m) {
5267
nanobind::arg("alpha"), nanobind::arg("theta"),
5368
nanobind::arg("phi") = 0.0,
5469
nanobind::call_guard<nanobind::gil_scoped_release>())
70+
.def(
71+
"__repr__",
72+
[](const hawkes_univariate_exponential& hue) {
73+
return std::format(
74+
"reticula.hawkes_univariate_exponential(mu={}, alpha={}, theta={}, "
75+
"phi={})",
76+
hue.mu(), hue.alpha(), hue.theta(), hue.phi());
77+
})
5578
.def(
5679
"__call__",
5780
[](hawkes_univariate_exponential& hue, std::mt19937_64& g) {
@@ -76,6 +99,11 @@ void define_processes(nanobind::module_& m) {
7699
.def(
77100
nanobind::init<double>(), nanobind::arg("mean"),
78101
nanobind::call_guard<nanobind::gil_scoped_release>())
102+
.def(
103+
"__repr__",
104+
[](const delta_distribution& dd) {
105+
return std::format("reticula.delta_distribution(mean={})", dd.mean());
106+
})
79107
.def(
80108
"__call__",
81109
[](const delta_distribution& dd, std::mt19937_64& g) { return dd(g); },
@@ -90,6 +118,12 @@ void define_processes(nanobind::module_& m) {
90118
.def(
91119
nanobind::init<double>(), nanobind::arg("lmbda"),
92120
nanobind::call_guard<nanobind::gil_scoped_release>())
121+
.def(
122+
"__repr__",
123+
[](const std::exponential_distribution<double>& ed) {
124+
return std::format(
125+
"reticula.exponential_distribution(lmbda={})", ed.lambda());
126+
})
93127
.def(
94128
"__call__",
95129
[](std::exponential_distribution<double>& ed, std::mt19937_64& g) {
@@ -106,6 +140,12 @@ void define_processes(nanobind::module_& m) {
106140
.def(
107141
nanobind::init<double, double>(), nanobind::arg("a"), nanobind::arg("b"),
108142
nanobind::call_guard<nanobind::gil_scoped_release>())
143+
.def(
144+
"__repr__",
145+
[](const std::uniform_real_distribution<double>& urd) {
146+
return std::format(
147+
"reticula.uniform_real_distribution(a={}, b={})", urd.a(), urd.b());
148+
})
109149
.def(
110150
"__call__",
111151
[](std::uniform_real_distribution<double>& urd, std::mt19937_64& g) {
@@ -125,6 +165,12 @@ void define_processes(nanobind::module_& m) {
125165
.def(
126166
nanobind::init<double, double>(), nanobind::arg("m"), nanobind::arg("s"),
127167
nanobind::call_guard<nanobind::gil_scoped_release>())
168+
.def(
169+
"__repr__",
170+
[](const std::lognormal_distribution<double>& lnd) {
171+
return std::format(
172+
"reticula.lognormal_distribution(m={}, s={})", lnd.m(), lnd.s());
173+
})
128174
.def(
129175
"__call__",
130176
[](std::lognormal_distribution<double>& lnd, std::mt19937_64& g) {
@@ -144,6 +190,13 @@ void define_processes(nanobind::module_& m) {
144190
nanobind::init<double, double>(), nanobind::arg("alpha"),
145191
nanobind::arg("beta"),
146192
nanobind::call_guard<nanobind::gil_scoped_release>())
193+
.def(
194+
"__repr__",
195+
[](const std::gamma_distribution<double>& gd) {
196+
return std::format(
197+
"reticula.gamma_distribution(alpha={}, beta={})", gd.alpha(),
198+
gd.beta());
199+
})
147200
.def(
148201
"__call__",
149202
[](std::gamma_distribution<double>& gd, std::mt19937_64& g) {
@@ -162,6 +215,12 @@ void define_processes(nanobind::module_& m) {
162215
.def(
163216
nanobind::init<double, double>(), nanobind::arg("a"), nanobind::arg("b"),
164217
nanobind::call_guard<nanobind::gil_scoped_release>())
218+
.def(
219+
"__repr__",
220+
[](const std::weibull_distribution<double>& wd) {
221+
return std::format(
222+
"reticula.weibull_distribution(a={}, b={})", wd.a(), wd.b());
223+
})
165224
.def(
166225
"__call__",
167226
[](std::weibull_distribution<double>& wd, std::mt19937_64& g) {
@@ -181,6 +240,11 @@ void define_processes(nanobind::module_& m) {
181240
.def(
182241
nanobind::init<double>(), nanobind::arg("p"),
183242
nanobind::call_guard<nanobind::gil_scoped_release>())
243+
.def(
244+
"__repr__",
245+
[](const std::geometric_distribution<std::uint64_t>& gd) {
246+
return std::format("reticula.geometric_distribution(p={})", gd.p());
247+
})
184248
.def(
185249
"__call__",
186250
[](std::geometric_distribution<std::uint64_t>& gd, std::mt19937_64& g) {

python/src/temporal_adjacency.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <nanobind/nanobind.h>
2+
#include <nanobind/stl/string.h>
23

34
#include <reticula/edges.hpp>
45
#include <reticula/temporal_adjacency.hpp>
@@ -37,12 +38,16 @@ void define_adjacency_linger(nanobind::class_<AdjT>& adj) {
3738

3839
void define_adjacency(nanobind::module_& m) {
3940
auto adj = m.def_submodule("adjacency");
40-
auto simple = nanobind::class_<adjacency::simple>(adj, "simple")
41-
.def(nanobind::init<>())
42-
.def(
43-
"maximum_linger", &adjacency::simple::maximum_linger,
44-
nanobind::arg("vertex"),
45-
nanobind::call_guard<nanobind::gil_scoped_release>());
41+
auto simple =
42+
nanobind::class_<adjacency::simple>(adj, "simple")
43+
.def(nanobind::init<>())
44+
.def(
45+
"__repr__",
46+
[](const adjacency::simple&) { return "reticula.adjacency.simple()"; })
47+
.def(
48+
"maximum_linger", &adjacency::simple::maximum_linger,
49+
nanobind::arg("vertex"),
50+
nanobind::call_guard<nanobind::gil_scoped_release>());
4651
define_adjacency_linger(simple);
4752

4853
auto lwt =
@@ -51,6 +56,12 @@ void define_adjacency(nanobind::module_& m) {
5156
.def(
5257
nanobind::init<double>(), nanobind::arg("dt"),
5358
nanobind::call_guard<nanobind::gil_scoped_release>())
59+
.def(
60+
"__repr__",
61+
[](const adjacency::limited_waiting_time& lwt) {
62+
return std::format(
63+
"reticula.adjacency.limited_waiting_time(dt={})", lwt.dt());
64+
})
5465
.def(
5566
"dt", &adjacency::limited_waiting_time::dt,
5667
nanobind::call_guard<nanobind::gil_scoped_release>())
@@ -65,6 +76,13 @@ void define_adjacency(nanobind::module_& m) {
6576
nanobind::init<double, std::size_t>(), nanobind::arg("rate"),
6677
nanobind::arg("seed") = 0,
6778
nanobind::call_guard<nanobind::gil_scoped_release>())
79+
.def(
80+
"__repr__",
81+
[](const adjacency::exponential& exp) {
82+
return std::format(
83+
"reticula.adjacency.exponential(rate={}, seed={})",
84+
exp.rate(), exp.seed());
85+
})
6886
.def(
6987
"rate", &adjacency::exponential::rate,
7088
nanobind::call_guard<nanobind::gil_scoped_release>())
@@ -82,6 +100,13 @@ void define_adjacency(nanobind::module_& m) {
82100
nanobind::init<double, std::size_t>(), nanobind::arg("p"),
83101
nanobind::arg("seed") = 0,
84102
nanobind::call_guard<nanobind::gil_scoped_release>())
103+
.def(
104+
"__repr__",
105+
[](const adjacency::geometric& geom) {
106+
return std::format(
107+
"reticula.adjacency.geometric(p={}, seed={})", geom.p(),
108+
geom.seed());
109+
})
85110
.def(
86111
"p", &adjacency::geometric::p,
87112
nanobind::call_guard<nanobind::gil_scoped_release>())

0 commit comments

Comments
 (0)