json

package
v0.0.0-...-dbea759 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2018 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package json provides functions that marshall/unmarshall a tree.Tree as/from JSON

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalJSONContinuousCriterion

func MarshalJSONContinuousCriterion(cfc feature.ContinuousCriterion) ([]byte, error)

MarshalJSONContinuousCriterion takes a feature.ContinuousCriterion and returns a serialization of it into JSON or an error. The serialization is a JSON object with the following fields: * "type": a string set to "continuous" * "feature": a string set to the name of the feature of the criterion * "a": a number specifying where the interval of the criterion starts or the string "-Inf" if it has no finite start. * "b": a number specifying where the interval of the criterion ends or the string "+Inf" if it has no finite end.

func MarshalJSONCriterion

func MarshalJSONCriterion(fc feature.Criterion) ([]byte, error)

MarshalJSONCriterion takes a feature.Criterion and returns a slice of bytes containing its serialization to JSON. It uses the MarshalJSONContinuousCriterion, MarshalJSONDiscreteCriterion and MarshalJSONUndefinedCriterion functions to serialize a feature.ContinuousCriterion, a feature.DiscreteCriterion or a feature.UndefinedCriterion respectively. It returns an error if the feature.Criterion is not one of these or if there is an error during the serialization.

func MarshalJSONDiscreteCriterion

func MarshalJSONDiscreteCriterion(dfc feature.DiscreteCriterion) ([]byte, error)

MarshalJSONDiscreteCriterion takes a feature.DiscreteCriterion and returns a serialization of it into JSON or an error. The serialization is a JSON object with the following fields: * "type": a string set to "discrete" * "feature": a string set to the name of the feature of the criterion * "value": a string with the value that satisfies the criterion.

func MarshalJSONNode

func MarshalJSONNode(n *tree.Node) ([]byte, error)

MarshalJSONNode returns a slice of bytes with the node serialized to JSON and an error. A node serialization includes the following properties:

  • "id": a string with the id of the node
  • "parentId": a string with the id of the parent of the node
  • "prediction": the prediction of the classFeature at this point in the tree
  • "subtreeIds": an array with the ids of the nodes opening the subtrees under this node.
  • "criterion": the feature criterion for the node, that is, the constraint on samples that distinguish it from its sibling nodes.
  • "feature": the feature on which the subtree nodes have a constraint, that is, the feature that is dividing the data

func MarshalJSONUndefinedCriterion

func MarshalJSONUndefinedCriterion(u feature.UndefinedCriterion) ([]byte, error)

MarshalJSONUndefinedCriterion takes a feature.UndefinedCriterion and returns a serialization of it into JSON or an error. The serialization is a JSON object with the following fields: * "type": a string set to "undefined" * "feature": a string set to the name of the feature of the criterion

func ReadJSONTree

func ReadJSONTree(ctx context.Context, t *tree.Tree, features []feature.Feature, r io.Reader) error

ReadJSONTree takes a context.Context, a pointer to a tree.Tree and an io.Reader and unmarshals the contents of the io.Reader onto the given tree. A tree is expected to be a JSON object with the following fields:

  • "rootID": a string with the ID of the node at the root of the tree
  • "classFeature": a string with the name of the feature the tree predicts
  • "nodes": an array containing the nodes that can be traversed on the tree unmarshalled by UnmarshalJSONNodeWithFeatures.

An error is returned if the JSON cannot be read from the io.Reader or unmarshalled onto the tree.

func UnmarshalJSONCriterion

func UnmarshalJSONCriterion(b []byte, features []feature.Feature) (feature.Criterion, error)

UnmarshalJSONCriterion takes a slice of bytes with a JSON-marshalled Criterion and a slice of features and returns the Criterion or an error. The slice of features should include exactly one feature with the serialized criterion's feature and for continuous and discrete criteria, the feature should be of the correspoding feature type, otherwise an error is returned.

func UnmarshalJSONNodeWithFeatures

func UnmarshalJSONNodeWithFeatures(n *tree.Node, b []byte, features []feature.Feature) error

UnmarshalJSONNodeWithFeatures takes a tree.Node, slice of bytes containing a serialized node and a slice with the available features and loads the serialized data into the given node. The slice of bytes is expected to have the node serialized in JSON format as generated by MarshalJSONNode.

func UnmarshalJSONPrediction

func UnmarshalJSONPrediction(b []byte) (*tree.Prediction, error)

UnmarshalJSONPrediction takes a slice of bytes and returns a pointer to a new tree.Prediction with the data from the slice unmarshalled into it or an error. The slice of bytes is expected to contain a JSON object with the following fields: * "probabilities": a JSON object with string keys (values) and numeric (float64) values (probability of that value) * "weight": a number (integer) corresponding to the number of samples in the set from which the prediction was made.

func WriteJSONTree

func WriteJSONTree(ctx context.Context, t *tree.Tree, w io.Writer) error

WriteJSONTree takes a context.Context, a pointer to a tree.Tree and an io.Writer and serializes the given tree as JSON onto the io.Writer. A tree is serialized as a JSON object with the following fields:

  • "rootID": a string with the ID of the node at the root of the tree
  • "classFeature": a string with the name of the feature the tree predicts
  • "nodes": an array containing the nodes that can be traversed on the tree serialized by MarshalJSONNode.

An error is returned if the tree cannot be traversed, serialized or written onto the io.Writer.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL