/usr/lib/python3/dist-packages/mdp/hinet/switchboard_factory.py is in python3-mdp 3.5-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | """
Extension for building switchboards in a 2d hierarchical network.
"""
from builtins import range
# TODO: add unittests and maybe mention it in the tutorial
# TODO: maybe integrate all this into the original switchboard classes?
import mdp
from mdp.hinet import (
ChannelSwitchboard, Rectangular2dSwitchboard, DoubleRect2dSwitchboard,
DoubleRhomb2dSwitchboard
)
def get_2d_image_switchboard(image_size_xy):
"""Return a Rectangular2dSwitchboard representing an image.
This can then be used as the prev_switchboard.
"""
return Rectangular2dSwitchboard(in_channels_xy=image_size_xy,
field_channels_xy=1,
field_spacing_xy=1)
class FactoryExtensionChannelSwitchboard(mdp.ExtensionNode,
ChannelSwitchboard):
"""Extension node for the assembly of channel switchboards.
data attributes:
free_parameters -- List of parameters that do not depend on the previous
layer. Note that there might still be restrictions imposed by the
switchboard.
By convention parameters that end with '_xy' can either be a single
int or a 2-tuple (sequence) of ints.
compatible_pre_switchboards -- List of compatible base classes for
prev_switchboard.
"""
extension_name = "switchboard_factory"
free_parameters = []
compatible_pre_switchboards = [ChannelSwitchboard]
@classmethod
def create_switchboard(cls, free_params, prev_switchboard,
prev_output_dim):
"""Return a new instance of this switchboard.
free_params -- Parameters as specified by free_parameters.
prev_switchboard -- Instance of the previous switchboard.
prev_output_dim -- Output dimension of the previous layer.
This template method checks the compatibility of the prev_switchboard
and sanitizes '_xy' in free_params.
"""
compatible = False
for base_class in cls.compatible_pre_switchboards:
if isinstance(prev_switchboard, base_class):
compatible = True
if not compatible:
err = ("The prev_switchboard class '%s'" %
prev_switchboard.__class__.__name__ +
" is not compatible with this switchboard class" +
" '%s'." % cls.__name__)
raise mdp.hinet.SwitchboardException(err)
kwargs = cls._get_switchboard_kwargs(free_params, prev_switchboard,
prev_output_dim)
return cls(**kwargs)
@staticmethod
def _get_switchboard_kwargs(free_params, prev_switchboard,
prev_output_dim):
"""Return the kwargs for the cls '__init__' method.
Reference implementation, merges input into one single channel.
Override this method for other switchboard classes.
"""
in_channel_dim = prev_output_dim // prev_switchboard.output_channels
return {"input_dim": prev_output_dim,
"connections": list(range(prev_output_dim)),
"out_channel_dim": prev_output_dim,
"in_channel_dim": in_channel_dim}
class FactoryRectangular2dSwitchboard(FactoryExtensionChannelSwitchboard,
Rectangular2dSwitchboard):
free_parameters = ["field_channels_xy", "field_spacing_xy", "ignore_cover"]
compatible_pre_switchboards = [Rectangular2dSwitchboard,
DoubleRhomb2dSwitchboard]
@staticmethod
def _get_switchboard_kwargs(free_params, prev_switchboard,
prev_output_dim):
in_channel_dim = (prev_output_dim // prev_switchboard.output_channels)
if not "ignore_cover" in free_params:
free_params["ignore_cover"] = True
return {"in_channels_xy": prev_switchboard.out_channels_xy,
"field_channels_xy": free_params["field_channels_xy"],
"field_spacing_xy": free_params["field_spacing_xy"],
"in_channel_dim": in_channel_dim,
"ignore_cover": free_params["ignore_cover"]}
class FactoryDoubleRect2dSwitchboard(FactoryExtensionChannelSwitchboard,
DoubleRect2dSwitchboard):
free_parameters = ["field_channels_xy", "ignore_cover"]
compatible_pre_switchboards = [Rectangular2dSwitchboard,
DoubleRhomb2dSwitchboard]
@staticmethod
def _get_switchboard_kwargs(free_params, prev_switchboard,
prev_output_dim):
in_channel_dim = (prev_output_dim // prev_switchboard.output_channels)
if not "ignore_cover" in free_params:
free_params["ignore_cover"] = True
return {"in_channels_xy": prev_switchboard.out_channels_xy,
"field_channels_xy": free_params["field_channels_xy"],
"in_channel_dim": in_channel_dim,
"ignore_cover": free_params["ignore_cover"]}
class FactoryDoubleRhomb2dSwitchboard(FactoryExtensionChannelSwitchboard,
DoubleRhomb2dSwitchboard):
free_parameters = ["field_size"]
compatible_pre_switchboards = [DoubleRect2dSwitchboard]
@staticmethod
def _get_switchboard_kwargs(free_params, prev_switchboard,
prev_output_dim):
in_channel_dim = (prev_output_dim // prev_switchboard.output_channels)
return {"long_out_channels_xy": prev_switchboard.long_out_channels_xy,
"diag_field_channels": free_params["field_size"],
"in_channel_dim": in_channel_dim}
|