perseuspy.io.perseus package¶
Submodules¶
perseuspy.io.perseus.matrix module¶
-
class
perseuspy.io.perseus.matrix.
PathOrFile
(path_or_file, mode=None, reset=False)[source]¶ Bases:
object
Small context manager for file paths or file-like objects :param path_or_file: Path to a file or file-like object :param mode: Set reading/writing mode :param reset: Reset file-like to initial position. Has no effect on path.
-
perseuspy.io.perseus.matrix.
create_column_index
(annotations)[source]¶ Create a pd.MultiIndex using the column names and any categorical rows. Note that also non-main columns will be assigned a default category ‘’.
-
perseuspy.io.perseus.matrix.
read_annotations
(path_or_file, separator, type_map={'C': 'category', 'T': <class 'str'>, 'N': <class 'float'>, 'M': <class 'str'>, 'E': <class 'float'>}, reset=True)[source]¶ Read all annotations from the specified file.
>>> annotations = read_annotations(path_or_file, separator) >>> colnames = annotations['Column Name'] >>> types = annotations['Type'] >>> annot_row = annotations['Annot. row name']
Parameters: - path_or_file – Path or file-like object
- separator – Column separator
- type_map – Mapping Perseus types to numpy.dtype
- reset – Reset the file after reading. Useful for file-like, no-op for paths.
Returns: Ordered dictionary of annotations.
-
perseuspy.io.perseus.matrix.
read_perseus
(path_or_file, type_map={'C': 'category', 'T': <class 'str'>, 'N': <class 'float'>, 'M': <class 'str'>, 'E': <class 'float'>}, **kwargs)[source]¶ Read a Perseus-formatted matrix into a pd.DataFrame. Annotation rows will be converted into a multi-index.
By monkey-patching the returned pd.DataFrame a to_perseus method for exporting the pd.DataFrame is made available.
Parameters: - path_or_file – File path or file-like object
- type_map – How to map Perseus types to numpy.dtype
- kwargs – Keyword arguments passed as-is to pandas.read_csv
Returns: The parsed data frame
-
perseuspy.io.perseus.matrix.
to_perseus
(df, path_or_file, main_columns=None, separator='\t', type_map={category: 'C', dtype('<U'): 'T', dtype('O'): 'T', dtype('float64'): 'N', dtype('int64'): 'N'}, numerical_annotation_rows=set())[source]¶ Save pd.DataFrame to Perseus text format.
Parameters: - df – pd.DataFrame
- path_or_file – File name or file-like object
- main_columns – Main columns. Will be infered if set to None. All numeric columns up-until the first non-numeric column are considered main columns.
- separator – For separating fields, default ‘ ‘
perseuspy.io.perseus.network module¶
-
perseuspy.io.perseus.network.
from_perseus
(network_table, networks)[source]¶ Create networkx graph from network tables
>>> from perseuspy import read_networks, nx >>> network_table, networks = read_networks(folder) >>> graphs = nx.from_perseus(network_table, networks)
-
perseuspy.io.perseus.network.
read_networks
(folder)[source]¶ Read perseus network collection folder format
>>> network_table, networks = read_networks(folder)
Parameters: folder – Path to network collection Returns: Network table and dictionary with ‘name’, ‘edge_table’, and ‘node_table’ keys.
-
perseuspy.io.perseus.network.
write_networks
(folder, network_table, networks)[source]¶ Writing networkTable, nodes and edges to Perseus readable format.
Parameters: - folder – Path to output directory.
- network_table – Network table.
- networks – Dictionary with node and edge tables, indexed by network guid.