pygsti.protocols.protocol.ProtocolResultsDir#

class ProtocolResultsDir(data, protocol_results=None, children=None)#

Bases: TreeNode, MongoSerializable

Holds a dictionary of ProtocolResults objects.

It contains a ProtocolData object and is rooted at the_model corresponding node of the data-tree. It contains links to child-ProtocolResultsDir objects representing sub-directories.

This container object holds two things:

  1. A .for_protocol dictionary of ProtocolResults corresponding to different protocols (keys are protocol names).

  2. Child ProtocolResultsDir objects, obtained by indexing this object directly using the name of the sub-directory.

Create a new ProtocolResultsDir object.

Parameters:
  • data (ProtocolData) – The data from which all the Results objects in this ProtocolResultsDir are derived.

  • protocol_results (ProtocolResults or dict, optional) – An initial dictionary of ProtocolResults objects to add, or a single results object. The name(s) of the results object(s) must be used as keys (and will used as its key for a single results object). This beccomes the created object’s .for_protocol dictionary. If None, then an empty results directory is created.

  • children (dict, optional) – A dictionary of the ProtocolResultsDir objects that are sub-directories beneath this one. If None, then children are automatically created based upon the tree given by data. (To avoid creating any children, you can pass an empty dict here.)

Return type:

ProtocolResultsDir

Methods

__init__(data[, protocol_results, children])

Create a new ProtocolResultsDir object.

add_mongodb_write_ops(write_ops, mongodb[, ...])

Accumulate write and update operations for writing this object to a MongoDB database.

add_results(for_protocol_name, results)

Add a new results object to this results directory node.

from_dir(dirname[, parent, name, ...])

Initialize a new ProtocolResultsDir object from dirname.

from_mongodb(mongodb, doc_id, **kwargs)

Create and initialize an object from a MongoDB instance.

from_mongodb_doc(mongodb, collection_name, ...)

Create and initialize an object from a MongoDB instance and pre-loaded primary document.

items()

An iterator over the (child_name, child_node) pairs of this node.

keys()

An iterator over the keys (child names) of this tree node.

prune_tree(paths[, paths_are_sorted])

Prune the tree rooted here to include only the given paths, discarding all other leaves & branches.

remove_from_mongodb(mongodb, doc_id[, ...])

Remove the documents corresponding to an instance of this class from a MongoDB database.

remove_me_from_mongodb(mongodb[, session, ...])

to_dataframe([pivot_valuename, pivot_value, ...])

Convert these results into Pandas dataframe.

to_nameddict()

Convert the results in this object into nested NamedDict objects.

underlying_tree_paths()

Dictionary paths leading to data objects/nodes beneath this one.

view(keys_to_keep)

Get a "view" of this tree node that only has a subset of this node's children.

write([dirname, parent, data_already_written])

Write this "protocol results directory" to a directory.

write_to_mongodb(mongodb[, session, ...])

Write this object to a MongoDB database.

Attributes

collection_name

add_mongodb_write_ops(write_ops, mongodb, overwrite_existing=False, **kwargs)#

Accumulate write and update operations for writing this object to a MongoDB database.

Similar to write_to_mongodb() but collects write operations instead of actually executing any write operations on the database. This function may be preferred to write_to_mongodb() when this object is being written as a part of a larger entity and executing write operations is saved until the end.

As in write_to_mongodb(), self.collection_name is the collection name and _id is either: 1) the ID used by a previous write or initial read-in, if one exists, OR 2) a new random bson.objectid.ObjectId

Parameters:
  • write_ops (WriteOpsByCollection) – An object that keeps track of pymongo write operations on a per-collection basis. This object accumulates write operations to be performed at some point in the future.

  • mongodb (pymongo.database.Database) – The MongoDB instance to write data to.

  • overwrite_existing (bool, optional) – Whether existing documents should be overwritten. The default of False causes a ValueError to be raised if a document with the given _id already exists and is different from what is being written.

  • **kwargs (dict) – Additional keyword arguments potentially used by subclass implementations. Any arguments allowed by a subclass’s _add_auxiliary_write_ops_and_update_doc method is allowed here.

Returns:

The identifier (_id value) of the main document that was written.

Return type:

bson.objectid.ObjectId

add_results(for_protocol_name, results)#

Add a new results object to this results directory node.

The added results object must share this result directory’s data, i.e., its .data attribute must match the .data of this directory. This requirement is usually met because the results have been created by running a protocol on this directory’s .data. The results object is stored in the .for_protocol[for_protocol_name] attribute of this directory.

Parameters:
  • for_protocol_name (str) – Name of the protocol to be added.

  • results (ProtocolResults) – The results object to be added

Return type:

None

classmethod from_dir(dirname, parent=None, name=None, preloaded_data=None, quick_load=False)#

Initialize a new ProtocolResultsDir object from dirname.

Parameters:
  • dirname (str) – The root directory name (under which there are ‘edesign’ and ‘data’, and possibly ‘results’, subdirectories).

  • parent (ProtocolResultsDir, optional) – The parent results-directory object that is loading the returned object as a sub-results. This is used internally when loading a ProtocolResultsDir that represents a node of the data-tree with children.

  • name (str, optional) – The name of this result within parent. This is only used when parent is not None.

  • preloaded_data (ProtocolData, optional) – In the case that the ProtocolData object for dirname is already loaded, it can be passed in here. Otherwise leave this as None and it will be loaded.

  • quick_load (bool, optional) – Setting this to True skips the loading of data and experiment-design components that may take a long time to load. This can be useful all the information of interest lies only within the contained results objects.

Return type:

ProtcolResultsDir

classmethod from_mongodb(mongodb, doc_id, **kwargs)#

