text
stringlengths
0
828
1536,"def single_row_or_col_df_to_dict(desired_type: Type[T], single_rowcol_df: pd.DataFrame, logger: Logger, **kwargs)\
-> Dict[str, str]:
""""""
Helper method to convert a dataframe with one row or one or two columns into a dictionary
:param desired_type:
:param single_rowcol_df:
:param logger:
:param kwargs:
:return:
""""""
if single_rowcol_df.shape[0] == 1:
return single_rowcol_df.transpose()[0].to_dict()
# return {col_name: single_rowcol_df[col_name][single_rowcol_df.index.values[0]] for col_name in single_rowcol_df.columns}
elif single_rowcol_df.shape[1] == 2 and isinstance(single_rowcol_df.index, pd.RangeIndex):
# two columns but the index contains nothing but the row number : we can use the first column
d = single_rowcol_df.set_index(single_rowcol_df.columns[0])
return d[d.columns[0]].to_dict()
elif single_rowcol_df.shape[1] == 1:
# one column and one index
d = single_rowcol_df
return d[d.columns[0]].to_dict()
else:
raise ValueError('Unable to convert provided dataframe to a parameters dictionary : '
'expected exactly 1 row or 1 column, found : ' + str(single_rowcol_df.shape) + '')"
1537,"def get_default_pandas_converters() -> List[Union[Converter[Any, pd.DataFrame],
Converter[pd.DataFrame, Any]]]:
""""""
Utility method to return the default converters associated to dataframes (from dataframe to other type,
and from other type to dataframe)
:return:
""""""
return [ConverterFunction(from_type=pd.DataFrame, to_type=dict, conversion_method=single_row_or_col_df_to_dict),
ConverterFunction(from_type=dict, to_type=pd.DataFrame, conversion_method=dict_to_df,
option_hints=dict_to_single_row_or_col_df_opts),
ConverterFunction(from_type=pd.DataFrame, to_type=pd.Series,
conversion_method=single_row_or_col_df_to_series)]"
1538,"def full_subgraph(self, vertices):
""""""
Return the subgraph of this graph whose vertices
are the given ones and whose edges are all the edges
of the original graph between those vertices.
""""""
subgraph_vertices = {v for v in vertices}
subgraph_edges = {edge
for v in subgraph_vertices
for edge in self._out_edges[v]
if self._heads[edge] in subgraph_vertices}
subgraph_heads = {edge: self._heads[edge]
for edge in subgraph_edges}
subgraph_tails = {edge: self._tails[edge]
for edge in subgraph_edges}
return DirectedGraph._raw(
vertices=subgraph_vertices,
edges=subgraph_edges,
heads=subgraph_heads,
tails=subgraph_tails,
)"
1539,"def _raw(cls, vertices, edges, heads, tails):
""""""
Private constructor for direct construction of
a DirectedGraph from its consituents.
""""""
self = object.__new__(cls)
self._vertices = vertices
self._edges = edges
self._heads = heads
self._tails = tails
# For future use, map each vertex to its outward and inward edges.
# These could be computed on demand instead of precomputed.
self._out_edges = collections.defaultdict(set)
self._in_edges = collections.defaultdict(set)
for edge in self._edges:
self._out_edges[self._tails[edge]].add(edge)
self._in_edges[self._heads[edge]].add(edge)
return self"
1540,"def from_out_edges(cls, vertices, edge_mapper):
""""""
Create a DirectedGraph from a collection of vertices and
a mapping giving the vertices that each vertex is connected to.
""""""
vertices = set(vertices)
edges = set()
heads = {}
tails = {}
# Number the edges arbitrarily.
edge_identifier = itertools.count()
for tail in vertices:
for head in edge_mapper[tail]:
edge = next(edge_identifier)
edges.add(edge)
heads[edge] = head
tails[edge] = tail
return cls._raw(