Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
cozzyd committed Mar 4, 2024
2 parents da75530 + 7c43744 commit fae872d
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 102 deletions.
9 changes: 1 addition & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,8 @@ endforeach(subdir ${SUBDIRS})
export(TARGETS ${SPT3G_LIBRARIES} NAMESPACE spt3g:: FILE ${CMAKE_BINARY_DIR}/cmake/Spt3gTargets.cmake)
configure_file(${CMAKE_SOURCE_DIR}/cmake/Spt3gConfig.cmake.in ${CMAKE_BINARY_DIR}/cmake/Spt3gConfig.cmake @ONLY)

# Add fetching of test data
add_custom_target(testdata COMMAND rsync -vrlpt --delete rsync://bolo.berkeley.edu/testdata ${CMAKE_BINARY_DIR}/testdata COMMENT "Rsyncing test data from bolo.berkeley.edu")

add_custom_target(fasttest COMMAND ctest -LE SLOWTEST COMMENT "Running fast test suite")

# Custom things related to testing
configure_file(${CMAKE_SOURCE_DIR}/cmake/test_env.py.in ${CMAKE_BINARY_DIR}/test_env.py @ONLY)
configure_file(${CMAKE_SOURCE_DIR}/cmake/CTestCustom.cmake.in ${CMAKE_BINARY_DIR}/CTestCustom.cmake @ONLY)

add_custom_target(fasttest COMMAND ctest -LE SLOWTEST COMMENT "Running fast test suite")
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.17)
list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")
endif(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.17)
Expand Down
1 change: 0 additions & 1 deletion cmake/CTestCustom.cmake.in

This file was deleted.

4 changes: 3 additions & 1 deletion cmake/Spt3gIncludes.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ macro(add_spt3g_library lib_name)
endmacro(add_spt3g_library lib_name)

macro(add_spt3g_test test_name)
add_test(${PROJECT}/${test_name} ${SPT3G_BUILD_DIR}/env-shell.sh ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/${test_name}.py)
add_test(${PROJECT}/${test_name} ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/${test_name}.py)

set(extra_macro_args ${ARGN})
list(LENGTH extra_macro_args num_extra_args)
set_tests_properties(${PROJECT}/${test_name} PROPERTIES ENVIRONMENT
"PATH=${SPT3G_BUILD_DIR}/bin:$ENV{PATH};PYTHONPATH=${SPT3G_BUILD_DIR}:$ENV{PYTHONPATH};LD_LIBRARY_PATH=${SPT3G_BUILD_DIR}/spt3g:$ENV{LD_LIBRARY_PATH}")
if (${num_extra_args} GREATER 0)
list(GET extra_macro_args 0 test_labels)
set_tests_properties(${PROJECT}/${test_name} PROPERTIES LABELS ${test_labels})
Expand Down
8 changes: 0 additions & 8 deletions cmake/test_env.py.in

This file was deleted.

22 changes: 19 additions & 3 deletions core/include/core/G3PipelineInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ class G3ModulePythonArg : public G3FrameObject {
bool operator ==(const G3ModulePythonArg &other) const { return other.value == value; };
};

class G3ModuleArg : public G3FrameObject {
public:
std::string repr;
G3FrameObjectPtr object;

G3ModuleArg(const std::string &r) : repr(r) {}
G3ModuleArg(const std::string &r, G3FrameObjectPtr obj) :
repr(r), object(obj) {}
G3ModuleArg() {}

template <class A> void serialize(A &ar, unsigned v);

std::string Description() const;

bool operator ==(const G3ModuleArg &other) const { return other.repr == repr; };
};

class G3ModuleConfig : public G3FrameObject {
public:
std::string modname;
Expand All @@ -28,7 +45,6 @@ class G3ModuleConfig : public G3FrameObject {
template <class A> void save(A &ar, unsigned v) const;

std::string Description() const;
std::string Summary() const;

bool operator ==(const G3ModuleConfig &) const;

Expand Down Expand Up @@ -60,15 +76,15 @@ class G3PipelineInfo : public G3FrameObject {
SET_LOGGER("G3PipelineInfo");
};

G3_POINTERS(G3ModulePythonArg);
G3_POINTERS(G3ModuleArg);
G3_POINTERS(G3ModuleConfig);
G3_POINTERS(G3PipelineInfo);

namespace cereal {
template <class A> struct specialize<A, G3ModuleConfig, cereal::specialization::member_load_save> {};
}

G3_SERIALIZABLE(G3ModulePythonArg, 1);
G3_SERIALIZABLE(G3ModuleArg, 1);
G3_SERIALIZABLE(G3ModuleConfig, 2);
G3_SERIALIZABLE(G3PipelineInfo, 2);

Expand Down
193 changes: 116 additions & 77 deletions core/src/G3PipelineInfo.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,43 @@
#include <serialization.h>
#include <G3PipelineInfo.h>

template <class A> void G3ModulePythonArg::serialize(A &ar, unsigned v)
namespace bp = boost::python;

template <class A> void G3ModuleArg::serialize(A &ar, unsigned v)
{
G3_CHECK_VERSION(v);

ar & cereal::make_nvp("G3FrameObject",
cereal::base_class<G3FrameObject>(this));
ar & cereal::make_nvp("value", value);
ar & cereal::make_nvp("repr", repr);
ar & cereal::make_nvp("obj", object);
}

std::string
G3ModuleArg::Description() const {
std::string rv = "G3ModuleArg(";
if (repr.size())
rv += repr;
else if (!!object)
rv += object->Summary();
rv += ")";
return rv;
}

static std::string inline object_repr(bp::object obj)
{
return bp::extract<std::string>(obj.attr("__repr__")());
}

static std::string
G3ModuleArg_repr(const G3ModuleArg &arg)
{
// Some frame objects (e.g. G3Vectors) have repr only
// defined properly via the python interface.
if (!arg.repr.size() && !!arg.object)
return object_repr(bp::object(arg.object));
return arg.repr;
}

template <class A> void G3ModuleConfig::save(A &ar, unsigned v) const
{
Expand Down Expand Up @@ -50,33 +77,35 @@ template <class A> void G3ModuleConfig::load(A &ar, unsigned v)
if (is_frameobject) {
G3FrameObjectPtr fo;
ar >> cereal::make_nvp("value", fo);
config[key] = fo;
config[key] = G3ModuleArg("", fo);
} else {
std::string repr;
ar >> cereal::make_nvp("value", repr);
config[key] = boost::make_shared<G3ModulePythonArg>(repr);
config[key] = G3ModuleArg(repr);
}
}
}

std::string
G3ModuleConfig::Summary() const
G3ModuleConfig_repr(const G3ModuleConfig &mc)
{
std::string rv = "pipe.Add(" + modname;
for (auto i : config) {
rv += ", " + i.first + "=" + i.second->Summary();
}
std::string rv = "pipe.Add(" + mc.modname;
for (auto i : mc.config)
rv += ", " + i.first + "=" + G3ModuleArg_repr(i.second);

if (instancename.size() != 0 && instancename != modname)
rv += ", name=" + instancename;
if (mc.instancename.size() != 0 && mc.instancename != mc.modname)
rv += ", name=" + mc.instancename;
rv += ")";
return rv;
}

std::string
G3ModuleConfig::Description() const
{
return Summary();
std::ostringstream rv;
rv << "G3ModuleConfig(" << modname;
rv << ", " << config.size() << " arguments)";
return rv.str();
}

bool
Expand All @@ -86,6 +115,67 @@ G3ModuleConfig::operator == (const G3ModuleConfig &b) const
(b.config == config);
}


static bp::object
G3ModuleConfig_get(const G3ModuleConfig &mc, std::string key)
{
auto item = mc.config.find(key);
if (item == mc.config.end()) {
PyErr_SetString(PyExc_KeyError, key.c_str());
bp::throw_error_already_set();
}

auto arg = item->second;
if (!!arg.object)
return bp::object(arg.object);

bp::object main = bp::import("__main__");
bp::dict global = bp::dict(main.attr("__dict__"));
global["__main__"] = main;

try {
return bp::eval(bp::str(arg.repr), global, global);
} catch (const bp::error_already_set& e) {
PyErr_Clear();
return bp::object(arg.repr);
}
}

static void
G3ModuleConfig_set(G3ModuleConfig &mc, std::string key, bp::object obj)
{
std::string repr = object_repr(obj);

if (!bp::extract<G3FrameObjectPtr>(obj).check()) {
mc.config[key] = G3ModuleArg(repr);
return;
}

mc.config[key] = G3ModuleArg(repr, bp::extract<G3FrameObjectPtr>(obj)());
}

static bp::list
G3ModuleConfig_keys(const G3ModuleConfig &mc)
{
bp::list keys;

for (auto i: mc.config)
keys.append(i.first);

return keys;
}

