pulsatrix 1.1.0
Loading...
Searching...
No Matches
mime_bundle.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstdint>
8#include <string>
9#include <string_view>
10#include <utility>
11#include <vector>
12
13#include "pulsatrix/json.hpp"
19
20namespace pulsatrix {
21
22class Tensor;
23class CircuitGraph;
24struct Attribution;
25
28inline constexpr const char* kVegaLiteMimeType = "application/vnd.vegalite.v5+json";
29
35struct MimeBundle {
36 std::vector<std::pair<std::string, JsonValue>> entries;
37
39 void add(std::string mime, JsonValue data);
41 [[nodiscard]] const JsonValue* find(std::string_view mime) const;
43 [[nodiscard]] JsonValue to_json_value() const;
44};
45
62[[nodiscard]] MimeBundle mime_bundle_repr(const Tensor& tensor);
63[[nodiscard]] MimeBundle mime_bundle_repr(const Attribution& attribution);
64[[nodiscard]] MimeBundle mime_bundle_repr(const CircuitGraph& graph);
73[[nodiscard]] MimeBundle mime_bundle_repr(const MorrisDocument& doc);
74[[nodiscard]] MimeBundle mime_bundle_repr(const SobolDocument& doc);
81[[nodiscard]] MimeBundle mime_bundle_repr(const SeriesDocument& doc);
83[[nodiscard]] MimeBundle mime_bundle_repr(const SliceErrorReport& report);
85
92
94[[nodiscard]] std::string Base64Encode(const std::vector<uint8_t>& bytes);
95
103template <class Json>
104void to_json(Json& j, const JsonValue& value) {
105 switch (value.type()) {
107 j = nullptr;
108 return;
110 j = value.as_bool();
111 return;
113 const std::string& text = value.number_text();
114 if (text.find_first_of(".eE") == std::string::npos) {
115 try {
116 j = static_cast<std::int64_t>(value.as_int64());
117 return;
118 } catch (const std::invalid_argument&) {
119 }
120 }
121 j = value.as_double();
122 return;
123 }
125 j = value.as_string();
126 return;
128 j = Json::array();
129 for (const JsonValue& element : value.as_array()) {
130 Json converted;
131 to_json(converted, element);
132 j.push_back(std::move(converted));
133 }
134 return;
135 }
137 j = Json::object();
138 for (const auto& [key, member] : value.as_object()) {
139 Json converted;
140 to_json(converted, member);
141 j[key] = std::move(converted);
142 }
143 return;
144 }
145 }
146}
147
149template <class Json>
150void to_json(Json& j, const MimeBundle& bundle) {
151 to_json(j, bundle.to_json_value());
152}
153
154} // namespace pulsatrix
A copyable, self-contained circuit graph: every node of one forward pass with an ablation importance ...
Definition circuit_graph.hpp:78
One JSON value: null, a boolean, a number, a string, an array or an object.
Definition json.hpp:22
Type type() const
Definition json.hpp:47
const Object & as_object() const
const Array & as_array() const
const std::string & as_string() const
const std::string & number_text() const
A number's text exactly as written or parsed, for example "0.1".
int64_t as_int64() const
bool as_bool() const
double as_double() const
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Versioned JSON documents (pulsatrix.<kind>.v1) that every renderer reads.
A small strict JSON value, parser and deterministic writer (RFC 8259).
Definition acquisition_functions.hpp:16
JsonValue ToVegaLiteCircuitGraph(const CircuitGraphDocument &doc)
A Vega-Lite node-link view of a circuit graph: nodes in columns by depth (longest path from a source)...
void to_json(Json &j, const JsonValue &value)
Converts a JsonValue to another JSON library's value type by its common interface (assignment from nu...
Definition mime_bundle.hpp:104
MimeBundle mime_bundle_repr(const Tensor &tensor)
constexpr const char * kVegaLiteMimeType
The MIME type Jupyter front ends (JupyterLab, Notebook 7, VS Code) render Vega-Lite from....
Definition mime_bundle.hpp:28
std::string Base64Encode(const std::vector< uint8_t > &bytes)
Standard base64 (RFC 4648, with padding), for image/png data.
How well a model does (KS-3): a classifier's accuracy, log loss, Brier score, calibration and per-cla...
Viz documents for proteins (PLM-5): the mutation map, the sequence logo, the contact map and residue ...
The series document (VIZ-9): measured points with optional intervals, for sweeps and trade-off curves...
pulsatrix.attribution.v1: an Attribution's values, shape, method and metadata.
Definition document.hpp:58
An explanation result: the raw attribution values, the method that produced them, and any relevant me...
Definition attribution.hpp:23
pulsatrix.circuit_graph.v1: a CircuitGraph's scored nodes and weighted edges.
Definition document.hpp:135
How well a classifier does on a labeled dataset.
Definition performance.hpp:49
pulsatrix.contact_map.v1: predicted contacts between a sequence's residues, and the true ones when a ...
Definition protein_documents.hpp:105
pulsatrix.counterfactual.v1: an input, its counterfactual, and what reaching the target cost (CFS-5).
Definition document.hpp:316
pulsatrix.heatmap.v1: a row-major grid of values with optional axis labels.
Definition document.hpp:79
One displayable value in several formats: MIME type to data, richest first. Text formats (text/plain,...
Definition mime_bundle.hpp:35
JsonValue to_json_value() const
The bundle as one JSON object, {"text/plain": ..., ...}.
const JsonValue * find(std::string_view mime) const
The data for mime, or nullptr.
void add(std::string mime, JsonValue data)
std::vector< std::pair< std::string, JsonValue > > entries
Definition mime_bundle.hpp:36
pulsatrix.morris.v1: Morris screening statistics per feature (CFS-4).
Definition document.hpp:350
pulsatrix.mutation_map.v1: a score for every substitution, residue by residue, such as a model's log-...
Definition protein_documents.hpp:31
pulsatrix.partial_dependence.v1: a partial dependence curve over one feature, with the individual (IC...
Definition document.hpp:239
Persistence diagrams of one point cloud, one entry per homology dimension.
Definition topology_documents.hpp:50
pulsatrix.projection.v1: inputs placed in a few dimensions (INT-5), such as their first principal com...
Definition document.hpp:398
pulsatrix.residue_tracks.v1: per-residue signals stacked under a sequence, and annotated stretches (d...
Definition protein_documents.hpp:137
pulsatrix.sensitivity.v1: how one output moves when each input feature, alone, goes to a low and a hi...
Definition document.hpp:285
pulsatrix.sequence_logo.v1: a distribution over letters at each residue, such as what a masked langua...
Definition protein_documents.hpp:62
Definition series.hpp:50
PerSliceError()'s result.
Definition performance.hpp:155
pulsatrix.sobol.v1: first- and total-order Sobol indices per feature (CFS-4).
Definition document.hpp:365
pulsatrix.token_relevance.v1: relevance scores over the pieces of a text, in reading order....
Definition document.hpp:104
pulsatrix.training_log.v1: scalar series and the latest histogram per tag.
Definition document.hpp:168
The persistence diagram document (TDA-9): the features of each homology dimension,...