Skip to content

Commit

Permalink
+ opinion std mode
Browse files Browse the repository at this point in the history
  • Loading branch information
imtambovtcev committed Mar 5, 2024
1 parent b1e3cff commit b33c888
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions hari_plotter/node_gatherer.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ def min_opinion(self) -> Dict[Tuple[int], float]:
"""Returns a mapping of node IDs to the minimum opinion value among their inner opinions."""
return {node: min(data.get('Inner opinions', {None: data['Opinion']}).values()) for node, data in self.G.nodes(data=True)}

@node_parameter_logger('Opinion Standard Deviation')
def std_opinion(self) -> Dict[Tuple[int], float]:
"""Returns a mapping of node IDs to the standard deviation of opinion values among their inner opinions."""
return {node: np.std(list(data.get('Inner opinions', {None: data['Opinion']}).values())) for node, data in self.G.nodes(data=True)}

@node_parameter_logger('Label')
def label(self) -> Dict[Tuple[int], str]:
"""Returns a mapping of node IDs to their labels."""
Expand Down
17 changes: 14 additions & 3 deletions hari_plotter/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,7 @@ def __init__(self, color_scheme: ColorScheme, clustering_settings: dict = {},
scale: Optional[Tuple[str] | None] = None,
show_x_label: bool = True, show_y_label: bool = True,
x_lim: Optional[Sequence[float] | None] = None, y_lim: Optional[Sequence[float] | None] = None,
min_cluster_size: int = 2, colormap: str = 'coolwarm', show_colorbar: bool = False, show_legend: bool = True):
min_cluster_size: int = 2, colormap: str = 'coolwarm', show_colorbar: bool = False, show_legend: bool = True, mode='Std'):
self.parameters = ('Time', 'Opinion')
self.clustering_settings = clustering_settings
self.scale = scale or tuple('Linear', 'Linear')
Expand All @@ -1877,6 +1877,8 @@ def __init__(self, color_scheme: ColorScheme, clustering_settings: dict = {},
self.min_cluster_size = min_cluster_size
self.colormap = colormap

self.mode = mode

self.show_colorbar = show_colorbar
self.show_legend = show_legend

Expand All @@ -1887,6 +1889,8 @@ def __init__(self, color_scheme: ColorScheme, clustering_settings: dict = {},
self.min_value = None

def get_static_plot_requests(self):
if self.mode == 'Std':
return [{'method': 'clustering_graph_values', 'settings': {'parameters': ('Time', 'Opinion', 'Opinion Standard Deviation', 'Cluster size', 'Label'), 'scale': self.scale, 'clustering_settings': self.clustering_settings}}]
return [{'method': 'clustering_graph_values', 'settings': {'parameters': ('Time', 'Min opinion', 'Opinion', 'Max opinion', 'Cluster size', 'Label'), 'scale': self.scale, 'clustering_settings': self.clustering_settings}}]

def get_track_clusterings_requests(self):
Expand All @@ -1904,9 +1908,16 @@ def data(self, static_data_cache: Interface.StaticDataCache) -> dict:
# Transform data to suitable format for plotting
time = np.array(data['Time'])
labels = data['Label']
min_opinion = np.array(data['Min opinion'])

opinion = np.array(data['Opinion'])
max_opinion = np.array(data['Max opinion'])
if self.mode == 'Std':
std_opinion = np.array(data['Opinion Standard Deviation'])
min_opinion = opinion-std_opinion
max_opinion = opinion+std_opinion
else:
min_opinion = np.array(data['Min opinion'])
max_opinion = np.array(data['Max opinion'])

cluster_size = np.array(data['Cluster size'])

if self.scale[0] == 'Tanh':
Expand Down

0 comments on commit b33c888

Please sign in to comment.