Create and initialize an object from a MongoDB instance.

Parameters:
  • mongodb (pymongo.database.Database) – The MongoDB instance to load from.

  • doc_id (bson.objecctid.ObjectId or dict) – The object ID or filter used to find a single object ID within the database. This document is loaded from the collection given by the collection_name attribute of this class.

  • **kwargs (dict) – Additional keyword arguments potentially used by subclass implementations. Any arguments allowed by a subclass’s _create_obj_from_doc_and_mongodb method is allowed here.

Return type:

object

classmethod from_mongodb_doc(mongodb, collection_name, doc, **kwargs)#

Create and initialize an object from a MongoDB instance and pre-loaded primary document.

Parameters:
  • mongodb (pymongo.database.Database) – The MongoDB instance to load from.

  • collection_name (str) – The collection name within mongodb that doc was loaded from. This is needed for the sole purpose of setting the created (returned) object’s database “coordinates”.

  • doc (dict) – The already-retrieved main document for the object being loaded. This takes the place of giving an identifier for this object.

  • **kwargs (dict) – Additional keyword arguments potentially used by subclass implementations. Any arguments allowed by a subclass’s _create_obj_from_doc_and_mongodb method is allowed here.

Return type:

object

items()#

An iterator over the (child_name, child_node) pairs of this node.

keys()#

An iterator over the keys (child names) of this tree node.

prune_tree(paths, paths_are_sorted=False)#

Prune the tree rooted here to include only the given paths, discarding all other leaves & branches.

Parameters:
  • paths (list) – A list of tuples specifying the paths to keep.

  • paths_are_sorted (bool, optional) – Whether paths is sorted (lexicographically). Setting this to True will save a little time.

Returns:

A view of this node and its descendants where unwanted children have been removed.

Return type:

TreeNode

classmethod remove_from_mongodb(mongodb, doc_id, collection_name=None, session=None, recursive='default')#

Remove the documents corresponding to an instance of this class from a MongoDB database.

Parameters:
  • mongodb (pymongo.database.Database) – The MongoDB instance to remove documents from.

  • doc_id (bson.objectid.ObjectId) – The identifier of the root document stored in the database.

  • collection_name (str, optional) – the MongoDB collection within mongodb where the main document resides. If None, then <this_class>.collection_name is used (which is usually what you want).

  • session (pymongo.client_session.ClientSession, optional) – MongoDB session object to use when interacting with the MongoDB database. This can be used to implement transactions among other things.

  • recursive (RecursiveRemovalSpecification, optional) – An object that filters the type of documents that are removed. Used when working with inter-related experiment designs, data, and results objects to only remove the types of documents you know aren’t being shared with other documents.

Return type:

None

to_dataframe(pivot_valuename=None, pivot_value=None, drop_columns=False)#

Convert these results into Pandas dataframe.

Parameters:
  • pivot_valuename (str, optional) – If not None, the resulting dataframe is pivoted using pivot_valuename as the column whose values name the pivoted table’s column names. If None and pivot_value is not None,`”ValueName”` is used.

  • pivot_value (str, optional) – If not None, the resulting dataframe is pivoted such that values of the pivot_value column are rearranged into new columns whose names are given by the values of the pivot_valuename column. If None and pivot_valuename is not None,`”Value”` is used.

  • drop_columns (bool or list, optional) – A list of column names to drop (prior to performing any pivot). If True appears in this list or is given directly, then all constant-valued columns are dropped as well. No columns are dropped when drop_columns == False.

Return type:

DataFrame

to_nameddict()#

Convert the results in this object into nested NamedDict objects.

Return type:

NamedDict

underlying_tree_paths()#

Dictionary paths leading to data objects/nodes beneath this one.

Returns:

A list of tuples, each specifying the tree traversal to a child node. The first tuple is the empty tuple, referring to this (root) node.

Return type:

list

view(keys_to_keep)#

Get a “view” of this tree node that only has a subset of this node’s children.

Parameters:

keys_to_keep (iterable) – A sequence of key names to keep.

Return type:

TreeNode

write(dirname=None, parent=None, data_already_written=False)#

Write this “protocol results directory” to a directory.

Parameters:
  • dirname (str) – The root directory to write into. This directory will have ‘edesign’, ‘data’, and ‘results’ subdirectories, which will be created if needed and overwritten if present. If None, then the path this object was loaded from is used (if this object wasn’t loaded from disk, an error is raised).

  • parent (ProtocolResultsDir, optional) – The parent protocol results directory, when a parent is writing this results dir as a sub-results-dir. Otherwise leave as None.

  • data_already_written (bool, optional) – If True, the data object within this results directory is not written to disk, and it is left to the caller to ensure the data object is saved.

Return type:

None

write_to_mongodb(mongodb, session=None, overwrite_existing=False, **kwargs)#

Write this object to a MongoDB database.

The collection name used is self.collection_name, and the _id is either: 1) the ID used by a previous write or initial read-in, if one exists, OR 2) a new random bson.objectid.ObjectId

Parameters:
  • mongodb (pymongo.database.Database) – The MongoDB instance to write data to.

  • session (pymongo.client_session.ClientSession, optional) – MongoDB session object to use when interacting with the MongoDB database. This can be used to implement transactions among other things.

  • overwrite_existing (bool, optional) – Whether existing documents should be overwritten. The default of False causes a ValueError to be raised if a document with the given _id already exists and is different from what is being written.

  • **kwargs (dict) – Additional keyword arguments potentially used by subclass implementations. Any arguments allowed by a subclass’s _add_auxiliary_write_ops_and_update_doc method is allowed here.

Returns:

The identifier (_id value) of the main document that was written.

Return type:

bson.objectid.ObjectId