Skip to content

Commit

Permalink
Feature/metadata (#21)
Browse files Browse the repository at this point in the history
* add graph metadata object

* skip networkx tests if not available

* add python metadata interface

* add node and edge metadata

* use nodesymbol as primary arg type and add implicit conversion

* add node and edge metadata bindings
  • Loading branch information
nathanhhughes committed Dec 12, 2024
1 parent 7f3dc20 commit 471e28b
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 74 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[flake8]
max-line-length = 88
extend-ignore = E203, W503
per-file-ignores =
*__init__.py:F401,F403
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.14)
project(spark_dsg VERSION 1.0.5)
project(spark_dsg VERSION 1.0.6)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
32 changes: 18 additions & 14 deletions include/spark_dsg/dynamic_scene_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* -------------------------------------------------------------------------- */
#pragma once
#include <Eigen/Core>
#include <nlohmann/json.hpp>
#include <type_traits>

#include "spark_dsg/dynamic_scene_graph_layer.h"
Expand Down Expand Up @@ -410,20 +411,20 @@ class DynamicSceneGraph {

/**
* @brief Get number of static edges in the graph
* @return number of static edges in the graph
* @return Number of static edges in the graph
*/
size_t numStaticEdges() const;

/**
* @brief Get number of dynamic edges in the graph
* @return number of dynamic edges in the graph
* @return Number of dynamic edges in the graph
*/
size_t numDynamicEdges() const;

/**
* @brief Get whether or not the scene graph is empty
* @note the scene graph invariants make it so only nodes have to be checked
* @return true if the scene graph is empty
* @note The scene graph invariants make it so only nodes have to be checked
* @return True if the scene graph is empty
*/
bool empty() const;

Expand All @@ -435,10 +436,10 @@ class DynamicSceneGraph {
Eigen::Vector3d getPosition(NodeId node) const;

/**
* @brief merge two nodes
* @param node_from node to remove
* @param node_to node to merge to
* @returns true if operation succeeded
* @brief Merge two nodes
* @param node_from Node to remove
* @param node_to Node to merge to
* @returns True if operation succeeded
*/
bool mergeNodes(NodeId node_from, NodeId node_to);

Expand All @@ -457,7 +458,7 @@ class DynamicSceneGraph {
* @note Will add the nodes and edges not previously added in current graph
* @param other other graph to update from
* @param config Configuration controlling merge behavior
* @returns true if merge was successful
* @returns True if merge was successful
*/
bool mergeGraph(const DynamicSceneGraph& other, const GraphMergeConfig& config = {});

Expand All @@ -471,7 +472,7 @@ class DynamicSceneGraph {
/**
* @brief Get all new nodes in the graph
* @param clear_new Clear new status for all new nodes up until this point
* @returns list of all new nodes
* @returns List of all new nodes
*/
std::vector<NodeId> getNewNodes(bool clear_new = false);

Expand All @@ -490,12 +491,12 @@ class DynamicSceneGraph {
std::vector<EdgeKey> getNewEdges(bool clear_new = false);

/**
* @brief track which edges get used during a serialization update
* @brief Track which edges get used during a serialization update
*/
void markEdgesAsStale();

/**
* @brief remove edges that do not appear in serialization update
* @brief Remove edges that do not appear in serialization update
*/
void removeAllStaleEdges();

Expand All @@ -514,7 +515,7 @@ class DynamicSceneGraph {
void save(std::string filepath, bool include_mesh = true) const;

/**
* @brief parse graph from binary or JSON file
* @brief Parse graph from binary or JSON file
* @param filepath Complete path to file to read, including extension.
* @returns Resulting parsed scene graph
*/
Expand All @@ -526,9 +527,12 @@ class DynamicSceneGraph {

std::shared_ptr<Mesh> mesh() const;

//! current static layer ids in the graph
//! Current static layer ids in the graph
const LayerIds layer_ids;

//! Any extra information about the graph
nlohmann::json metadata;

protected:
BaseLayer& layerFromKey(const LayerKey& key);

Expand Down
3 changes: 3 additions & 0 deletions include/spark_dsg/edge_attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* -------------------------------------------------------------------------- */
#pragma once
#include <memory>
#include <nlohmann/json.hpp>
#include <ostream>

#include "spark_dsg/serialization/attribute_registry.h"
Expand Down Expand Up @@ -76,6 +77,8 @@ struct EdgeAttributes {
bool weighted;
//! the weight of the edge
double weight;
//! Arbitrary metadata about the edge
nlohmann::json metadata;

/**
* @brief output attribute information
Expand Down
3 changes: 3 additions & 0 deletions include/spark_dsg/node_attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <list>
#include <map>
#include <memory>
#include <nlohmann/json.hpp>
#include <optional>
#include <ostream>
#include <string>
Expand Down Expand Up @@ -113,6 +114,8 @@ struct NodeAttributes {
bool is_active;
//! whether the node was observed by Hydra, or added as a prediction
bool is_predicted;
//! Arbitrary node metadata
nlohmann::json metadata;

/**
* @brief output attribute information
Expand Down
20 changes: 20 additions & 0 deletions include/spark_dsg/serialization/attribute_serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@

#include <map>
#include <nlohmann/json.hpp>
#include <sstream>

#include "spark_dsg/serialization/attribute_registry.h"
#include "spark_dsg/serialization/binary_serialization.h"
#include "spark_dsg/serialization/versioning.h"

namespace spark_dsg::serialization {

Expand Down Expand Up @@ -170,6 +172,7 @@ void Visitor::to(nlohmann::json& record, const Attrs& attrs) {
visitor.type_ = Type::JSON_WRITE;
visitor.impl_ = std::make_unique<JsonWriter>(&record);
record["type"] = attrs.registration().name;
record["metadata"] = attrs.metadata;
attrs.serialization_info();
visitor.impl_.reset();
}
Expand All @@ -180,6 +183,12 @@ void Visitor::to(BinarySerializer& serializer, const Attrs& attrs) {
visitor.type_ = Type::BINARY_WRITE;
visitor.impl_ = std::make_unique<BinaryWriter>(&serializer);
serializer.write(attrs.registration().type_id);

// serialize metadata
std::stringstream ss;
ss << attrs.metadata;
serializer.write(ss.str());

attrs.serialization_info();
visitor.impl_.reset();
}
Expand All @@ -196,6 +205,10 @@ std::unique_ptr<Attrs> Visitor::from(const AttributeFactory<Attrs>& factory,
return nullptr;
}

if (record.contains("metadata")) {
attrs->metadata = record["metadata"];
}

attrs->serialization_info();
visitor.impl_.reset();
return attrs;
Expand All @@ -215,6 +228,13 @@ std::unique_ptr<Attrs> Visitor::from(const AttributeFactory<Attrs>& factory,
return nullptr;
}

const auto& header = io::GlobalInfo::loadedHeader();
if (header.version >= io::Version(1, 0, 6)) {
std::string metadata_json;
deserializer.read(metadata_json);
attrs->metadata = nlohmann::json::parse(metadata_json);
}

attrs->serialization_info();
visitor.impl_.reset();
return attrs;
Expand Down
2 changes: 1 addition & 1 deletion package.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<package format="2">
<name>spark_dsg</name>
<version>1.0.5</version>
<version>1.0.6</version>
<description>Dynamic Scene Graph (DSG) type definitions and core library code</description>

<author email="[email protected]">Nathan Hughes</author>
Expand Down
11 changes: 6 additions & 5 deletions python/bindings/include/spark_dsg/python/python_layer_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* -------------------------------------------------------------------------- */
#pragma once
#include <spark_dsg/dynamic_scene_graph.h>
#include <spark_dsg/node_symbol.h>

#include "spark_dsg/python/scene_graph_iterators.h"

Expand All @@ -46,11 +47,11 @@ class LayerView {
EdgeIter edges() const;
size_t numNodes() const;
size_t numEdges() const;
bool hasNode(NodeId node_id) const;
bool hasEdge(NodeId source, NodeId target) const;
const SceneGraphNode& getNode(NodeId node_id) const;
const SceneGraphEdge& getEdge(NodeId source, NodeId target) const;
Eigen::Vector3d getPosition(NodeId node_id) const;
bool hasNode(NodeSymbol node_id) const;
bool hasEdge(NodeSymbol source, NodeSymbol target) const;
const SceneGraphNode& getNode(NodeSymbol node_id) const;
const SceneGraphEdge& getEdge(NodeSymbol source, NodeSymbol target) const;
Eigen::Vector3d getPosition(NodeSymbol node_id) const;

const LayerId id;

Expand Down
10 changes: 5 additions & 5 deletions python/bindings/src/python_layer_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ size_t LayerView::numNodes() const { return layer_ref_.numNodes(); }

size_t LayerView::numEdges() const { return layer_ref_.numEdges(); }

bool LayerView::hasNode(NodeId node_id) const { return layer_ref_.hasNode(node_id); }
bool LayerView::hasNode(NodeSymbol node_id) const { return layer_ref_.hasNode(node_id); }

bool LayerView::hasEdge(NodeId source, NodeId target) const {
bool LayerView::hasEdge(NodeSymbol source, NodeSymbol target) const {
return layer_ref_.hasEdge(source, target);
}

const SceneGraphNode& LayerView::getNode(NodeId node_id) const {
const SceneGraphNode& LayerView::getNode(NodeSymbol node_id) const {
return layer_ref_.getNode(node_id);
}

const SceneGraphEdge& LayerView::getEdge(NodeId source, NodeId target) const {
const SceneGraphEdge& LayerView::getEdge(NodeSymbol source, NodeSymbol target) const {
return layer_ref_.getEdge(source, target);
}

Eigen::Vector3d LayerView::getPosition(NodeId node_id) const {
Eigen::Vector3d LayerView::getPosition(NodeSymbol node_id) const {
return layer_ref_.getNode(node_id).attributes().position;
}

Expand Down
Loading

0 comments on commit 471e28b

Please sign in to comment.