static bp::list
G3ModuleConfig_values(const G3ModuleConfig &mc)
{
bp::list values;

for (auto i: mc.config)
values.append(G3ModuleConfig_get(mc, i.first));

return values;
}

template <class A> void G3PipelineInfo::serialize(A &ar, unsigned v)
{
G3_CHECK_VERSION(v);
Expand Down Expand Up @@ -132,7 +222,7 @@ G3PipelineInfo::Description() const
rv << "Run by: " << user << " on " << hostname << "\n";

rv << modules.size();
rv << " modules\n";
rv << " modules";

return rv.str();
}
Expand All @@ -141,87 +231,35 @@ static std::string
G3PipelineInfo_repr(const G3PipelineInfo &pi)
{
std::string rv;
rv = "pipe = spt3g.core.G3Pipeline()\n";
rv = "pipe = spt3g.core.G3Pipeline()";

for (auto i : pi.modules) {
rv += i.Summary();
rv += "\n";
}
for (auto i : pi.modules)
rv += "\n" + G3ModuleConfig_repr(i);
return rv;
}

namespace bp = boost::python;

static bp::object
G3ModuleConfig_get(G3ModuleConfigConstPtr mc, std::string key)
{
auto item = mc->config.find(key);
if (item == mc->config.end()) {
PyErr_SetString(PyExc_KeyError, key.c_str());
bp::throw_error_already_set();
}

G3ModulePythonArgConstPtr arg =
boost::dynamic_pointer_cast<const G3ModulePythonArg>(item->second);
if (!arg)
return bp::object(item->second);

bp::object main = bp::import("__main__");
bp::object global = main.attr("__dict__");

try {
return bp::eval(bp::str(arg->value), global, global);
} catch (const bp::error_already_set& e) {
PyErr_Clear();
return bp::object(arg->value);
}
}

static void
G3ModuleConfig_set(G3ModuleConfigPtr mc, std::string key, bp::object obj)
{
if (bp::extract<G3FrameObjectPtr>(obj).check()) {
mc->config[key] = bp::extract<G3FrameObjectPtr>(obj)();
return;
}

std::string repr = bp::extract<std::string>(obj.attr("__repr__")());
mc->config[key] = boost::make_shared<G3ModulePythonArg>(repr);
}

static bp::list
G3ModuleConfig_keys(G3ModuleConfigConstPtr mc)
{
bp::list keys;

for (auto i: mc->config)
keys.append(i.first);

return keys;
}

static bp::list
G3ModuleConfig_values(G3ModuleConfigConstPtr mc)
G3PipelineInfo_run(const G3PipelineInfo &pi)
{
bp::list values;
bp::object main = bp::import("__main__");
bp::dict global = bp::dict(main.attr("__dict__"));
global["__main__"] = main;

for (auto i: mc->config)
values.append(G3ModuleConfig_get(mc, i.first));
std::string pipe = G3PipelineInfo_repr(pi);
pipe += "\npipe.Run()";

return values;
bp::exec(bp::str(pipe), global, global);
}



G3_SERIALIZABLE_CODE(G3ModulePythonArg);
G3_SERIALIZABLE_CODE(G3ModuleArg);
G3_SPLIT_SERIALIZABLE_CODE(G3ModuleConfig);
G3_SERIALIZABLE_CODE(G3PipelineInfo);

PYBINDINGS("core") {
EXPORT_FRAMEOBJECT(G3ModuleConfig, init<>(), "Stored configuration of a pipeline module or segment")
.def_readwrite("modname", &G3ModuleConfig::modname)
.def_readwrite("instancename", &G3ModuleConfig::instancename)
.def("__repr__", &G3ModuleConfig::Summary)
.def("__repr__", &G3ModuleConfig_repr)
.def("__getitem__", &G3ModuleConfig_get)
.def("__setitem__", &G3ModuleConfig_set)
.def("keys", &G3ModuleConfig_keys)
Expand All @@ -242,6 +280,7 @@ PYBINDINGS("core") {
.def_readwrite("user", &G3PipelineInfo::user)
.def_readwrite("modules", &G3PipelineInfo::modules)
.def("__repr__", &G3PipelineInfo_repr)
.def("Run", &G3PipelineInfo_run)
;
register_pointer_conversions<G3PipelineInfo>();
}
Expand Down
Loading

0 comments on commit fae872d

Please sign in to comment.