Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to latest holoviews #688

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion external/holoviews
1 change: 0 additions & 1 deletion topo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@

import os
import param
import imagen


def version_int(v):
Expand Down
10 changes: 5 additions & 5 deletions topo/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import numpy as np

from holoviews.interface.collector import Reference
from holoviews.core.io import Reference
from holoviews import HSV, Image
from holoviews.core.options import Compositor
from holoviews.ipython import IPTestCase
Expand All @@ -29,10 +29,10 @@



CoG_spec = "Image.X CoG * Image.Y CoG * Image.BlueChannel"
XYCoG = chain.instance(group='XYCoG', name='XYCoG',
operations = [image_overlay.instance(spec=CoG_spec), factory.instance()])
Compositor.register(Compositor("Image.X CoG * Image.Y CoG", XYCoG, 'XYCoG', 'display'))
# CoG_spec = "Image.X CoG * Image.Y CoG * Image.BlueChannel"
# XYCoG = chain.instance(group='XYCoG', name='XYCoG',
# operations = [image_overlay.instance(spec=CoG_spec), factory.instance()])
# Compositor.register(Compositor("Image.X CoG * Image.Y CoG", XYCoG, 'XYCoG', 'display'))


import param
Expand Down
4 changes: 2 additions & 2 deletions topo/analysis/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ def _update_proj_cog(self, p, proj):

