WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
spy.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
6
7#include <stdint.h>
8
9#include <bit>
10#include <fstream>
11#include <string>
12#include <string_view>
13
14#include <Eigen/SparseCore>
15#include <wpi/util/bit.hpp>
16
17namespace slp {
18
19/// Writes the sparsity pattern of a sparse matrix to a file.
20///
21/// Each file represents the sparsity pattern of one matrix over time. <a
22/// href="https://github.com/SleipnirGroup/Sleipnir/blob/main/tools/spy.py">spy.py</a>
23/// can display it as an animation.
24///
25/// The file starts with the following header:
26/// <ol>
27/// <li>Plot title (length as a little-endian int32, then characters)</li>
28/// <li>Row label (length as a little-endian int32, then characters)</li>
29/// <li>Column label (length as a little-endian int32, then characters)</li>
30/// </ol>
31///
32/// Then, each sparsity pattern starts with:
33/// <ol>
34/// <li>Number of coordinates as a little-endian int32</li>
35/// </ol>
36///
37/// followed by that many coordinates in the following format:
38/// <ol>
39/// <li>Row index as a little-endian int32</li>
40/// <li>Column index as a little-endian int32</li>
41/// <li>Sign as a character ('+' for positive, '-' for negative, or '0' for
42/// zero)</li>
43/// </ol>
44///
45/// @tparam Scalar Scalar type.
46template <typename Scalar>
47class Spy {
48 public:
49 /// Constructs a Spy instance.
50 ///
51 /// @param filename The filename.
52 /// @param title Plot title.
53 /// @param row_label Row label.
54 /// @param col_label Column label.
55 /// @param rows The sparse matrix's number of rows.
56 /// @param cols The sparse matrix's number of columns.
57 Spy(std::string_view filename, std::string_view title,
58 std::string_view row_label, std::string_view col_label, int rows,
59 int cols)
60 : m_file{std::string{filename}, std::ios::binary} {
61 // Write title
62 write32le(title.size());
63 m_file.write(title.data(), title.size());
64
65 // Write row label
66 write32le(row_label.size());
67 m_file.write(row_label.data(), row_label.size());
68
69 // Write column label
70 write32le(col_label.size());
71 m_file.write(col_label.data(), col_label.size());
72
73 // Write row and column counts
74 write32le(rows);
75 write32le(cols);
76 }
77
78 /// Adds a matrix to the file.
79 ///
80 /// @param mat The matrix.
81 void add(const Eigen::SparseMatrix<Scalar>& mat) {
82 // Write number of coordinates
83 write32le(mat.nonZeros());
84
85 // Write coordinates
86 for (int k = 0; k < mat.outerSize(); ++k) {
87 for (typename Eigen::SparseMatrix<Scalar>::InnerIterator it{mat, k}; it;
88 ++it) {
89 write32le(it.row());
90 write32le(it.col());
91 if (it.value() > Scalar(0)) {
92 m_file << '+';
93 } else if (it.value() < Scalar(0)) {
94 m_file << '-';
95 } else {
96 m_file << '0';
97 }
98 }
99 }
100 }
101
102 private:
103 std::ofstream m_file;
104
105 /// Writes a 32-bit signed integer to the file as little-endian.
106 ///
107 /// @param num A 32-bit signed integer.
108 void write32le(int32_t num) {
109 if constexpr (std::endian::native != std::endian::little) {
110 num = wpi::util::byteswap(num);
111 }
112 m_file.write(reinterpret_cast<char*>(&num), sizeof(num));
113 }
114};
115
116} // namespace slp
117
118#endif
Spy(std::string_view filename, std::string_view title, std::string_view row_label, std::string_view col_label, int rows, int cols)
Constructs a Spy instance.
Definition spy.hpp:57
void add(const Eigen::SparseMatrix< Scalar > &mat)
Adds a matrix to the file.
Definition spy.hpp:81
Definition expression_graph.hpp:11
Definition StringMap.hpp:773
@ string
Definition ranges.h:31