timestamp = topo.sim.time()
lbrt = sheet.bounds.lbrt()
xsv = Image(xcog, sheet.bounds, label=proj.name, group='X CoG',
xsv = Image(xcog, bounds=sheet.bounds, label=proj.name, group='X CoG',
vdims=[Dimension('X CoG', range=(lbrt[0], lbrt[2]))])
ysv = Image(ycog, sheet.bounds, label=proj.name, group='Y CoG',
ysv = Image(ycog, bounds=sheet.bounds, label=proj.name, group='Y CoG',
vdims=[Dimension('Y CoG', range=(lbrt[1], lbrt[3]))])

lines = []
Expand Down
8 changes: 4 additions & 4 deletions topo/analysis/featureresponses.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from holoviews import Image, HoloMap
from holoviews.ipython.widgets import ProgressBar
from holoviews.interface.collector import AttrDict
from topo.misc.attrdict import AttrDict

import topo
import topo.base.sheetcoords
Expand Down Expand Up @@ -52,14 +52,14 @@ def update_sheet_activity(sheet_name, force=False):
src_name=sheet.name, shape=sheet.activity.shape,
timestamp=time)
if not view:
im = Image(np.array(sheet.activity), sheet.bounds)
im = Image(np.array(sheet.activity), bounds=sheet.bounds)
im.metadata=metadata
view = HoloMap((time, im), key_dimensions=[Time])
view = HoloMap((time, im), kdims=[Time])
view.metadata = metadata
sheet.views.Maps[name] = view
else:
if force or view.range('Time')[1] < time:
im = Image(np.array(sheet.activity), sheet.bounds)
im = Image(np.array(sheet.activity), bounds=sheet.bounds)
im.metadata=metadata
view[time] = im
return view
Expand Down
6 changes: 3 additions & 3 deletions topo/analysis/weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _process(self, tree, key=None):

# Construct Elements
label =' '.join([s, p])
histogram = Histogram(bins, edges, group="Weight Isotropy",
histogram = Histogram((edges, bins), group="Weight Isotropy",
kdims=[Dimension('Azimuth')], label=label)
layout.WeightIsotropy['_'.join([s, p])] = histogram
return layout
Expand Down Expand Up @@ -148,7 +148,7 @@ def _process(self, tree, key=None):
featurepref = preferences[s]
if isinstance(featurepref, HoloMap):
featurepref = featurepref.last
feature = featurepref.value_dimensions[0]
feature = featurepref.vdims[0]
feature_arr = featurepref.data.flat
cfs = tree.CFs[p]
deltas, weights = [], []
Expand All @@ -171,7 +171,7 @@ def _process(self, tree, key=None):
# Construct Elements
label = ' '.join([s,p])
group = '%s Weight Distribution' % self.p.feature
histogram = Histogram(bins, edges, group=group, label=label,
histogram = Histogram((edges, bins), group=group, label=label,
kdims=[' '.join([self.p.feature, 'Difference'])],
vdims=['Weight'])

Expand Down
4 changes: 2 additions & 2 deletions topo/base/cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import param
from holoviews import GridSpace, Dimension, HoloMap, Layout
from holoviews.interface.collector import AttrDict
from topo.misc.attrdict import AttrDict
from holoviews.core import BoundingBox, BoundingRegionParameter, Slice

import patterngenerator
Expand Down Expand Up @@ -782,7 +782,7 @@ def view(self, sheet_x, sheet_y, timestamp=None, situated=False, **kwargs):
matrix_data = cf.weights.copy()
bounds = roi_bounds

sv = CFView(matrix_data, bounds, situated_bounds=situated_bounds,
sv = CFView(matrix_data, bounds=bounds, situated_bounds=situated_bounds,
input_sheet_slice=(r1, r2, c1, c2), roi_bounds=roi_bounds,
label=self.name, group='CF Weight')
sv.metadata=AttrDict(timestamp=timestamp)
Expand Down
4 changes: 2 additions & 2 deletions topo/base/generatorsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from topo.base.patterngenerator import PatternGenerator,Constant
from topo.base.simulation import FunctionEvent, PeriodicEventSequence

from holoviews.interface.collector import AttrDict
from topo.misc.attrdict import AttrDict
from holoviews import Image

import numpy as np
Expand Down Expand Up @@ -279,7 +279,7 @@ def __getitem__(self, coords):
else:
arr = self.activity.copy()

im = Image(arr, self.bounds,
im = Image(arr, bounds=self.bounds,
label=self.name+' Activity', group='Activity')[coords]
im.metadata=metadata
return im
4 changes: 2 additions & 2 deletions topo/base/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from imagen import Disk
from holoviews import Image, Layout
from holoviews.interface.collector import AttrDict
from topo.misc.attrdict import AttrDict

from sheet import Sheet
from simulation import EPConnection
Expand Down Expand Up @@ -326,7 +326,7 @@ def projection_view(self, timestamp=None):
"""Returns the activity in a single projection"""
if timestamp is None:
timestamp = self.src.simulation.time()
im = Image(self.activity.copy(), self.dest.bounds,
im = Image(self.activity.copy(), bounds=self.dest.bounds,
label=self.name, group='Activity')
im.metadata=AttrDict(proj_src_name=self.src.name,
precedence=self.src.precedence,
Expand Down
4 changes: 2 additions & 2 deletions topo/base/sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from holoviews import Image, Layout
from holoviews.core import BoundingBox, BoundingRegionParameter, SheetCoordinateSystem
from holoviews.interface.collector import AttrDict
from topo.misc.attrdict import AttrDict

from simulation import EventProcessor
from functionfamily import TransferFn
Expand Down Expand Up @@ -309,7 +309,7 @@ def __getitem__(self, coords):
row_precedence=self.row_precedence,
timestamp=self.simulation.time())

image = Image(self.activity.copy(), self.bounds,
image = Image(self.activity.copy(), bounds=self.bounds,
label=self.name, group='Activity')[coords]
image.metadata=metadata
return image
Expand Down
2 changes: 1 addition & 1 deletion topo/base/sheetview.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def situated(self):
r1, r2, c1, c2 = self.input_sheet_slice
data[r1:r2, c1:c2] = self.data

return CFView(data, self.situated_bounds, roi_bounds=self.bounds,
return CFView(data, bounds=self.situated_bounds, roi_bounds=self.bounds,
situated_bounds=self.situated_bounds,
label=self.label, group=self.group)

Expand Down
2 changes: 1 addition & 1 deletion topo/base/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import time
import bisect

from holoviews.interface.collector import AttrDict
from topo.misc.attrdict import AttrDict

#: Default path to the current simulation, from main
#: Only to be used by script_repr(), to allow it to generate
Expand Down
100 changes: 75 additions & 25 deletions topo/misc/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,20 @@
from param.parameterized import Parameterized
from topo.base.simulation import OptionalSingleton

try:
# By default, use a non-GUI backend for matplotlib.
from matplotlib import pyplot as plt
plt.switch_backend('agg')

matplotlib_imported=True
except ImportError:
matplotlib_imported=False

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, seeing anyone change this code just raises the hairs on the back of my neck! There have been so, so many issues over the years from Matplotlib choosing the wrong backend, that I am very reluctant ever to change this code. Are you certain that when used in batch mode (no Tk), it still works?

# Dummy object just for messages
cmdline_main=Parameterized(name="CommandLine")

ipython_shell_interface = None
ipython_prompt_interface = None
try:
try:
from IPython.terminal.embed import InteractiveShellEmbed as IPShell
from IPython.terminal.embed import InteractiveShellEmbed as IPShell
from traitlets.config.loader import Config
from IPython.terminal.prompts import Prompts, Token
import IPython.lib.inputhook
except ImportError: # Prior to IPython 1.0, InteractiveShellEmbed was found in the frontend package
from IPython.frontend.terminal.embed import InteractiveShellEmbed as IPShell # pyflakes:ignore (try/except import)
from IPython.config.loader import Config
from IPython.config.loader import Config
ipython_shell_interface = "InteractiveShellEmbed"
try:
from IPython.core.prompts import PromptManager # pyflakes:ignore (try/except import)
Expand Down Expand Up @@ -224,6 +218,34 @@ def get_format(cls):
return cls._format


class IPythonCommandPrompt(Prompts):
"""
Control over input prompt.

Several predefined formats are provided, and any of these (or any
arbitrary string) can be used by calling set_format() with their
values.

See the IPython manual for details:
https://ipython.readthedocs.io/en/5.x/config/details.html?highlight=Prompts
"""
def in_prompt_tokens(self, cli=None):

return [
(Token, 'Topographica '),
(Token.Prompt, 'In <'),
(Token.PromptNum, str(self.shell.execution_count)),
(Token.Prompt, '>: '),
]

def out_prompt_tokens(self):
return [
(Token.OutPrompt, 'Out<'),
(Token.OutPromptNum, str(self.shell.execution_count)),
(Token.OutPrompt, '>: '),
]


class CommandPrompt(IPCommandPromptHandler):
"""
Control over input prompt.
Expand Down Expand Up @@ -455,11 +477,44 @@ def o_action(option,opt_str,value,parser):
help="set the default output path")


if ipython_shell_interface == "InteractiveShellEmbed":
# IPython 0.11 and later

config = Config()
if ipython_prompt_interface == "PromptManager":
config.PromptManager.in_template = CommandPrompt.get_format()
config.PromptManager.in2_template = CommandPrompt2.get_format()
config.PromptManager.out_template = OutputPrompt.get_format()
config.InteractiveShell.confirm_exit = False
else:
config.TerminalInteractiveShell.prompts_class=IPythonCommandPrompt
config.InteractiveShell.confirm_exit = False


def gui(start=True,exit_on_quit=True):
"""Start the GUI as if -g were supplied in the command used to launch Topographica."""
if matplotlib_imported:
#
# To integrate with Tk we need to
# create ipshell *before* calling enable_gui
# it is important that you use instance(), instead of the class
# constructor, so that it creates the global InteractiveShell singleton

IPShell.instance(config=config)

try:
# Need to connect Tk before import of pyplot
import matplotlib
matplotlib.use("TkAgg")

# # By default, use a non-GUI backend for matplotlib.
# from matplotlib import pyplot as plt
# plt.ioff()
# plt.switch_backend('agg')

from holoviews.plotting import mpl
plt.switch_backend('TkAgg')
except:
pass

auto_import_commands()
if start:
import topo.tkgui
Expand Down Expand Up @@ -708,8 +763,6 @@ def exec_startup_files():
if os.path.exists(startup_file):
cmdline_main.warning("Ignoring %s; location for startup file is %s (UNIX/Linux/Mac OS X) or %s (Windows)."%(startup_file,rcpath,inipath))



### Execute what is specified by the options.

def process_argv(argv):
Expand Down Expand Up @@ -799,19 +852,16 @@ def process_argv(argv):
elif ipython_shell_interface == "InteractiveShellEmbed":
# IPython 0.11 and later

config = Config()

if ipython_prompt_interface == "PromptManager":
config.PromptManager.in_template = CommandPrompt.get_format()
config.PromptManager.in2_template = CommandPrompt2.get_format()
config.PromptManager.out_template = OutputPrompt.get_format()
else:
config.InteractiveShell.prompt_in1 = CommandPrompt.get_format()
config.InteractiveShell.prompt_in2 = CommandPrompt2.get_format()
config.InteractiveShell.prompt_out = OutputPrompt.get_format()
config.InteractiveShell.confirm_exit = False
ipshell = IPShell(config=config,user_ns=__main__.__dict__,
banner1="",exit_msg="")
config.InteractiveShell.confirm_exit = False

IPython.lib.inputhook.enable_gui(gui='tk')


ipshell = IPShell.instance()
if option.pdb:
ipshell.call_pdb = True

Expand All @@ -822,7 +872,7 @@ def process_argv(argv):
cmdline_main.warning(
"Could not load IPython extension 'topo.misc.ipython'; ignored error was:\n%s"%traceback.format_exc())

ipshell()
ipshell.mainloop(local_ns=__main__.__dict__)

global return_code
if return_code != 0:
Expand Down
2 changes: 1 addition & 1 deletion topo/misc/lancext.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import param

from holoviews import NdMapping, Layout
from holoviews.interface.collector import Collector
from featuremapper.collector import Collector
from holoviews.core.element import Collator
from holoviews.core.io import Pickler

Expand Down
9 changes: 7 additions & 2 deletions topo/submodel/specifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ def __lt__(self, other):


def __eq__(self, other):
return self.sort_precedence == other.sort_precedence

try:
return self.sort_precedence == other.sort_precedence
except Exception as e:
# Change in holoviews
if type(other) == str:
return False
raise e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the motivation for this change? When will a Specification ever be compared to a string?


def __init__(self, object_type):
self._object_type = object_type
Expand Down
4 changes: 4 additions & 0 deletions topo/tests/unit/testcf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import unittest
import numpy

# Need to load TkAgg before any calls to pyplot in the imports
import matplotlib
matplotlib.use("TkAgg")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem correct; tests cannot assume that Tk is available in that run.

from topo.base.simulation import Simulation
from topo.base.boundingregion import BoundingBox
from topo.base.cf import CFIter,ResizableCFProjection,CFSheet
Expand Down
Loading