fontfiles = {}<EOL>fontexts = get_fontext_synonyms(fontext)<EOL>if fontpaths is None:<EOL><INDENT>if sys.platform == '<STR_LIT:win32>':<EOL><INDENT>fontdir = win32FontDirectory()<EOL>fontpaths = [fontdir]<EOL>for f in win32InstalledFonts(fontdir):<EOL><INDENT>base, ext = os.path.splitext(f)<EOL>if len(ext)><NUM_LIT:1> and ext[<NUM_LIT:1>:].lower() in fontexts:<EOL><INDENT>fontfiles[f] = <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>fontpaths = x11FontDirectory()<EOL>if sys.platform == '<STR_LIT>':<EOL><INDENT>for f in OSXInstalledFonts(fontext=fontext):<EOL><INDENT>fontfiles[f] = <NUM_LIT:1><EOL><DEDENT><DEDENT>for f in get_fontconfig_fonts(fontext):<EOL><INDENT>fontfiles[f] = <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>elif isinstance(fontpaths, str):<EOL><INDENT>fontpaths = [fontpaths]<EOL><DEDENT>for path in fontpaths:<EOL><INDENT>files = []<EOL>for ext in fontexts:<EOL><INDENT>files.extend(glob.glob(os.path.join(path, '<STR_LIT>'+ext)))<EOL>files.extend(glob.glob(os.path.join(path, '<STR_LIT>'+ext.upper())))<EOL><DEDENT>for fname in files:<EOL><INDENT>fontfiles[os.path.abspath(fname)] = <NUM_LIT:1><EOL><DEDENT><DEDENT>return [fname for fname in list(fontfiles.keys()) if os.path.exists(fname)]<EOL>

Search for fonts in the specified font paths.  If no paths are
given, will use a standard set of system paths, as well as the
list of fonts tracked by fontconfig if fontconfig is installed and
available.  A list of TrueType fonts are returned by default with
AFM fonts as an option.


if isinstance(weight, str):<EOL><INDENT>try:<EOL><INDENT>weight = weight_dict[weight.lower()]<EOL><DEDENT>except KeyError:<EOL><INDENT>weight = <NUM_LIT><EOL><DEDENT><DEDENT>elif weight in range(<NUM_LIT:100>, <NUM_LIT:1000>, <NUM_LIT:100>):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return weight<EOL>

Return the weight property as a numeric value.  String values
are converted to their corresponding numeric value.


name = font.family_name<EOL>sfnt = font.get_sfnt()<EOL>sfnt2 = sfnt.get((<NUM_LIT:1>,<NUM_LIT:0>,<NUM_LIT:0>,<NUM_LIT:2>))<EOL>sfnt4 = sfnt.get((<NUM_LIT:1>,<NUM_LIT:0>,<NUM_LIT:0>,<NUM_LIT:4>))<EOL>if sfnt2:<EOL><INDENT>sfnt2 = sfnt2.lower()<EOL><DEDENT>else:<EOL><INDENT>sfnt2 = '<STR_LIT>'<EOL><DEDENT>if sfnt4:<EOL><INDENT>sfnt4 = sfnt4.lower()<EOL><DEDENT>else:<EOL><INDENT>sfnt4 = '<STR_LIT>'<EOL><DEDENT>if   sfnt4.find('<STR_LIT>') >= <NUM_LIT:0>:<EOL><INDENT>style = '<STR_LIT>'<EOL><DEDENT>elif sfnt4.find('<STR_LIT>') >= <NUM_LIT:0>:<EOL><INDENT>style = '<STR_LIT>'<EOL><DEDENT>elif sfnt2.find('<STR_LIT>') >= <NUM_LIT:0>:<EOL><INDENT>style = '<STR_LIT>'<EOL><DEDENT>elif font.style_flags & ft2font.ITALIC:<EOL><INDENT>style = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>style = '<STR_LIT>'<EOL><DEDENT>if name.lower() in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>variant = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>variant = '<STR_LIT>'<EOL><DEDENT>weight = None<EOL>for w in list(weight_dict.keys()):<EOL><INDENT>if sfnt4.find(w) >= <NUM_LIT:0>:<EOL><INDENT>weight = w<EOL>break<EOL><DEDENT><DEDENT>if not weight:<EOL><INDENT>if font.style_flags & ft2font.BOLD:<EOL><INDENT>weight = <NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>weight = <NUM_LIT><EOL><DEDENT><DEDENT>weight = weight_as_number(weight)<EOL>if   sfnt4.find('<STR_LIT>') >= <NUM_LIT:0> or sfnt4.find('<STR_LIT>') >= <NUM_LIT:0> orsfnt4.find('<STR_LIT>') >= <NUM_LIT:0>:<EOL><INDENT>stretch = '<STR_LIT>'<EOL><DEDENT>elif sfnt4.find('<STR_LIT>') >= <NUM_LIT:0>:<EOL><INDENT>stretch = '<STR_LIT>'<EOL><DEDENT>elif sfnt4.find('<STR_LIT>') >= <NUM_LIT:0> or sfnt4.find('<STR_LIT>') >= <NUM_LIT:0>:<EOL><INDENT>stretch = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>stretch = '<STR_LIT>'<EOL><DEDENT>if font.scalable:<EOL><INDENT>size = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>size = str(float(font.get_fontsize()))<EOL><DEDENT>size_adjust = None<EOL>return FontEntry(font.fname, name, style, variant, weight, stretch, size)<EOL>

A function for populating the :class:`FontKey` by extracting
information from the TrueType font file.

*font* is a :class:`FT2Font` instance.


name = font.get_familyname()<EOL>if font.get_angle() != <NUM_LIT:0> or name.lower().find('<STR_LIT>') >= <NUM_LIT:0>:<EOL><INDENT>style = '<STR_LIT>'<EOL><DEDENT>elif name.lower().find('<STR_LIT>') >= <NUM_LIT:0>:<EOL><INDENT>style = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>style = '<STR_LIT>'<EOL><DEDENT>if name.lower() in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>variant = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>variant = '<STR_LIT>'<EOL><DEDENT>weight = weight_as_number(font.get_weight().lower())<EOL>stretch = '<STR_LIT>'<EOL>size = '<STR_LIT>'<EOL>size_adjust = None<EOL>return FontEntry(fontpath, name, style, variant, weight, stretch, size)<EOL>

A function for populating a :class:`FontKey` instance by
extracting information from the AFM font file.

*font* is a class:`AFM` instance.


fontlist = []<EOL>seen = {}<EOL>for fpath in fontfiles:<EOL><INDENT>verbose.report('<STR_LIT>' % (fpath), '<STR_LIT>')<EOL>fname = os.path.split(fpath)[<NUM_LIT:1>]<EOL>if fname in seen:  continue<EOL>else: seen[fname] = <NUM_LIT:1><EOL>if fontext == '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>fh = open(fpath, '<STR_LIT:r>')<EOL><DEDENT>except:<EOL><INDENT>verbose.report("<STR_LIT>" % fpath)<EOL>continue<EOL><DEDENT>try:<EOL><INDENT>try:<EOL><INDENT>font = afm.AFM(fh)<EOL><DEDENT>finally:<EOL><INDENT>fh.close()<EOL><DEDENT><DEDENT>except RuntimeError:<EOL><INDENT>verbose.report("<STR_LIT>"%fpath)<EOL>continue<EOL><DEDENT>prop = afmFontProperty(fpath, font)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>font = ft2font.FT2Font(str(fpath))<EOL><DEDENT>except RuntimeError:<EOL><INDENT>verbose.report("<STR_LIT>"%fpath)<EOL>continue<EOL><DEDENT>except UnicodeError:<EOL><INDENT>verbose.report("<STR_LIT>")<EOL>continue<EOL><DEDENT>try: prop = ttfFontProperty(font)<EOL>except: continue<EOL><DEDENT>fontlist.append(prop)<EOL><DEDENT>return fontlist<EOL>

A function to create a font lookup list.  The default is to create
a list of TrueType fonts.  An AFM font list can optionally be
created.


fnames = []<EOL>for named in list(d.values()):<EOL><INDENT>for styled in list(named.values()):<EOL><INDENT>for variantd in list(styled.values()):<EOL><INDENT>for weightd in list(variantd.values()):<EOL><INDENT>for stretchd in list(weightd.values()):<EOL><INDENT>for fname in list(stretchd.values()):<EOL><INDENT>fnames.append(fname)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>return fnames<EOL>

flatten a ttfdict to all the filenames it contains


fh = open(filename, '<STR_LIT:w>')<EOL>try:<EOL><INDENT>pickle.dump(data, fh)<EOL><DEDENT>finally:<EOL><INDENT>fh.close()<EOL><DEDENT>

Equivalent to pickle.dump(data, open(filename, 'w'))
but closes the file to prevent filehandle leakage.


fh = open(filename, '<STR_LIT:r>')<EOL>try:<EOL><INDENT>data = pickle.load(fh)<EOL><DEDENT>finally:<EOL><INDENT>fh.close()<EOL><DEDENT>return data<EOL>

Equivalent to pickle.load(open(filename, 'r'))
but closes the file to prevent filehandle leakage.


if os.path.splitext(filename)[<NUM_LIT:1>].lower() == '<STR_LIT>':<EOL><INDENT>result = _is_opentype_cff_font_cache.get(filename)<EOL>if result is None:<EOL><INDENT>fd = open(filename, '<STR_LIT:rb>')<EOL>tag = fd.read(<NUM_LIT:4>)<EOL>fd.close()<EOL>result = (tag == '<STR_LIT>')<EOL>_is_opentype_cff_font_cache[filename] = result<EOL><DEDENT>return result<EOL><DEDENT>return False<EOL>

Returns True if the given font is a Postscript Compact Font Format
Font embedded in an OpenType wrapper.  Used by the PostScript and
PDF backends that can not subset these fonts.


if self._family is None:<EOL><INDENT>family = rcParams['<STR_LIT>']<EOL>if is_string_like(family):<EOL><INDENT>return [family]<EOL><DEDENT>return family<EOL><DEDENT>return self._family<EOL>

Return a list of font names that comprise the font family.


return ft2font.FT2Font(str(findfont(self))).family_name<EOL>

Return the name of the font that best matches the font
properties.


if self._slant is None:<EOL><INDENT>return rcParams['<STR_LIT>']<EOL><DEDENT>return self._slant<EOL>

Return the font style.  Values are: 'normal', 'italic' or
'oblique'.


if self._variant is None:<EOL><INDENT>return rcParams['<STR_LIT>']<EOL><DEDENT>return self._variant<EOL>

Return the font variant.  Values are: 'normal' or
'small-caps'.


if self._weight is None:<EOL><INDENT>return rcParams['<STR_LIT>']<EOL><DEDENT>return self._weight<EOL>

Set the font weight.  Options are: A numeric value in the
range 0-1000 or one of 'light', 'normal', 'regular', 'book',
'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold',
'heavy', 'extra bold', 'black'


if self._stretch is None:<EOL><INDENT>return rcParams['<STR_LIT>']<EOL><DEDENT>return self._stretch<EOL>

Return the font stretch or width.  Options are: 'ultra-condensed',
'extra-condensed', 'condensed', 'semi-condensed', 'normal',
'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'.


if self._size is None:<EOL><INDENT>return rcParams['<STR_LIT>']<EOL><DEDENT>return self._size<EOL>


return self._file<EOL>

Return the filename of the associated font.


return generate_fontconfig_pattern(self)<EOL>

Get a fontconfig pattern suitable for looking up the font as
specified with fontconfig's ``fc-match`` utility.

See the documentation on `fontconfig patterns
<http://www.fontconfig.org/fontconfig-user.html>`_.

This support does not require fontconfig to be installed or
support for it to be enabled.  We are merely borrowing its
pattern syntax for use here.


if family is None:<EOL><INDENT>self._family = None<EOL><DEDENT>else:<EOL><INDENT>if is_string_like(family):<EOL><INDENT>family = [family]<EOL><DEDENT>self._family = family<EOL><DEDENT>

Change the font family.  May be either an alias (generic name
is CSS parlance), such as: 'serif', 'sans-serif', 'cursive',
'fantasy', or 'monospace', or a real font name.


if style not in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', None):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>self._slant = style<EOL>

Set the font style.  Values are: 'normal', 'italic' or
'oblique'.


if variant not in ('<STR_LIT>', '<STR_LIT>', None):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>self._variant = variant<EOL>

Set the font variant.  Values are: 'normal' or 'small-caps'.


if weight is not None:<EOL><INDENT>try:<EOL><INDENT>weight = int(weight)<EOL>if weight < <NUM_LIT:0> or weight > <NUM_LIT:1000>:<EOL><INDENT>raise ValueError()<EOL><DEDENT><DEDENT>except ValueError:<EOL><INDENT>if weight not in weight_dict:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>self._weight = weight<EOL>

Set the font weight.  May be either a numeric value in the
range 0-1000 or one of 'ultralight', 'light', 'normal',
'regular', 'book', 'medium', 'roman', 'semibold', 'demibold',
'demi', 'bold', 'heavy', 'extra bold', 'black'


if stretch is not None:<EOL><INDENT>try:<EOL><INDENT>stretch = int(stretch)<EOL>if stretch < <NUM_LIT:0> or stretch > <NUM_LIT:1000>:<EOL><INDENT>raise ValueError()<EOL><DEDENT><DEDENT>except ValueError:<EOL><INDENT>if stretch not in stretch_dict:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>self._stretch = stretch<EOL>

Set the font stretch or width.  Options are: 'ultra-condensed',
'extra-condensed', 'condensed', 'semi-condensed', 'normal',
'semi-expanded', 'expanded', 'extra-expanded' or
'ultra-expanded', or a numeric value in the range 0-1000.


if size is not None:<EOL><INDENT>try:<EOL><INDENT>size = float(size)<EOL><DEDENT>except ValueError:<EOL><INDENT>if size is not None and size not in font_scalings:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>self._size = size<EOL>

Set the font size.  Either an relative value of 'xx-small',
'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'
or an absolute font size, e.g. 12.


self._file = file<EOL>

Set the filename of the fontfile to use.  In this case, all
other properties will be ignored.


for key, val in list(self._parse_fontconfig_pattern(pattern).items()):<EOL><INDENT>if type(val) == list:<EOL><INDENT>getattr(self, "<STR_LIT>" + key)(val[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>getattr(self, "<STR_LIT>" + key)(val)<EOL><DEDENT><DEDENT>

Set the properties by parsing a fontconfig *pattern*.

See the documentation on `fontconfig patterns
<http://www.fontconfig.org/fontconfig-user.html>`_.

This support does not require fontconfig to be installed or
support for it to be enabled.  We are merely borrowing its
pattern syntax for use here.


return FontProperties(_init = self)<EOL>


return self.__default_weight<EOL>


if self.default_size is None:<EOL><INDENT>return rcParams['<STR_LIT>']<EOL><DEDENT>return self.default_size<EOL>


self.__default_weight = weight<EOL>

Set the default font weight.  The initial value is 'normal'.


self.default_size = size<EOL>

Set the default font size in points.  The initial value is set
by ``font.size`` in rc.


<EOL>raise NotImplementedError<EOL>

Update the font dictionary with new font files.
Currently not implemented.


def score_family(self, families, family2):

for i, family1 in enumerate(families):<EOL><INDENT>if family1.lower() in font_family_aliases:<EOL><INDENT>if family1 == '<STR_LIT>':<EOL><INDENT>family1 == '<STR_LIT>'<EOL><DEDENT>options = rcParams['<STR_LIT>' + family1]<EOL>if family2 in options:<EOL><INDENT>idx = options.index(family2)<EOL>return <NUM_LIT:0.1> * (float(idx) / len(options))<EOL><DEDENT><DEDENT>elif family1.lower() == family2.lower():<EOL><INDENT>return <NUM_LIT:0.0><EOL><DEDENT><DEDENT>return <NUM_LIT:1.0><EOL>

Returns a match score between the list of font families in
*families* and the font family name *family2*.

An exact match anywhere in the list returns 0.0.

A match by generic font name will return 0.1.

No match will return 1.0.


if style1 == style2:<EOL><INDENT>return <NUM_LIT:0.0><EOL><DEDENT>elif style1 in ('<STR_LIT>', '<STR_LIT>') andstyle2 in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>return <NUM_LIT:0.1><EOL><DEDENT>return <NUM_LIT:1.0><EOL>

Returns a match score between *style1* and *style2*.

An exact match returns 0.0.

A match between 'italic' and 'oblique' returns 0.1.

No match returns 1.0.


def score_variant(self, variant1, variant2):

if variant1 == variant2:<EOL><INDENT>return <NUM_LIT:0.0><EOL><DEDENT>else:<EOL><INDENT>return <NUM_LIT:1.0><EOL><DEDENT>

Returns a match score between *variant1* and *variant2*.

An exact match returns 0.0, otherwise 1.0.


def score_stretch(self, stretch1, stretch2):

try:<EOL><INDENT>stretchval1 = int(stretch1)<EOL><DEDENT>except ValueError:<EOL><INDENT>stretchval1 = stretch_dict.get(stretch1, <NUM_LIT>)<EOL><DEDENT>try:<EOL><INDENT>stretchval2 = int(stretch2)<EOL><DEDENT>except ValueError:<EOL><INDENT>stretchval2 = stretch_dict.get(stretch2, <NUM_LIT>)<EOL><DEDENT>return abs(stretchval1 - stretchval2) / <NUM_LIT><EOL>

Returns a match score between *stretch1* and *stretch2*.

The result is the absolute value of the difference between the
CSS numeric values of *stretch1* and *stretch2*, normalized
between 0.0 and 1.0.


def score_weight(self, weight1, weight2):

try:<EOL><INDENT>weightval1 = int(weight1)<EOL><DEDENT>except ValueError:<EOL><INDENT>weightval1 = weight_dict.get(weight1, <NUM_LIT>)<EOL><DEDENT>try:<EOL><INDENT>weightval2 = int(weight2)<EOL><DEDENT>except ValueError:<EOL><INDENT>weightval2 = weight_dict.get(weight2, <NUM_LIT>)<EOL><DEDENT>return abs(weightval1 - weightval2) / <NUM_LIT><EOL>

Returns a match score between *weight1* and *weight2*.

The result is the absolute value of the difference between the
CSS numeric values of *weight1* and *weight2*, normalized
between 0.0 and 1.0.


if size2 == '<STR_LIT>':<EOL><INDENT>return <NUM_LIT:0.0><EOL><DEDENT>try:<EOL><INDENT>sizeval1 = float(size1)<EOL><DEDENT>except ValueError:<EOL><INDENT>sizeval1 = self.default_size * font_scalings(size1)<EOL><DEDENT>try:<EOL><INDENT>sizeval2 = float(size2)<EOL><DEDENT>except ValueError:<EOL><INDENT>return <NUM_LIT:1.0><EOL><DEDENT>return abs(sizeval1 - sizeval2) / <NUM_LIT><EOL>

Returns a match score between *size1* and *size2*.

If *size2* (the size specified in the font file) is 'scalable', this
function always returns 0.0, since any font size can be generated.

Otherwise, the result is the absolute distance between *size1* and
*size2*, normalized so that the usual range of font sizes (6pt -
72pt) will lie between 0.0 and 1.0.


def findfont(self, prop, fontext='<STR_LIT>'):

debug = False<EOL>if prop is None:<EOL><INDENT>return self.defaultFont<EOL><DEDENT>if is_string_like(prop):<EOL><INDENT>prop = FontProperties(prop)<EOL><DEDENT>fname = prop.get_file()<EOL>if fname is not None:<EOL><INDENT>verbose.report('<STR_LIT>'%fname, '<STR_LIT>')<EOL>return fname<EOL><DEDENT>if fontext == '<STR_LIT>':<EOL><INDENT>font_cache = self.afm_lookup_cache<EOL>fontlist = self.afmlist<EOL><DEDENT>else:<EOL><INDENT>font_cache = self.ttf_lookup_cache<EOL>fontlist = self.ttflist<EOL><DEDENT>cached = font_cache.get(hash(prop))<EOL>if cached:<EOL><INDENT>return cached<EOL><DEDENT>best_score = <NUM_LIT><EOL>best_font = None<EOL>for font in fontlist:<EOL><INDENT>score =self.score_family(prop.get_family(), font.name) * <NUM_LIT> +self.score_style(prop.get_style(), font.style) +self.score_variant(prop.get_variant(), font.variant) +self.score_weight(prop.get_weight(), font.weight) +self.score_stretch(prop.get_stretch(), font.stretch) +self.score_size(prop.get_size(), font.size)<EOL>if score < best_score:<EOL><INDENT>best_score = score<EOL>best_font = font<EOL><DEDENT>if score == <NUM_LIT:0>:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if best_font is None or best_score >= <NUM_LIT>:<EOL><INDENT>verbose.report('<STR_LIT>' %<EOL>(prop, self.defaultFont))<EOL>result = self.defaultFont<EOL><DEDENT>else:<EOL><INDENT>verbose.report('<STR_LIT>' %<EOL>(prop, best_font.name, best_font.fname, best_score))<EOL>result = best_font.fname<EOL><DEDENT>font_cache[hash(prop)] = result<EOL>return result<EOL>

Search the font list for the font that most closely matches
the :class:`FontProperties` *prop*.

:meth:`findfont` performs a nearest neighbor search.  Each
font is given a similarity score to the target font
properties.  The first font with the highest score is
returned.  If no matches below a certain threshold are found,
the default font (usually Vera Sans) is returned.

The result is cached, so subsequent lookups don't have to
perform the O(n) nearest neighbor search.

See the `W3C Cascading Style Sheet, Level 1
<http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ documentation
for a description of the font finding algorithm.


return reshape(array([_isnan(i) for i in ravel(a)],'<STR_LIT:b>'), shape(a))<EOL>

y = isnan(x) returns True where x is Not-A-Number


if axis is None:<EOL><INDENT>return alltrue(ravel(a))<EOL><DEDENT>else:<EOL><INDENT>return alltrue(a, axis)<EOL><DEDENT>


_dict = { "<STR_LIT>" : which[<NUM_LIT:0>],<EOL>"<STR_LIT>" : module,<EOL>"<STR_LIT>" : version + module<EOL>}<EOL>print("""<STR_LIT>""" % _dict)<EOL>

Prints a message when the array package specific version of an extension
    fails to import correctly.


if axis is None:<EOL><INDENT>return _all(a)<EOL><DEDENT>return alltrue(a, axis)<EOL>


def Matrix(data, typecode=None, copy=<NUM_LIT:1>, savespace=<NUM_LIT:0>):

if isinstance(data, type("<STR_LIT>")):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>a = fromlist(data, type=typecode)<EOL>if a.rank == <NUM_LIT:0>:<EOL><INDENT>a.shape = (<NUM_LIT:1>,<NUM_LIT:1>)<EOL><DEDENT>elif a.rank == <NUM_LIT:1>:<EOL><INDENT>a.shape = (<NUM_LIT:1>,) + a.shape<EOL><DEDENT>a.__class__ = _Matrix<EOL>return a<EOL>

Matrix constructs new matrices from 2D nested lists of numbers


insp = ArtistInspector(o)<EOL>if property is None:<EOL><INDENT>ret = insp.pprint_getters()<EOL>print('<STR_LIT:\n>'.join(ret))<EOL>return<EOL><DEDENT>func = getattr(o, '<STR_LIT>' + property)<EOL>return func()<EOL>

Return the value of handle property.  property is an optional string
for the property you want to return

Example usage::

    getp(o)  # get all the object properties
    getp(o, 'linestyle')  # get the linestyle property

*o* is a :class:`Artist` instance, eg
:class:`~matplotllib.lines.Line2D` or an instance of a
:class:`~matplotlib.axes.Axes` or :class:`matplotlib.text.Text`.
If the *property* is 'somename', this function returns

  o.get_somename()

:func:`getp` can be used to query all the gettable properties with
``getp(o)``. Many properties have aliases for shorter typing, e.g.
'lw' is an alias for 'linewidth'.  In the output, aliases and full
property names will be listed as:

  property or alias = value

e.g.:

  linewidth or lw = 2


insp = ArtistInspector(h)<EOL>if len(kwargs)==<NUM_LIT:0> and len(args)==<NUM_LIT:0>:<EOL><INDENT>print('<STR_LIT:\n>'.join(insp.pprint_setters()))<EOL>return<EOL><DEDENT>if len(kwargs)==<NUM_LIT:0> and len(args)==<NUM_LIT:1>:<EOL><INDENT>print(insp.pprint_setters(prop=args[<NUM_LIT:0>]))<EOL>return<EOL><DEDENT>if not cbook.iterable(h): h = [h]<EOL>else: h = cbook.flatten(h)<EOL>if len(args)%<NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>funcvals = []<EOL>for i in range(<NUM_LIT:0>, len(args)-<NUM_LIT:1>, <NUM_LIT:2>):<EOL><INDENT>funcvals.append((args[i], args[i+<NUM_LIT:1>]))<EOL><DEDENT>funcvals.extend(list(kwargs.items()))<EOL>ret = []<EOL>for o in h:<EOL><INDENT>for s, val in funcvals:<EOL><INDENT>s = s.lower()<EOL>funcName = "<STR_LIT>"%s<EOL>func = getattr(o,funcName)<EOL>ret.extend( [func(val)] )<EOL><DEDENT><DEDENT>return [x for x in cbook.flatten(ret)]<EOL>

matplotlib supports the use of :func:`setp` ("set property") and
:func:`getp` to set and get object properties, as well as to do
introspection on the object.  For example, to set the linestyle of a
line to be dashed, you can do::

  >>> line, = plot([1,2,3])
  >>> setp(line, linestyle='--')

If you want to know the valid types of arguments, you can provide the
name of the property you want to set without a value::

  >>> setp(line, 'linestyle')
      linestyle: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' ]

If you want to see all the properties that can be set, and their
possible values, you can do::

  >>> setp(line)
      ... long output listing omitted

:func:`setp` operates on a single instance or a list of instances.
If you are in query mode introspecting the possible values, only
the first instance in the sequence is used.  When actually setting
values, all the instances will be set.  E.g., suppose you have a
list of two lines, the following will make both lines thicker and
red::

  >>> x = arange(0,1.0,0.01)
  >>> y1 = sin(2*pi*x)
  >>> y2 = sin(4*pi*x)
  >>> lines = plot(x, y1, x, y2)
  >>> setp(lines, linewidth=2, color='r')

:func:`setp` works with the matlab(TM) style string/value pairs or
with python kwargs.  For example, the following are equivalent::

  >>> setp(lines, 'linewidth', 2, 'color', r')  # matlab style

  >>> setp(lines, linewidth=2, color='r')       # python style


<EOL>if self._remove_method != None:<EOL><INDENT>self._remove_method(self)<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError('<STR_LIT>')<EOL><DEDENT>

Remove the artist from the figure if possible.  The effect
will not be visible until the figure is redrawn, e.g., with
:meth:`matplotlib.axes.Axes.draw_idle`.  Call
:meth:`matplotlib.axes.Axes.relim` to update the axes limits
if desired.

Note: :meth:`~matplotlib.axes.Axes.relim` will not see
collections even if the collection was added to axes with
*autolim* = True.

Note: there is no support for removing the artist's legend entry.


ax = self.axes<EOL>if ax is None or ax.xaxis is None:<EOL><INDENT>return False<EOL><DEDENT>return ax.xaxis.have_units() or ax.yaxis.have_units()<EOL>

Return *True* if units are set on the *x* or *y* axes


ax = getattr(self, '<STR_LIT>', None)<EOL>if ax is None or ax.xaxis is None:<EOL><INDENT>return x<EOL><DEDENT>return ax.xaxis.convert_units(x)<EOL>

For artists in an axes, if the xaxis has units support,
        convert *x* using xaxis unit type


ax = getattr(self, '<STR_LIT>', None)<EOL>if ax is None or ax.yaxis is None: return y<EOL>return ax.yaxis.convert_units(y)<EOL>

For artists in an axes, if the yaxis has units support,
        convert *y* using yaxis unit type


self.axes = axes<EOL>

Set the :class:`~matplotlib.axes.Axes` instance in which the
artist resides, if any.

ACCEPTS: an :class:`~matplotlib.axes.Axes` instance


return self.axes<EOL>

Return the :class:`~matplotlib.axes.Axes` instance the artist
resides in, or *None*


oid = self._oid<EOL>self._propobservers[oid] = func<EOL>self._oid += <NUM_LIT:1><EOL>return oid<EOL>

Adds a callback function that will be called whenever one of
the :class:`Artist`'s properties changes.

Returns an *id* that is useful for removing the callback with
:meth:`remove_callback` later.


try: del self._propobservers[oid]<EOL>except KeyError: pass<EOL>

Remove a callback based on its *id*.

.. seealso::
    :meth:`add_callback`


for oid, func in list(self._propobservers.items()):<EOL><INDENT>func(self)<EOL><DEDENT>

Fire an event when property changed, calling all of the
registered callbacks.


return self._transformSet<EOL>

Returns *True* if :class:`Artist` has a transform explicitly
set.


self._transform = t<EOL>self._transformSet = True<EOL>self.pchanged()<EOL>

Set the :class:`~matplotlib.transforms.Transform` instance
used by this artist.

ACCEPTS: :class:`~matplotlib.transforms.Transform` instance


if self._transform is None:<EOL><INDENT>self._transform = IdentityTransform()<EOL><DEDENT>return self._transform<EOL>

Return the :class:`~matplotlib.transforms.Transform`
instance used by this artist.


import traceback<EOL>L = []<EOL>try:<EOL><INDENT>hascursor,info = self.contains(event)<EOL>if hascursor:<EOL><INDENT>L.append(self)<EOL><DEDENT><DEDENT>except:<EOL><INDENT>traceback.print_exc()<EOL>print("<STR_LIT>",self.__class__)<EOL><DEDENT>for a in self.get_children():<EOL><INDENT>L.extend(a.hitlist(event))<EOL><DEDENT>return L<EOL>

List the children of the artist which contain the mouse event *event*.


return []<EOL>

Return a list of the child :class:`Artist`s this
:class:`Artist` contains.


if callable(self._contains): return self._contains(self,mouseevent)<EOL>warnings.warn("<STR_LIT>" % self.__class__.__name__)<EOL>return False,{}<EOL>

Test whether the artist contains the mouse event.

        Returns the truth value and a dictionary of artist specific details of
        selection, such as which points are contained in the pick radius.  See
        individual artists for details.


self._contains = picker<EOL>

Replace the contains test used by this artist. The new picker
should be a callable function which determines whether the
artist is hit by the mouse event::

    hit, props = picker(artist, mouseevent)

If the mouse event is over the artist, return *hit* = *True*
and *props* is a dictionary of properties you want returned
with the contains test.

ACCEPTS: a callable function


return self._contains<EOL>

Return the _contains test used by the artist, or *None* for default.


return (self.figure is not None and<EOL>self.figure.canvas is not None and<EOL>self._picker is not None)<EOL>

Return *True* if :class:`Artist` is pickable.


<EOL>if self.pickable():<EOL><INDENT>picker = self.get_picker()<EOL>if callable(picker):<EOL><INDENT>inside,prop = picker(self,mouseevent)<EOL><DEDENT>else:<EOL><INDENT>inside,prop = self.contains(mouseevent)<EOL><DEDENT>if inside:<EOL><INDENT>self.figure.canvas.pick_event(mouseevent, self, **prop)<EOL><DEDENT><DEDENT>for a in self.get_children():<EOL><INDENT>a.pick(mouseevent)<EOL><DEDENT>

call signature::

  pick(mouseevent)

each child artist will fire a pick event if *mouseevent* is over
the artist and the artist has picker set


self._picker = picker<EOL>

Set the epsilon for picking used by this artist

*picker* can be one of the following:

  * *None*: picking is disabled for this artist (default)

  * A boolean: if *True* then picking will be enabled and the
    artist will fire a pick event if the mouse event is over
    the artist

  * A float: if picker is a number it is interpreted as an
    epsilon tolerance in points and the artist will fire
    off an event if it's data is within epsilon of the mouse
    event.  For some artists like lines and patch collections,
    the artist may provide additional data to the pick event
    that is generated, e.g. the indices of the data within
    epsilon of the pick event

  * A function: if picker is callable, it is a user supplied
    function which determines whether the artist is hit by the
    mouse event::

      hit, props = picker(artist, mouseevent)

    to determine the hit test.  if the mouse event is over the
    artist, return *hit=True* and props is a dictionary of
    properties you want added to the PickEvent attributes.

ACCEPTS: [None|float|boolean|callable]


return self._picker<EOL>

Return the picker object used by this artist


return self.figure is not None<EOL>

Returns True if the artist is assigned to a
:class:`~matplotlib.figure.Figure`.


return self._url<EOL>


self._url = url<EOL>


return self._snap<EOL>

Returns the snap setting which may be:

  * True: snap vertices to the nearest pixel center

  * False: leave vertices as-is

  * None: (auto) If the path contains only rectilinear line
    segments, round to the nearest pixel center

Only supported by the Agg backends.


self._snap = snap<EOL>

Sets the snap setting which may be:

  * True: snap vertices to the nearest pixel center

  * False: leave vertices as-is

  * None: (auto) If the path contains only rectilinear line
    segments, round to the nearest pixel center

Only supported by the Agg backends.


return self.figure<EOL>

Return the :class:`~matplotlib.figure.Figure` instance the
artist belongs to.


self.figure = fig<EOL>self.pchanged()<EOL>

Set the :class:`~matplotlib.figure.Figure` instance the artist
belongs to.

ACCEPTS: a :class:`matplotlib.figure.Figure` instance


self.clipbox = clipbox<EOL>self.pchanged()<EOL>

Set the artist's clip :class:`~matplotlib.transforms.Bbox`.

ACCEPTS: a :class:`matplotlib.transforms.Bbox` instance


def set_clip_path(self, path, transform=None):

from patches import Patch, Rectangle<EOL>success = False<EOL>if transform is None:<EOL><INDENT>if isinstance(path, Rectangle):<EOL><INDENT>self.clipbox = TransformedBbox(Bbox.unit(), path.get_transform())<EOL>self._clippath = None<EOL>success = True<EOL><DEDENT>elif isinstance(path, Patch):<EOL><INDENT>self._clippath = TransformedPath(<EOL>path.get_path(),<EOL>path.get_transform())<EOL>success = True<EOL><DEDENT><DEDENT>if path is None:<EOL><INDENT>self._clippath = None<EOL>success = True<EOL><DEDENT>elif isinstance(path, Path):<EOL><INDENT>self._clippath = TransformedPath(path, transform)<EOL>success = True<EOL><DEDENT>if not success:<EOL><INDENT>print(type(path), type(transform))<EOL>raise TypeError("<STR_LIT>")<EOL><DEDENT>self.pchanged()<EOL>

Set the artist's clip path, which may be:

  * a :class:`~matplotlib.patches.Patch` (or subclass) instance

  * a :class:`~matplotlib.path.Path` instance, in which case
     an optional :class:`~matplotlib.transforms.Transform`
     instance may be provided, which will be applied to the
     path before using it for clipping.

  * *None*, to remove the clipping path

For efficiency, if the path happens to be an axis-aligned
rectangle, this method will set the clipping box to the
corresponding rectangle and set the clipping path to *None*.

ACCEPTS: [ (:class:`~matplotlib.path.Path`,
:class:`~matplotlib.transforms.Transform`) |
:class:`~matplotlib.patches.Patch` | None ]


return self._alpha<EOL>

Return the alpha value used for blending - not supported on all
backends


return self._visible<EOL>


return self._animated<EOL>


return self._clipon<EOL>


return self.clipbox<EOL>


return self._clippath<EOL>


if self._clippath is not None:<EOL><INDENT>return self._clippath.get_transformed_path_and_affine()<EOL><DEDENT>return None, None<EOL>

Return the clip path with the non-affine part of its
transformation applied, and the remaining affine part of its
transformation.


self._clipon = b<EOL>self.pchanged()<EOL>

Set whether artist uses clipping.

ACCEPTS: [True | False]


if self._clipon:<EOL><INDENT>if self.clipbox is not None:<EOL><INDENT>gc.set_clip_rectangle(self.clipbox)<EOL><DEDENT>gc.set_clip_path(self._clippath)<EOL><DEDENT>else:<EOL><INDENT>gc.set_clip_rectangle(None)<EOL>gc.set_clip_path(None)<EOL><DEDENT>


def draw(self, renderer, *args, **kwargs):

if not self.get_visible(): return<EOL>


self._alpha = alpha<EOL>self.pchanged()<EOL>

Set the alpha value used for blending - not supported on
all backends

ACCEPTS: float (0.0 transparent through 1.0 opaque)


self._lod = on<EOL>self.pchanged()<EOL>

Set Level of Detail on or off.  If on, the artists may examine
things like the pixel width of the axes and draw a subset of
their contents accordingly

ACCEPTS: [True | False]


self._visible = b<EOL>self.pchanged()<EOL>

Set the artist's visiblity.

ACCEPTS: [True | False]


self._animated = b<EOL>self.pchanged()<EOL>

Set the artist's animation state.

ACCEPTS: [True | False]


store = self.eventson<EOL>self.eventson = False<EOL>changed = False<EOL>for k,v in list(props.items()):<EOL><INDENT>func = getattr(self, '<STR_LIT>'+k, None)<EOL>if func is None or not callable(func):<EOL><INDENT>raise AttributeError('<STR_LIT>'%k)<EOL><DEDENT>func(v)<EOL>changed = True<EOL><DEDENT>self.eventson = store<EOL>if changed: self.pchanged()<EOL>

Update the properties of this :class:`Artist` from the
dictionary *prop*.


return self._label<EOL>

Get the label used for this artist in the legend.


self._label = s<EOL>self.pchanged()<EOL>

Set the label to *s* for auto legend.

ACCEPTS: any string


return self.zorder<EOL>


self.zorder = level<EOL>self.pchanged()<EOL>

Set the zorder for the artist.  Artists with lower zorder
values are drawn first.

ACCEPTS: any number


self._transform = other._transform<EOL>self._transformSet = other._transformSet<EOL>self._visible = other._visible<EOL>self._alpha = other._alpha<EOL>self.clipbox = other.clipbox<EOL>self._clipon = other._clipon<EOL>self._clippath = other._clippath<EOL>self._lod = other._lod<EOL>self._label = other._label<EOL>self.pchanged()<EOL>


ret = []<EOL>for k,v in list(kwargs.items()):<EOL><INDENT>k = k.lower()<EOL>funcName = "<STR_LIT>"%k<EOL>func = getattr(self,funcName)<EOL>ret.extend( [func(v)] )<EOL><DEDENT>return ret<EOL>

A tkstyle set command, pass *kwargs* to set properties


if match is None: <EOL><INDENT>def matchfunc(x): return True<EOL><DEDENT>elif cbook.issubclass_safe(match, Artist):<EOL><INDENT>def matchfunc(x):<EOL><INDENT>return isinstance(x, match)<EOL><DEDENT><DEDENT>elif callable(match):<EOL><INDENT>matchfunc = match<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>artists = []<EOL>for c in self.get_children():<EOL><INDENT>if matchfunc(c):<EOL><INDENT>artists.append(c)<EOL><DEDENT>artists.extend([thisc for thisc in c.findobj(matchfunc) if matchfunc(thisc)])<EOL><DEDENT>if matchfunc(self):<EOL><INDENT>artists.append(self)<EOL><DEDENT>return artists<EOL>

pyplot signature:
  findobj(o=gcf(), match=None)

Recursively find all :class:matplotlib.artist.Artist instances
contained in self.

*match* can be

  - None: return all objects contained in artist (including artist)

  - function with signature ``boolean = match(artist)`` used to filter matches

  - class instance: eg Line2D.  Only return artists of class type

.. plot:: mpl_examples/pylab_examples/findobj_demo.py


if cbook.iterable(o) and len(o): o = o[<NUM_LIT:0>]<EOL>self.oorig = o<EOL>if not isinstance(o, type):<EOL><INDENT>o = type(o)<EOL><DEDENT>self.o = o<EOL>self.aliasd = self.get_aliases()<EOL>

Initialize the artist inspector with an
:class:`~matplotlib.artist.Artist` or sequence of
:class:`Artists`.  If a sequence is used, we assume it is a
homogeneous sequence (all :class:`Artists` are of the same
type) and it is your responsibility to make sure this is so.


names = [name for name in dir(self.o) if<EOL>(name.startswith('<STR_LIT>') or name.startswith('<STR_LIT>'))<EOL>and callable(getattr(self.o,name))]<EOL>aliases = {}<EOL>for name in names:<EOL><INDENT>func = getattr(self.o, name)<EOL>if not self.is_alias(func): continue<EOL>docstring = func.__doc__<EOL>fullname = docstring[<NUM_LIT:10>:]<EOL>aliases.setdefault(fullname[<NUM_LIT:4>:], {})[name[<NUM_LIT:4>:]] = None<EOL><DEDENT>return aliases<EOL>

Get a dict mapping *fullname* -> *alias* for each *alias* in
the :class:`~matplotlib.artist.ArtistInspector`.

Eg., for lines::

  {'markerfacecolor': 'mfc',
   'linewidth'      : 'lw',
  }


name = '<STR_LIT>'%attr<EOL>if not hasattr(self.o, name):<EOL><INDENT>raise AttributeError('<STR_LIT>'%(self.o,name))<EOL><DEDENT>func = getattr(self.o, name)<EOL>docstring = func.__doc__<EOL>if docstring is None: return '<STR_LIT>'<EOL>if docstring.startswith('<STR_LIT>'):<EOL><INDENT>return None<EOL><DEDENT>match = self._get_valid_values_regex.search(docstring)<EOL>if match is not None:<EOL><INDENT>return match.group(<NUM_LIT:1>).replace('<STR_LIT:\n>', '<STR_LIT:U+0020>')<EOL><DEDENT>return '<STR_LIT>'<EOL>

Get the legal arguments for the setter associated with *attr*.

This is done by querying the docstring of the function *set_attr*
for a line that begins with ACCEPTS:

Eg., for a line linestyle, return
[ '-' | '--' | '-.' | ':' | 'steps' | 'None' ]


setters = []<EOL>for name in dir(self.o):<EOL><INDENT>if not name.startswith('<STR_LIT>'): continue<EOL>o = getattr(self.o, name)<EOL>if not callable(o): continue<EOL>func = o<EOL>if self.is_alias(func): continue<EOL>source_class = self.o.__module__ + "<STR_LIT:.>" + self.o.__name__<EOL>for cls in self.o.mro():<EOL><INDENT>if name in cls.__dict__:<EOL><INDENT>source_class = cls.__module__ + "<STR_LIT:.>" + cls.__name__<EOL>break<EOL><DEDENT><DEDENT>setters.append((name[<NUM_LIT:4>:], source_class + "<STR_LIT:.>" + name))<EOL><DEDENT>return setters<EOL>

Get the attribute strings and a full path to where the setter
is defined for all setters in an object.


return [prop for prop, target in self._get_setters_and_targets()]<EOL>

Get the attribute strings with setters for object.  Eg., for a line,
return ``['markerfacecolor', 'linewidth', ....]``.


ds = o.__doc__<EOL>if ds is None: return False<EOL>return ds.startswith('<STR_LIT>')<EOL>

Return *True* if method object *o* is an alias for another
function.


if s in self.aliasd:<EOL><INDENT>return s + '<STR_LIT>'.join(['<STR_LIT>' % x for x in list(self.aliasd[s].keys())])<EOL><DEDENT>else:<EOL><INDENT>return s<EOL><DEDENT>

return 'PROPNAME or alias' if *s* has an alias, else return
PROPNAME.

E.g. for the line markerfacecolor property, which has an
alias, return 'markerfacecolor or mfc' and for the transform
property, which does not, return 'transform'


if s in self.aliasd:<EOL><INDENT>aliases = '<STR_LIT>'.join(['<STR_LIT>' % x for x in list(self.aliasd[s].keys())])<EOL><DEDENT>else:<EOL><INDENT>aliases = '<STR_LIT>'<EOL><DEDENT>return '<STR_LIT>' % (s, target, aliases)<EOL>

return 'PROPNAME or alias' if *s* has an alias, else return
PROPNAME formatted for ReST

E.g. for the line markerfacecolor property, which has an
alias, return 'markerfacecolor or mfc' and for the transform
property, which does not, return 'transform'


def pprint_setters(self, prop=None, leadingspace=<NUM_LIT:2>):

if leadingspace:<EOL><INDENT>pad = '<STR_LIT:U+0020>'*leadingspace<EOL><DEDENT>else:<EOL><INDENT>pad  = '<STR_LIT>'<EOL><DEDENT>if prop is not None:<EOL><INDENT>accepts = self.get_valid_values(prop)<EOL>return '<STR_LIT>' %(pad, prop, accepts)<EOL><DEDENT>attrs = self._get_setters_and_targets()<EOL>attrs.sort()<EOL>lines = []<EOL>for prop, path in attrs:<EOL><INDENT>accepts = self.get_valid_values(prop)<EOL>name = self.aliased_name(prop)<EOL>lines.append('<STR_LIT>' %(pad, name, accepts))<EOL><DEDENT>return lines<EOL>

If *prop* is *None*, return a list of strings of all settable properies
and their valid values.

If *prop* is not *None*, it is a valid property name and that
property will be returned as a string of property : valid
values.


def pprint_setters_rest(self, prop=None, leadingspace=<NUM_LIT:2>):

if leadingspace:<EOL><INDENT>pad = '<STR_LIT:U+0020>'*leadingspace<EOL><DEDENT>else:<EOL><INDENT>pad  = '<STR_LIT>'<EOL><DEDENT>if prop is not None:<EOL><INDENT>accepts = self.get_valid_values(prop)<EOL>return '<STR_LIT>' %(pad, prop, accepts)<EOL><DEDENT>attrs = self._get_setters_and_targets()<EOL>attrs.sort()<EOL>lines = []<EOL>names = [self.aliased_name_rest(prop, target) for prop, target in attrs]<EOL>accepts = [self.get_valid_values(prop) for prop, target in attrs]<EOL>col0_len = max([len(n) for n in names])<EOL>col1_len = max([len(a) for a in accepts])<EOL>table_formatstr = pad + '<STR_LIT:=>'*col0_len + '<STR_LIT:U+0020>' + '<STR_LIT:=>'*col1_len<EOL>lines.append('<STR_LIT>')<EOL>lines.append(table_formatstr)<EOL>lines.append(pad + '<STR_LIT>'.ljust(col0_len+<NUM_LIT:3>) +'<STR_LIT>'.ljust(col1_len))<EOL>lines.append(table_formatstr)<EOL>lines.extend([pad + n.ljust(col0_len+<NUM_LIT:3>) + a.ljust(col1_len)<EOL>for n, a in zip(names, accepts)])<EOL>lines.append(table_formatstr)<EOL>lines.append('<STR_LIT>')<EOL>return lines<EOL>for prop, path in attrs:<EOL><INDENT>accepts = self.get_valid_values(prop)<EOL>name = self.aliased_name_rest(prop, path)<EOL>lines.append('<STR_LIT>' %(pad, name, accepts))<EOL><DEDENT>return lines<EOL>

If *prop* is *None*, return a list of strings of all settable properies
and their valid values.  Format the output for ReST

If *prop* is not *None*, it is a valid property name and that
property will be returned as a string of property : valid
values.


o = self.oorig<EOL>getters = [name for name in dir(o)<EOL>if name.startswith('<STR_LIT>')<EOL>and callable(getattr(o, name))]<EOL>getters.sort()<EOL>lines = []<EOL>for name in getters:<EOL><INDENT>func = getattr(o, name)<EOL>if self.is_alias(func): continue<EOL>try: val = func()<EOL>except: continue<EOL>if getattr(val, '<STR_LIT>', ()) != () and len(val)><NUM_LIT:6>:<EOL><INDENT>s = str(val[:<NUM_LIT:6>]) + '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>s = str(val)<EOL><DEDENT>s = s.replace('<STR_LIT:\n>', '<STR_LIT:U+0020>')<EOL>if len(s)><NUM_LIT:50>:<EOL><INDENT>s = s[:<NUM_LIT:50>] + '<STR_LIT>'<EOL><DEDENT>name = self.aliased_name(name[<NUM_LIT:4>:])<EOL>lines.append('<STR_LIT>' %(name, s))<EOL><DEDENT>return lines<EOL>

Return the getters and actual values as list of strings.


if match is None: <EOL><INDENT>def matchfunc(x): return True<EOL><DEDENT>elif issubclass(match, Artist):<EOL><INDENT>def matchfunc(x):<EOL><INDENT>return isinstance(x, match)<EOL><DEDENT><DEDENT>elif callable(match):<EOL><INDENT>matchfunc = func<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>artists = []<EOL>for c in self.get_children():<EOL><INDENT>if matchfunc(c):<EOL><INDENT>artists.append(c)<EOL><DEDENT>artists.extend([thisc for thisc in c.findobj(matchfunc) if matchfunc(thisc)])<EOL><DEDENT>if matchfunc(self):<EOL><INDENT>artists.append(self)<EOL><DEDENT>return artists<EOL>

Recursively find all :class:`matplotlib.artist.Artist`
instances contained in *self*.

If *match* is not None, it can be

  - function with signature ``boolean = match(artist)``

  - class instance: eg :class:`~matplotlib.lines.Line2D`

used to filter matches.


return cls.__subclasses__() + [<EOL>g for s in cls.__subclasses__() for g in _allSubclasses(s)<EOL>]<EOL>

Get all subclasses
:param cls: The class to get subclasses from
:return: list with all subclasses


if isinstance(obj, dict):<EOL><INDENT>attrs = obj<EOL><DEDENT>elif hasattr(obj, "<STR_LIT>"):<EOL><INDENT>attrs = {attr: getattr(obj, attr) for attr in obj.__slots__}<EOL><DEDENT>elif hasattr(obj, "<STR_LIT>"):<EOL><INDENT>attrs = obj.__dict__<EOL><DEDENT>testParams = SERIALIZABLE_SUBCLASSES[obj.__class__.__name__]<EOL>if "<STR_LIT>" in testParams:<EOL><INDENT>for f in testParams["<STR_LIT>"]:<EOL><INDENT>if f in attrs:<EOL><INDENT>del attrs[f]<EOL><DEDENT><DEDENT><DEDENT>return attrs<EOL>


if os.path.isfile(fname):<EOL><INDENT>os.remove(fname)<EOL><DEDENT>

Clean up function used to delete files created by the test
:param fname: File to be deleted
:return:


def customAssertArrayEquals(self, a1, a2, msg=None):

numpy.testing.assert_equal(a1, a2, msg)<EOL>

Function used by `addTypeEqualityFunc` comparing numpy arrays


def customAssertSequenceEquals(self, l1, l2, msg=None):

self.assertEqual(len(l1), len(l2), msg)<EOL>for i in range(len(l1)):<EOL><INDENT>first = l1[i]<EOL>second = l2[i]<EOL>if type(first).__name__ in SERIALIZABLE_SUBCLASSES:<EOL><INDENT>first = _getAttributes(first)<EOL>second = _getAttributes(second)<EOL><DEDENT>self.assertEqual(first, second, msg)<EOL><DEDENT>

Function used by `addTypeEqualityFunc` comparing sequences


def customAssertDictEquals(self, d1, d2, msg=None):

self.assertIsInstance(d1, dict, '<STR_LIT>')<EOL>self.assertIsInstance(d2, dict, '<STR_LIT>')<EOL>self.assertEqual(len(d1), len(d2), msg + str(d1) + '<STR_LIT>' + str(d2))<EOL>for k, _ in list(d1.items()):<EOL><INDENT>if k not in d2:<EOL><INDENT>raise AssertionError(repr(k))<EOL><DEDENT>first = d1[k]<EOL>second = d2[k]<EOL>if type(first).__name__ in SERIALIZABLE_SUBCLASSES:<EOL><INDENT>first = _getAttributes(first)<EOL>second = _getAttributes(second)<EOL><DEDENT>self.assertEqual(first, second, '<STR_LIT>' % (k, msg))<EOL><DEDENT>

Function used by `addTypeEqualityFunc` comparing dicts


def overlapsForRelativeAreas(n, w, initPosition, initRadius, dPosition=None,<EOL>dRadius=<NUM_LIT:0>, num=<NUM_LIT:100>, verbose=False):

encoder = CoordinateEncoder(name="<STR_LIT>", n=n, w=w)<EOL>overlaps = np.empty(num)<EOL>outputA = encode(encoder, np.array(initPosition), initRadius)<EOL>for i in range(num):<EOL><INDENT>newPosition = initPosition if dPosition is None else (<EOL>initPosition + (i + <NUM_LIT:1>) * dPosition)<EOL>newRadius = initRadius + (i + <NUM_LIT:1>) * dRadius<EOL>outputB = encode(encoder, newPosition, newRadius)<EOL>overlaps[i] = overlap(outputA, outputB)<EOL><DEDENT>if verbose:<EOL><INDENT>print()<EOL>print(("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>").format(<EOL>n, w, initPosition, initRadius, dPosition, dRadius))<EOL>print("<STR_LIT>".format(np.average(overlaps)))<EOL>print("<STR_LIT>".format(np.max(overlaps)))<EOL><DEDENT>return overlaps<EOL>

Return overlaps between an encoding and other encodings relative to it

:param n: the size of the encoder output
:param w: the number of active bits in the encoder output
:param initPosition: the position of the first encoding
:param initRadius: the radius of the first encoding
:param dPosition: the offset to apply to each subsequent position
:param dRadius: the offset to apply to each subsequent radius
:param num: the number of encodings to generate
:param verbose: whether to print verbose output


def overlapsForUnrelatedAreas(n, w, radius, repetitions=<NUM_LIT:100>, verbose=False):

return overlapsForRelativeAreas(n, w, np.array([<NUM_LIT:0>, <NUM_LIT:0>]), radius,<EOL>dPosition=np.array([<NUM_LIT:0>, radius * <NUM_LIT:10>]),<EOL>num=repetitions, verbose=verbose)<EOL>

Return overlaps between an encoding and other, unrelated encodings


return (x & y).sum()<EOL>

Given two binary arrays, compute their overlap. The overlap is the number
of bits where x[i] and y[i] are both 1


for i in range(encoder.minIndex, encoder.maxIndex+<NUM_LIT:1>, <NUM_LIT:1>):<EOL><INDENT>for j in range(i+<NUM_LIT:1>, encoder.maxIndex+<NUM_LIT:1>, subsampling):<EOL><INDENT>if not encoder._overlapOK(i, j):<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>return True<EOL>

Given an encoder, calculate overlaps statistics and ensure everything is ok.
We don't check every possible combination for speed reasons.


print('<STR_LIT>'.join('<STR_LIT:1>' if k != <NUM_LIT:0> else '<STR_LIT:.>' for k in x))<EOL>


numCols = numOnes * numPatterns<EOL>p = []<EOL>for i in range(numPatterns):<EOL><INDENT>x = np.zeros(numCols, dtype='<STR_LIT>')<EOL>x[i*numOnes:(i + <NUM_LIT:1>)*numOnes] = <NUM_LIT:1><EOL>p.append(x)<EOL><DEDENT>return p<EOL>

Very simple patterns. Each pattern has numOnes consecutive
    bits on. There are numPatterns*numOnes bits in the vector. These patterns
    are used as elements of sequences when building up a training set.


<EOL>minThreshold = <NUM_LIT:4><EOL>activationThreshold = <NUM_LIT:5><EOL>newSynapseCount = <NUM_LIT:7><EOL>initialPerm = <NUM_LIT><EOL>connectedPerm = <NUM_LIT:0.5><EOL>permanenceInc = <NUM_LIT:0.1><EOL>permanenceDec = <NUM_LIT><EOL>globalDecay = <NUM_LIT:0><EOL>cellsPerColumn = <NUM_LIT:1><EOL>cppTm = BacktrackingTMCPP(numberOfCols=numCols,<EOL>cellsPerColumn=cellsPerColumn,<EOL>initialPerm=initialPerm,<EOL>connectedPerm=connectedPerm,<EOL>minThreshold=minThreshold,<EOL>newSynapseCount=newSynapseCount,<EOL>permanenceInc=permanenceInc,<EOL>permanenceDec=permanenceDec,<EOL>activationThreshold=activationThreshold,<EOL>globalDecay=globalDecay, burnIn=<NUM_LIT:1>,<EOL>seed=_SEED, verbosity=VERBOSITY,<EOL>checkSynapseConsistency=True,<EOL>pamLength=<NUM_LIT:1000>)<EOL>cppTm.retrieveLearningStates = True<EOL>pyTm = BacktrackingTM(numberOfCols=numCols,<EOL>cellsPerColumn=cellsPerColumn,<EOL>initialPerm=initialPerm,<EOL>connectedPerm=connectedPerm,<EOL>minThreshold=minThreshold,<EOL>newSynapseCount=newSynapseCount,<EOL>permanenceInc=permanenceInc,<EOL>permanenceDec=permanenceDec,<EOL>activationThreshold=activationThreshold,<EOL>globalDecay=globalDecay, burnIn=<NUM_LIT:1>,<EOL>seed=_SEED, verbosity=VERBOSITY,<EOL>pamLength=<NUM_LIT:1000>)<EOL>return cppTm, pyTm<EOL>

Create two instances of temporal poolers (backtracking_tm.py
    and backtracking_tm_cpp.py) with identical parameter settings.


trainingSet = _getSimplePatterns(<NUM_LIT:10>, <NUM_LIT:10>)<EOL>for _ in range(<NUM_LIT:2>):<EOL><INDENT>for seq in trainingSet[<NUM_LIT:0>:<NUM_LIT:5>]:<EOL><INDENT>for _ in range(<NUM_LIT:10>):<EOL><INDENT>tm.learn(seq)<EOL><DEDENT>tm.reset()<EOL><DEDENT><DEDENT>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>tm.collectStats = True<EOL>for seq in trainingSet[<NUM_LIT:0>:<NUM_LIT:5>]:<EOL><INDENT>tm.reset()<EOL>tm.resetStats()<EOL>for _ in range(<NUM_LIT:10>):<EOL><INDENT>tm.infer(seq)<EOL>if VERBOSITY > <NUM_LIT:1> :<EOL><INDENT>print()<EOL>_printOneTrainingVector(seq)<EOL>tm.printStates(False, False)<EOL>print()<EOL>print()<EOL><DEDENT><DEDENT>if VERBOSITY > <NUM_LIT:1>:<EOL><INDENT>print(tm.getStats())<EOL><DEDENT>self.assertGreater(tm.getStats()['<STR_LIT>'], <NUM_LIT>)<EOL>print(("<STR_LIT>",<EOL>tm.getStats()['<STR_LIT>']))<EOL><DEDENT>print("<STR_LIT>")<EOL>

Test creation, pickling, and basic run of learning and inference.


dateList = []<EOL>td = datetime.timedelta(minutes=<NUM_LIT:1>)<EOL>curDate = startDatetime + td<EOL>for _ in range(numSamples):<EOL><INDENT>dateList.append(curDate)<EOL>curDate = curDate + td<EOL><DEDENT>return dateList<EOL>

Generate a sequence of sample dates starting at startDatetime and incrementing
every minute.


if origData is None:<EOL><INDENT>origData = []<EOL><DEDENT>if len(origData) > <NUM_LIT:0>:<EOL><INDENT>lastDate = origData[-<NUM_LIT:1>][<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>lastDate = datetime.datetime(<NUM_LIT>, <NUM_LIT:2>, <NUM_LIT:3>)<EOL><DEDENT>dateList = _getDateList(numSamples, lastDate)<EOL>data = copy.copy(origData)<EOL>for idx, date in enumerate(dateList):<EOL><INDENT>if (spikePeriod > <NUM_LIT:0>) and ( (idx + <NUM_LIT:1>) % spikePeriod == <NUM_LIT:0>):<EOL><INDENT>data.append([date, idx, spikeValue])<EOL><DEDENT>else:<EOL><INDENT>data.append([date, idx, <NUM_LIT:0.0>])<EOL><DEDENT><DEDENT>return data<EOL>

Add sample anomaly data to the existing data list and return it.
Note: this does not modify the original data list
Note 2: here we just add in increasing integers as the metric value


def runSideBySide(self, params, seed = None,<EOL>learnMode = None,<EOL>convertEveryIteration = False):

randomState = getNumpyRandomGenerator(seed)<EOL>cppSp = CreateSP("<STR_LIT>", params)<EOL>pySp = CreateSP("<STR_LIT>", params)<EOL>self.compare(pySp, cppSp)<EOL>numColumns = pySp.getNumColumns()<EOL>numInputs = pySp.getNumInputs()<EOL>threshold = <NUM_LIT><EOL>inputMatrix = (<EOL>randomState.rand(numRecords,numInputs) > threshold).astype(uintType)<EOL>for i in range(numRecords):<EOL><INDENT>if learnMode is None:<EOL><INDENT>learn = (randomState.rand() > <NUM_LIT:0.5>)<EOL><DEDENT>else:<EOL><INDENT>learn = learnMode<EOL><DEDENT>if self.verbosity > <NUM_LIT:1>:<EOL><INDENT>print("<STR_LIT>",i,"<STR_LIT>",learn)<EOL><DEDENT>PyActiveArray = numpy.zeros(numColumns).astype(uintType)<EOL>CppActiveArray = numpy.zeros(numColumns).astype(uintType)<EOL>inputVector = inputMatrix[i,:]<EOL>pySp.compute(inputVector, learn, PyActiveArray)<EOL>cppSp.compute(inputVector, learn, CppActiveArray)<EOL>self.assertListEqual(list(PyActiveArray), list(CppActiveArray))<EOL>self.compare(pySp,cppSp)<EOL>cppBoostFactors = numpy.zeros(numColumns, dtype=realType)<EOL>cppSp.getBoostFactors(cppBoostFactors)<EOL>pySp.setBoostFactors(cppBoostFactors)<EOL>if convertEveryIteration or ((i+<NUM_LIT:1>)%<NUM_LIT:10> == <NUM_LIT:0>):<EOL><INDENT>convertPermanences(pySp, cppSp)<EOL><DEDENT><DEDENT>

Run the PY and CPP implementations side by side on random inputs.
If seed is None a random seed will be chosen based on time, otherwise
the fixed seed will be used.

If learnMode is None learning will be randomly turned on and off.
If it is False or True then set it accordingly.

If convertEveryIteration is True, the CPP will be copied from the PY
instance on every iteration just before each compute.


def frequency(self,<EOL>n=<NUM_LIT:15>,<EOL>w=<NUM_LIT:7>,<EOL>columnDimensions = <NUM_LIT>,<EOL>numActiveColumnsPerInhArea = <NUM_LIT>,<EOL>stimulusThreshold = <NUM_LIT:0>,<EOL>spSeed = <NUM_LIT:1>,<EOL>spVerbosity = <NUM_LIT:0>,<EOL>numColors = <NUM_LIT:2>,<EOL>seed=<NUM_LIT>,<EOL>minVal=<NUM_LIT:0>,<EOL>maxVal=<NUM_LIT:10>,<EOL>encoder = '<STR_LIT>',<EOL>forced=True):

print("<STR_LIT>")<EOL>print(encoder, '<STR_LIT>', '<STR_LIT>', seed, '<STR_LIT>', numColors, '<STR_LIT>')<EOL>spImpl = SpatialPooler(<EOL>columnDimensions=(columnDimensions, <NUM_LIT:1>),<EOL>inputDimensions=(<NUM_LIT:1>, n),<EOL>potentialRadius=n/<NUM_LIT:2>,<EOL>numActiveColumnsPerInhArea=numActiveColumnsPerInhArea,<EOL>spVerbosity=spVerbosity,<EOL>stimulusThreshold=stimulusThreshold,<EOL>potentialPct=<NUM_LIT:0.5>,<EOL>seed=spSeed,<EOL>globalInhibition=True,<EOL>)<EOL>rnd.seed(seed)<EOL>numpy.random.seed(seed)<EOL>colors = []<EOL>coincs = []<EOL>reUsedCoincs = []<EOL>spOutput = []<EOL>patterns = set([])<EOL>if encoder=='<STR_LIT>':<EOL><INDENT>enc = scalar.ScalarEncoder(name='<STR_LIT>', w=w, n=n, minval=minVal,<EOL>maxval=maxVal, periodic=False, forced=True) <EOL>for y in range(numColors):<EOL><INDENT>temp = enc.encode(rnd.random()*maxVal)<EOL>colors.append(numpy.array(temp, dtype=numpy.uint32))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for y in range(numColors):<EOL><INDENT>sdr = numpy.zeros(n, dtype=numpy.uint32)<EOL>sdr[rnd.sample(range(n), w)] = <NUM_LIT:1><EOL>colors.append(sdr)<EOL><DEDENT><DEDENT>print('<STR_LIT>', numColors, '<STR_LIT>')<EOL>startTime = time.time()<EOL>for i in range(numColors):<EOL><INDENT>spInput = colors[i]<EOL>onCells = numpy.zeros(columnDimensions, dtype=numpy.uint32)<EOL>spImpl.compute(spInput, True, onCells)<EOL>spOutput.append(onCells.tolist())<EOL>activeCoincIndices = set(onCells.nonzero()[<NUM_LIT:0>])<EOL>reUsed = activeCoincIndices.intersection(patterns)<EOL>if len(reUsed) == <NUM_LIT:0>:<EOL><INDENT>coincs.append((i, activeCoincIndices, colors[i]))<EOL><DEDENT>else:<EOL><INDENT>reUsedCoincs.append((i, activeCoincIndices, colors[i]))<EOL><DEDENT>patterns.update(activeCoincIndices)<EOL>if (i + <NUM_LIT:1>) % <NUM_LIT:100> == <NUM_LIT:0>:<EOL><INDENT>print('<STR_LIT>', i + <NUM_LIT:1>)<EOL>print("<STR_LIT>" % (time.time() - startTime))<EOL>print(len(reUsedCoincs), "<STR_LIT>")<EOL><DEDENT><DEDENT>summ = []<EOL>for z in coincs:<EOL><INDENT>summ.append(sum([len(z[<NUM_LIT:1>].intersection(y[<NUM_LIT:1>])) for y in reUsedCoincs]))<EOL><DEDENT>zeros = len([x for x in summ if x==<NUM_LIT:0>])<EOL>factor = max(summ)*len(summ)/sum(summ)<EOL>if len(reUsed) < <NUM_LIT:10>:<EOL><INDENT>self.assertLess(factor, <NUM_LIT>,<EOL>"<STR_LIT>" % (<EOL>factor, <NUM_LIT>))<EOL>self.assertLess(zeros, <NUM_LIT>*len(summ),<EOL>"<STR_LIT>" % (<EOL>zeros, <NUM_LIT>*len(summ)))<EOL><DEDENT>else:<EOL><INDENT>self.assertLess(factor, <NUM_LIT:8>,<EOL>"<STR_LIT>" % (<EOL>factor, <NUM_LIT:8>))<EOL>self.assertLess(zeros, <NUM_LIT:12>,<EOL>"<STR_LIT>" % (<EOL>zeros, <NUM_LIT:12>))<EOL><DEDENT>

Helper function that tests whether the SP predicts the most
        frequent record


def basicComputeLoop(self, imp, params, inputSize, columnDimensions,<EOL>seed = None):

sp = CreateSP(imp,params)<EOL>numRecords = <NUM_LIT:100><EOL>randomState = getNumpyRandomGenerator(seed)<EOL>inputMatrix = (<EOL>randomState.rand(numRecords,inputSize) > <NUM_LIT>).astype(uintType)<EOL>y = numpy.zeros(columnDimensions, dtype = uintType)<EOL>dutyCycles = numpy.zeros(columnDimensions, dtype = uintType)<EOL>for v in inputMatrix:<EOL><INDENT>y.fill(<NUM_LIT:0>)<EOL>sp.compute(v, True, y)<EOL>self.assertEqual(sp.getNumActiveColumnsPerInhArea(),y.sum())<EOL>self.assertEqual(<NUM_LIT:0>,y.min())<EOL>self.assertEqual(<NUM_LIT:1>,y.max())<EOL><DEDENT>for v in inputMatrix:<EOL><INDENT>y.fill(<NUM_LIT:0>)<EOL>sp.compute(v, False, y)<EOL>self.assertEqual(sp.getNumActiveColumnsPerInhArea(),y.sum())<EOL>self.assertEqual(<NUM_LIT:0>,y.min())<EOL>self.assertEqual(<NUM_LIT:1>,y.max())<EOL><DEDENT>

Feed in some vectors and retrieve outputs. Ensure the right number of
columns win, that we always get binary outputs, and that nothing crashes.


self.assertEqual(tm1, tm2, tm1.diff(tm2))<EOL>self.assertTrue(fdrutilities.tmDiff2(tm1, tm2, <NUM_LIT:1>, False))<EOL>

Asserts that two TM instances are the same.

        This is temporarily disabled since it does not work with the C++
        implementation of the TM.


@staticmethod<EOL><INDENT>def generateSequence(n=<NUM_LIT:10>, numCols=<NUM_LIT:100>, minOnes=<NUM_LIT>, maxOnes=<NUM_LIT>):<DEDENT>

return [None] + [BacktrackingTMTest.generatePattern(numCols, minOnes,<EOL>maxOnes)<EOL>for _ in range(n)]<EOL>


@staticmethod<EOL><INDENT>def generatePattern(numCols=<NUM_LIT:100>, minOnes=<NUM_LIT>, maxOnes=<NUM_LIT>):<DEDENT>

assert minOnes < maxOnes<EOL>assert maxOnes < numCols<EOL>nOnes = random.randint(minOnes, maxOnes)<EOL>ind = random.sample(range(numCols), nOnes)<EOL>x = numpy.zeros(numCols, dtype='<STR_LIT>')<EOL>x[ind] = <NUM_LIT:1><EOL>return x<EOL>

Generate a single test pattern with given parameters.

        Parameters:
          numCols: Number of columns in each pattern.
          minOnes: The minimum number of 1's in each pattern.
          maxOnes: The maximum number of 1's in each pattern.


return ((x + y) == <NUM_LIT:2>).sum()<EOL>


for k1, v1 in sdrDict.items():<EOL><INDENT>for k2, v2 in sdrDict.items():<EOL><INDENT>if (k2 != k1) and ((v1 == v2).sum() == v1.size):<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>return True<EOL>

Return True iff all the SDR's in the dict are unique.


self.inputSize = <NUM_LIT><EOL>self.columnDimensions = <NUM_LIT><EOL>self.x = numpy.zeros((<NUM_LIT:5>, self.inputSize), dtype=uintType)<EOL>self.x[<NUM_LIT:0>, <NUM_LIT:0>:<NUM_LIT:20>]  = <NUM_LIT:1>   <EOL>self.x[<NUM_LIT:1>, <NUM_LIT:10>:<NUM_LIT:30>] = <NUM_LIT:1>   <EOL>self.x[<NUM_LIT:2>, <NUM_LIT:30>:<NUM_LIT:50>] = <NUM_LIT:1>   <EOL>self.x[<NUM_LIT:3>, <NUM_LIT:50>:<NUM_LIT>] = <NUM_LIT:1>   <EOL>self.x[<NUM_LIT:4>, <NUM_LIT>:<NUM_LIT>] = <NUM_LIT:1>   <EOL>self.winningIteration = numpy.zeros(self.columnDimensions)<EOL>self.lastSDR = {}<EOL>self.spImplementation = "<STR_LIT:None>"<EOL>self.sp = None<EOL>self.params = {<EOL>'<STR_LIT>':            [self.inputSize],<EOL>'<STR_LIT>':           [self.columnDimensions],<EOL>'<STR_LIT>':            self.inputSize,<EOL>'<STR_LIT>':               <NUM_LIT>,<EOL>'<STR_LIT>':           True,<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>'<STR_LIT>':           <NUM_LIT:0.0>,<EOL>'<STR_LIT>':         <NUM_LIT:0.0>,<EOL>'<STR_LIT>':            <NUM_LIT:10>,<EOL>'<STR_LIT>':              <NUM_LIT>,<EOL>'<STR_LIT>':                       SEED,<EOL>}<EOL>print("<STR_LIT>", self.params['<STR_LIT>'])<EOL>

Set various constants. Create the input patterns and the spatial pooler


activeDutyCycle = numpy.zeros(self.columnDimensions, dtype=GetNTAReal())<EOL>self.sp.getActiveDutyCycles(activeDutyCycle)<EOL>boost = numpy.zeros(self.columnDimensions, dtype=GetNTAReal())<EOL>self.sp.getBoostFactors(boost)<EOL>print("<STR_LIT>", (<EOL>self.sp.getIterationNum() ),"<STR_LIT>")<EOL>print("<STR_LIT>", self.spImplementation)<EOL>print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>", (<EOL>activeDutyCycle.max(), activeDutyCycle.min() ))<EOL>print("<STR_LIT>", (<EOL>activeDutyCycle[activeDutyCycle><NUM_LIT:0>].mean() ))<EOL>print("<STR_LIT>", activeDutyCycle)<EOL>print()<EOL>print("<STR_LIT>", boost)<EOL>print()<EOL>print("<STR_LIT>")<EOL>print(self.winningIteration)<EOL>print("<STR_LIT>", (<EOL>self.columnDimensions - (self.winningIteration==<NUM_LIT:0>).sum() ))<EOL>

Helpful debug print statements while debugging this test.


<EOL>self.assertTrue(_areAllSDRsUnique(self.lastSDR), "<STR_LIT>")<EOL>self.assertGreater(_computeOverlap(self.lastSDR[<NUM_LIT:0>], self.lastSDR[<NUM_LIT:1>]), <NUM_LIT:9>,<EOL>"<STR_LIT>")<EOL>for i in [<NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>]:<EOL><INDENT>for j in range(<NUM_LIT:5>):<EOL><INDENT>if (i!=j):<EOL><INDENT>self.assertLess(_computeOverlap(self.lastSDR[i], self.lastSDR[j]),<EOL><NUM_LIT>, "<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>

Verify that all SDRs have the properties desired for this test.

The bounds for checking overlap are set fairly loosely here since there is
some variance due to randomness and the artificial parameters used in this
test.


self.sp = CreateSP(imp, self.params)<EOL>self.spImplementation = imp<EOL>self.winningIteration.fill(<NUM_LIT:0>)<EOL>self.lastSDR = {}<EOL>self.boostTestPhase1()<EOL>self.boostTestPhase2()<EOL>self.boostTestPhase3()<EOL>self.boostTestPhase4()<EOL>


def _sampleDistribution(params, numSamples, verbosity=<NUM_LIT:0>):

if "<STR_LIT:name>" in params:<EOL><INDENT>if params["<STR_LIT:name>"] == "<STR_LIT>":<EOL><INDENT>samples = numpy.random.normal(loc=params["<STR_LIT>"],<EOL>scale=math.sqrt(params["<STR_LIT>"]),<EOL>size=numSamples)<EOL><DEDENT>elif params["<STR_LIT:name>"] == "<STR_LIT>":<EOL><INDENT>samples = numpy.random.pareto(params["<STR_LIT>"], size=numSamples)<EOL><DEDENT>elif params["<STR_LIT:name>"] == "<STR_LIT>":<EOL><INDENT>samples = numpy.random.beta(a=params["<STR_LIT>"], b=params["<STR_LIT>"],<EOL>size=numSamples)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" + params["<STR_LIT:name>"])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" + str(params))<EOL><DEDENT>if verbosity > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>", params)<EOL>print("<STR_LIT>", numpy.mean(samples),"<STR_LIT>", numpy.var(samples), "<STR_LIT>", math.sqrt(numpy.var(samples)))<EOL><DEDENT>return samples<EOL>

Given the parameters of a distribution, generate numSamples points from it.
This routine is mostly for testing.

:returns: A numpy array of samples.


def _generateSampleData(mean=<NUM_LIT>, variance=<NUM_LIT>, metricMean=<NUM_LIT>,<EOL>metricVariance=<NUM_LIT>):

data = []<EOL>p = {"<STR_LIT>": mean,<EOL>"<STR_LIT:name>": "<STR_LIT>",<EOL>"<STR_LIT>": math.sqrt(variance),<EOL>"<STR_LIT>": variance}<EOL>samples = _sampleDistribution(p, <NUM_LIT>)<EOL>p = {"<STR_LIT>": metricMean,<EOL>"<STR_LIT:name>": "<STR_LIT>",<EOL>"<STR_LIT>": math.sqrt(metricVariance),<EOL>"<STR_LIT>": metricVariance}<EOL>metricValues = _sampleDistribution(p, <NUM_LIT>)<EOL>for hour in range(<NUM_LIT:0>, <NUM_LIT>):<EOL><INDENT>for minute in range(<NUM_LIT:0>, <NUM_LIT>):<EOL><INDENT>data.append(<EOL>[<EOL>datetime.datetime(<NUM_LIT>, <NUM_LIT:2>, <NUM_LIT:2>, hour, minute, <NUM_LIT:0>),<EOL>metricValues[hour * <NUM_LIT> + minute],<EOL>samples[hour * <NUM_LIT> + minute],<EOL>]<EOL>)<EOL><DEDENT><DEDENT>return data<EOL>

Generate 1440 samples of fake metrics data with a particular distribution
of anomaly scores and metric values. Here we generate values every minute.


for c in range(tm.numberOfCols):<EOL><INDENT>assert tm.getNumSegmentsInCell(c, <NUM_LIT:0>) == <NUM_LIT:0><EOL><DEDENT>

Check that cell 0 has no incoming segments


tm.cells4.setVerbosity(verbosity)<EOL>tm.verbosity = verbosity<EOL>tmPy.verbosity = verbosity<EOL>


<EOL>tm = BacktrackingTMCPP(numberOfCols=<NUM_LIT:10>, cellsPerColumn=<NUM_LIT:3>,<EOL>initialPerm=<NUM_LIT>, connectedPerm= <NUM_LIT>,<EOL>minThreshold=<NUM_LIT:2>, newSynapseCount=<NUM_LIT:5>,<EOL>permanenceInc=<NUM_LIT>, permanenceDec= <NUM_LIT>,<EOL>permanenceMax=<NUM_LIT:1>, globalDecay=<NUM_LIT>,<EOL>activationThreshold=<NUM_LIT:4>, doPooling=False,<EOL>segUpdateValidDuration=<NUM_LIT:5>, seed=SEED,<EOL>verbosity=VERBOSITY)<EOL>tm.retrieveLearningStates = True<EOL>tm.makeCells4Ephemeral = False<EOL>pickle.dump(tm, open("<STR_LIT>", "<STR_LIT:wb>"))<EOL>tm2 = pickle.load(open("<STR_LIT>"))<EOL>self.assertTrue(fdrutils.tmDiff2(tm, tm2, VERBOSITY, checkStates=False))<EOL>for i in range(<NUM_LIT:5>):<EOL><INDENT>x = numpy.zeros(tm.numberOfCols, dtype='<STR_LIT>')<EOL>_RGEN.initializeUInt32Array(x, <NUM_LIT:2>)<EOL>tm.learn(x)<EOL><DEDENT>tm.reset()<EOL>tm.makeCells4Ephemeral = False<EOL>pickle.dump(tm, open("<STR_LIT>", "<STR_LIT:wb>"))<EOL>tm2 = pickle.load(open("<STR_LIT>"))<EOL>self.assertTrue(fdrutils.tmDiff2(tm, tm2, VERBOSITY))<EOL>patterns = numpy.zeros((<NUM_LIT:4>, tm.numberOfCols), dtype='<STR_LIT>')<EOL>for i in range(<NUM_LIT:4>):<EOL><INDENT>_RGEN.initializeUInt32Array(patterns[i], <NUM_LIT:2>)<EOL><DEDENT>for i in range(<NUM_LIT:10>):<EOL><INDENT>x = numpy.zeros(tm.numberOfCols, dtype='<STR_LIT>')<EOL>_RGEN.initializeUInt32Array(x, <NUM_LIT:2>)<EOL>tm.infer(x)<EOL>if i > <NUM_LIT:0>:<EOL><INDENT>tm._checkPrediction(patterns)<EOL><DEDENT><DEDENT>

Basic test (creation, pickling, basic run of learning and inference)


network = Network()<EOL>network.addRegion('<STR_LIT>', '<STR_LIT>', '<STR_LIT:{}>')<EOL>network.addRegion('<STR_LIT>', '<STR_LIT>', '<STR_LIT:{}>')<EOL>_createSensorToClassifierLinks(network, '<STR_LIT>', '<STR_LIT>')<EOL>sensorRegion = network.regions['<STR_LIT>'].getSelf()<EOL>encoderParams = {'<STR_LIT>': {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>'<STR_LIT>': <NUM_LIT:1>,<EOL>'<STR_LIT:name>': '<STR_LIT>',<EOL>'<STR_LIT:type>': '<STR_LIT>'}}<EOL>encoder = MultiEncoder()<EOL>encoder.addMultipleEncoders(encoderParams)<EOL>sensorRegion.encoder = encoder<EOL>testDir = os.path.dirname(os.path.abspath(__file__))<EOL>inputFile = os.path.join(testDir, '<STR_LIT>', '<STR_LIT>')<EOL>dataSource = FileRecordStream(streamID=inputFile)<EOL>sensorRegion.dataSource = dataSource<EOL>network.regions['<STR_LIT>'].setParameter('<STR_LIT>', '<STR_LIT>')<EOL>return network<EOL>

Create a network with a RecordSensor region and a SDRClassifier region


network.link(sensorRegionName, classifierRegionName, '<STR_LIT>', '<STR_LIT>',<EOL>srcOutput='<STR_LIT>', destInput='<STR_LIT>')<EOL>network.link(sensorRegionName, classifierRegionName, '<STR_LIT>', '<STR_LIT>',<EOL>srcOutput='<STR_LIT>', destInput='<STR_LIT>')<EOL>network.link(sensorRegionName, classifierRegionName, '<STR_LIT>', '<STR_LIT>',<EOL>srcOutput='<STR_LIT>', destInput='<STR_LIT>')<EOL>network.link(sensorRegionName, classifierRegionName, '<STR_LIT>', '<STR_LIT>',<EOL>srcOutput='<STR_LIT>', destInput='<STR_LIT>')<EOL>

Create links from sensor region to classifier region.


network = Network()<EOL>network.addRegion('<STR_LIT>', '<STR_LIT>', '<STR_LIT:{}>')<EOL>sensorRegion = network.regions['<STR_LIT>'].getSelf()<EOL>encoderParams = {'<STR_LIT>': {'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>'<STR_LIT>': <NUM_LIT:1>,<EOL>'<STR_LIT:name>': '<STR_LIT>',<EOL>'<STR_LIT:type>': '<STR_LIT>'}}<EOL>encoder = MultiEncoder()<EOL>encoder.addMultipleEncoders(encoderParams)<EOL>sensorRegion.encoder = encoder<EOL>testDir = os.path.dirname(os.path.abspath(__file__))<EOL>inputFile = os.path.join(testDir, '<STR_LIT>', '<STR_LIT>')<EOL>dataSource = FileRecordStream(streamID=inputFile)<EOL>sensorRegion.dataSource = dataSource<EOL>network.regions['<STR_LIT>'].setParameter('<STR_LIT>', '<STR_LIT>')<EOL>return network<EOL>

Create network with one RecordSensor region.


self.files = {}<EOL>with tempfile.NamedTemporaryFile(<EOL>prefix='<STR_LIT>', delete=False) as outp:<EOL><INDENT>self.addCleanup(os.remove, outp.name)<EOL>with open(resource_filename(__name__, '<STR_LIT>')) as inp:<EOL><INDENT>outp.write(inp.read())<EOL><DEDENT>self.files['<STR_LIT>'] = outp.name<EOL><DEDENT>with tempfile.NamedTemporaryFile(<EOL>prefix='<STR_LIT>', delete=False) as outp:<EOL><INDENT>self.addCleanup(os.remove, outp.name)<EOL>with open(resource_filename(__name__, '<STR_LIT>')) as inp:<EOL><INDENT>outp.write(inp.read())<EOL><DEDENT>self.files['<STR_LIT>'] = outp.name<EOL><DEDENT>

configuration.Configuration relies on static methods
        which load files by name.  Since we need to be able to run tests and
        potentially change the content of those files between tests without
        interfering with one another and with the system configuration, this
        setUp() function will allocate temporary files used only during the using
        conf/nupic-default.xml and conf/nupic-site.xml (relative to the unit tests)
        as templates.


def mockSleepTime(self, mockTime, mockSleep):

class _TimeContainer(object):<EOL><INDENT>accumulatedTime = <NUM_LIT:0><EOL><DEDENT>def testTime():<EOL><INDENT>return _TimeContainer.accumulatedTime<EOL><DEDENT>def testSleep(duration):<EOL><INDENT>_TimeContainer.accumulatedTime += duration<EOL><DEDENT>mockTime.side_effect = testTime<EOL>mockSleep.side_effect = testSleep<EOL>

Configures mocks for time.time and time.sleep such that every call
        to time.sleep(x) increments the return value of time.time() by x.

        mockTime:     time.time mock
        mockSleep:    time.sleep mock


handle = tempfile.NamedTemporaryFile(prefix='<STR_LIT:test>', suffix='<STR_LIT>', dir='<STR_LIT:.>')<EOL>filename = handle.name<EOL>handle.close()<EOL>return filename<EOL>

Creates unique file name that starts with 'test' and ends with '.txt'.


self.interpreter = SafeInterpreter(writer=io.BytesIO())<EOL>

Set up an interpreter directing output to a BytesIO stream.


sys.path.insert(<NUM_LIT:0>, os.path.join(examplesDir, exampleName))<EOL>modName = "<STR_LIT>" % exampleName<EOL>mod = __import__(modName, fromlist=["<STR_LIT>"])<EOL>return getattr(mod, "<STR_LIT>")<EOL>

Get predictions generator for one of the quick-start example.

.. note::

  The examples are not part of the nupic package so we need to manually
  append the example module path to syspath.

:param examplesDir:
  (str) path to the example parent directory.
:param exampleName:
  (str) name of the example. E.g: "opf", "network", "algo".
:return predictionsGenerator:
  (function) predictions generator functions.


for example in cls.examples:<EOL><INDENT>predictionGenerator = _getPredictionsGenerator(cls.examplesDir, example)<EOL>for prediction in predictionGenerator(MAX_PREDICTIONS):<EOL><INDENT>cls.oneStepPredictions[example].append(prediction[<NUM_LIT:0>])<EOL>cls.oneStepConfidences[example].append(prediction[<NUM_LIT:1>])<EOL>cls.fiveStepPredictions[example].append(prediction[<NUM_LIT:2>])<EOL>cls.fiveStepConfidences[example].append(prediction[<NUM_LIT:3>])<EOL><DEDENT><DEDENT>

Get the predictions and prediction confidences for all examples.


consumption_encoder = ScalarEncoder(<NUM_LIT>, <NUM_LIT:0.0>, <NUM_LIT>, n=<NUM_LIT:50>, name="<STR_LIT>",<EOL>clipInput=True)<EOL>time_encoder = DateEncoder(timeOfDay=(<NUM_LIT>, <NUM_LIT>), name="<STR_LIT>")<EOL>encoder = MultiEncoder()<EOL>encoder.addEncoder("<STR_LIT>", consumption_encoder)<EOL>encoder.addEncoder("<STR_LIT>", time_encoder)<EOL>return encoder<EOL>

Create the encoder instance for our test and return it.


def createNetwork(dataSource, enableTP=False, temporalImp="<STR_LIT>"):

network = Network()<EOL>network.addRegion("<STR_LIT>", "<STR_LIT>",<EOL>json.dumps({"<STR_LIT>": _VERBOSITY}))<EOL>sensor = network.regions["<STR_LIT>"].getSelf()<EOL>sensor.encoder = createEncoder()<EOL>sensor.dataSource = dataSource<EOL>SP_PARAMS["<STR_LIT>"] = sensor.encoder.getWidth()<EOL>network.addRegion("<STR_LIT>", "<STR_LIT>", json.dumps(SP_PARAMS))<EOL>network.link("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>")<EOL>network.link("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>",<EOL>srcOutput="<STR_LIT>", destInput="<STR_LIT>")<EOL>network.link("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>",<EOL>srcOutput="<STR_LIT>", destInput="<STR_LIT>")<EOL>network.link("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>",<EOL>srcOutput="<STR_LIT>", destInput="<STR_LIT>")<EOL>if enableTP:<EOL><INDENT>TM_PARAMS["<STR_LIT>"] = temporalImp<EOL>network.addRegion("<STR_LIT>", "<STR_LIT>",<EOL>json.dumps(TM_PARAMS))<EOL>network.link("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>")<EOL>network.link("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>",<EOL>srcOutput="<STR_LIT>", destInput="<STR_LIT>")<EOL><DEDENT>spatialPoolerRegion = network.regions["<STR_LIT>"]<EOL>spatialPoolerRegion.setParameter("<STR_LIT>", True)<EOL>spatialPoolerRegion.setParameter("<STR_LIT>", False)<EOL>if enableTP:<EOL><INDENT>temporalPoolerRegion = network.regions["<STR_LIT>"]<EOL>temporalPoolerRegion.setParameter("<STR_LIT>", True)<EOL>temporalPoolerRegion.setParameter("<STR_LIT>", True)<EOL>temporalPoolerRegion.setParameter("<STR_LIT>", True)<EOL>temporalPoolerRegion.setParameter("<STR_LIT>", True)<EOL><DEDENT>return network<EOL>

Create the Network instance.

    The network has a sensor region reading data from `dataSource` and passing
    the encoded representation to an SPRegion. The SPRegion output is passed to
    a TMRegion.

    :param dataSource: a RecordStream instance to get data from
    :returns: a Network instance ready to run


def generatePattern(numCols = <NUM_LIT:100>,<EOL>minOnes =<NUM_LIT>,<EOL>maxOnes =<NUM_LIT>,<EOL>colSet = [],<EOL>prevPattern =numpy.array([])):

assert minOnes < maxOnes<EOL>assert maxOnes < numCols<EOL>nOnes = rgen.randint(minOnes, maxOnes)<EOL>candidates = list(colSet.difference(set(prevPattern.nonzero()[<NUM_LIT:0>])))<EOL>rgen.shuffle(candidates)<EOL>ind = candidates[:nOnes]<EOL>x = numpy.zeros(numCols, dtype='<STR_LIT>')<EOL>x[ind] = <NUM_LIT:1><EOL>return x<EOL>

Generate a single test pattern with given parameters.

    Parameters:
    --------------------------------------------
    numCols:                Number of columns in each pattern.
    minOnes:                The minimum number of 1's in each pattern.
    maxOnes:                The maximum number of 1's in each pattern.
    colSet:                 The set of column indices for the pattern.
    prevPattern:            Pattern to avoid (null intersection).


def buildTrainingSet(numSequences = <NUM_LIT:2>,<EOL>sequenceLength = <NUM_LIT:100>,<EOL>pctShared = <NUM_LIT>,<EOL>seqGenMode = '<STR_LIT>',<EOL>subsequenceStartPos = <NUM_LIT:10>,<EOL>numCols = <NUM_LIT:100>,<EOL>minOnes=<NUM_LIT>,<EOL>maxOnes = <NUM_LIT>,<EOL>disjointConsecutive =True):

<EOL>colSet = set(range(numCols))<EOL>if '<STR_LIT>' in seqGenMode:<EOL><INDENT>assert '<STR_LIT>' in seqGenMode and '<STR_LIT>' not in seqGenMode<EOL><DEDENT>if '<STR_LIT>' in seqGenMode or numSequences == <NUM_LIT:1>:<EOL><INDENT>pctShared = <NUM_LIT:0.0><EOL><DEDENT>if '<STR_LIT>' not in seqGenMode and '<STR_LIT>' not in seqGenMode:<EOL><INDENT>sharedSequenceLength = int(pctShared*sequenceLength)<EOL><DEDENT>elif '<STR_LIT>' in seqGenMode:<EOL><INDENT>sharedSequenceLength = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>sharedSequenceLength = <NUM_LIT:0><EOL><DEDENT>assert sharedSequenceLength + subsequenceStartPos < sequenceLength<EOL>sharedSequence = []<EOL>for i in range(sharedSequenceLength):<EOL><INDENT>if disjointConsecutive and i > <NUM_LIT:0>:<EOL><INDENT>x = generatePattern(numCols, minOnes, maxOnes, colSet, sharedSequence[i-<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>x = generatePattern(numCols, minOnes, maxOnes, colSet)<EOL><DEDENT>sharedSequence.append(x)<EOL><DEDENT>trainingSequences = []<EOL>if '<STR_LIT>' not in seqGenMode:<EOL><INDENT>trailingLength = sequenceLength - sharedSequenceLength - subsequenceStartPos<EOL><DEDENT>else:<EOL><INDENT>trailingLength = sequenceLength - sharedSequenceLength<EOL><DEDENT>for k,s in enumerate(range(numSequences)):<EOL><INDENT>if len(trainingSequences) > <NUM_LIT:0> and '<STR_LIT>' in seqGenMode:<EOL><INDENT>r = list(range(subsequenceStartPos))+ list(range(subsequenceStartPos + sharedSequenceLength, sequenceLength))<EOL>rgen.shuffle(r)<EOL>r = r[:subsequenceStartPos]+ list(range(subsequenceStartPos, subsequenceStartPos + sharedSequenceLength))+ r[subsequenceStartPos:]<EOL>sequence = [trainingSequences[k-<NUM_LIT:1>][j] for j in r]<EOL><DEDENT>else:<EOL><INDENT>sequence = []<EOL>if '<STR_LIT>' not in seqGenMode:<EOL><INDENT>for i in range(subsequenceStartPos):<EOL><INDENT>if disjointConsecutive and i > <NUM_LIT:0>:<EOL><INDENT>x = generatePattern(numCols, minOnes, maxOnes, colSet, sequence[i-<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>x = generatePattern(numCols, minOnes, maxOnes, colSet)<EOL><DEDENT>sequence.append(x)<EOL><DEDENT><DEDENT>if '<STR_LIT>' in seqGenMode and '<STR_LIT>' not in seqGenMode:<EOL><INDENT>sequence.extend(sharedSequence)<EOL><DEDENT>for i in range(trailingLength):<EOL><INDENT>if disjointConsecutive and i > <NUM_LIT:0>:<EOL><INDENT>x = generatePattern(numCols, minOnes, maxOnes, colSet, sequence[i-<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>x = generatePattern(numCols, minOnes, maxOnes, colSet)<EOL><DEDENT>sequence.append(x)<EOL><DEDENT><DEDENT>assert len(sequence) == sequenceLength<EOL>trainingSequences.append(sequence)<EOL><DEDENT>assert len(trainingSequences) == numSequences<EOL>if VERBOSITY >= <NUM_LIT:2>:<EOL><INDENT>print("<STR_LIT>")<EOL>pprint.pprint(trainingSequences)<EOL><DEDENT>if sharedSequenceLength > <NUM_LIT:0>:<EOL><INDENT>return (trainingSequences, subsequenceStartPos + sharedSequenceLength)<EOL><DEDENT>else:<EOL><INDENT>return (trainingSequences, -<NUM_LIT:1>)<EOL><DEDENT>

Build random high order test sequences.

    Parameters:
    --------------------------------------------
    numSequences:           The number of sequences created.
    sequenceLength:         The length of each sequence.
    pctShared:              The percentage of sequenceLength that is shared across
                            every sequence. If sequenceLength is 100 and pctShared
                            is 0.2, then a subsequence consisting of 20 patterns
                            will be in every sequence. Can also be the keyword
                            'one pattern', in which case a single time step is shared.
    seqGenMode:             What kind of sequence to generate. If contains 'shared'
                            generates shared subsequence. If contains 'no shared',
                            does not generate any shared subsequence. If contains
                            'shuffle', will use common patterns shuffle among the
                            different sequences. If contains 'beginning', will
                            place shared subsequence at the beginning.
    subsequenceStartPos:    The position where the shared subsequence starts
    numCols:                Number of columns in each pattern.
    minOnes:                The minimum number of 1's in each pattern.
    maxOnes:                The maximum number of 1's in each pattern.
    disjointConsecutive:    Whether to generate disjoint consecutive patterns or not.


numCols = numOnes * numPatterns<EOL>p = []<EOL>for i in range(numPatterns):<EOL><INDENT>x = numpy.zeros(numCols, dtype='<STR_LIT>')<EOL>x[i*numOnes:(i+<NUM_LIT:1>)*numOnes] = <NUM_LIT:1><EOL>p.append(x)<EOL><DEDENT>return p<EOL>

Very simple patterns. Each pattern has numOnes consecutive
    bits on. There are numPatterns*numOnes bits in the vector.


numPatterns = <NUM_LIT:11><EOL>p = getSimplePatterns(numOnes, numPatterns)<EOL>s1 = [p[<NUM_LIT:0>], p[<NUM_LIT:1>], p[<NUM_LIT:2>], p[<NUM_LIT:3>], p[<NUM_LIT:4>], p[<NUM_LIT:5>], p[<NUM_LIT:6>] ]<EOL>s2 = [p[<NUM_LIT:7>], p[<NUM_LIT:8>], p[<NUM_LIT:2>], p[<NUM_LIT:3>], p[<NUM_LIT:4>], p[<NUM_LIT:9>], p[<NUM_LIT:10>]]<EOL>trainingSequences = [s1, s2]<EOL>return (trainingSequences, <NUM_LIT:5>)<EOL>

Two very simple high order sequences for debugging. Each pattern in the
    sequence has a series of 1's in a specific set of columns.


numPatterns = <NUM_LIT><EOL>p = getSimplePatterns(numOnes, numPatterns)<EOL>s1 = [p[<NUM_LIT:0>], p[<NUM_LIT:1>], p[<NUM_LIT:0>], p[<NUM_LIT:1>], p[<NUM_LIT:0>], p[<NUM_LIT:2>]]<EOL>s2 = [p[<NUM_LIT:0>], p[<NUM_LIT:1>], p[<NUM_LIT:0>], p[<NUM_LIT:1>], p[<NUM_LIT:3>], p[<NUM_LIT:4>]]<EOL>s3 = [p[<NUM_LIT:0>], p[<NUM_LIT:1>], p[<NUM_LIT:5>], p[<NUM_LIT:6>], p[<NUM_LIT:7>], p[<NUM_LIT:8>]]<EOL>s4 = [p[<NUM_LIT:0>], p[<NUM_LIT:9>], p[<NUM_LIT:10>], p[<NUM_LIT:11>], p[<NUM_LIT:12>], p[<NUM_LIT>]]<EOL>trainingSequences = [s1, s2, s3, s4]<EOL>return (trainingSequences, <NUM_LIT:5>)<EOL>

High order sequences that alternate elements. Pattern i has one's in
    i*numOnes to (i+1)*numOnes.

    The sequences are:
       A B A B A C
       A B A B D E
       A B F G H I
       A J K L M N


numPatterns = <NUM_LIT><EOL>p = getSimplePatterns(numOnes, numPatterns)<EOL>s = []<EOL>s.append(p[rgen.randint(<NUM_LIT:3>,<NUM_LIT>)])<EOL>for i in range(<NUM_LIT:20>):<EOL><INDENT>s.append(p[rgen.randint(<NUM_LIT:3>,<NUM_LIT>)])<EOL>s.append(p[<NUM_LIT:0>])<EOL>s.append(p[<NUM_LIT:1>])<EOL>s.append(p[<NUM_LIT:2>])<EOL>s.append(p[rgen.randint(<NUM_LIT:3>,<NUM_LIT>)])<EOL><DEDENT>return ([s], [[p[<NUM_LIT:0>], p[<NUM_LIT:1>], p[<NUM_LIT:2>]]])<EOL>

Simple sequences for HL0. Each pattern in the sequence has a series of 1's
    in a specific set of columns.
      There are 23 patterns, p0 to p22.
      The sequence we want to learn is p0->p1->p2
      We create a very long sequence consisting of N N p0 p1 p2 N N p0 p1 p2
      N is randomly chosen from p3 to p22


numPatterns = <NUM_LIT><EOL>p = getSimplePatterns(numOnes, numPatterns)<EOL>s = []<EOL>s.append(p[rgen.randint(<NUM_LIT:5>,numPatterns)])<EOL>for i in range(<NUM_LIT:50>):<EOL><INDENT>r = rgen.randint(<NUM_LIT:5>,numPatterns)<EOL>print(r, end='<STR_LIT:U+0020>')<EOL>s.append(p[r])<EOL>if rgen.binomial(<NUM_LIT:1>, <NUM_LIT:0.5>) > <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>s.append(p[<NUM_LIT:0>])<EOL>s.append(p[<NUM_LIT:1>])<EOL>s.append(p[<NUM_LIT:2>])<EOL>s.append(p[<NUM_LIT:4>])<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>s.append(p[<NUM_LIT:1>])<EOL>s.append(p[<NUM_LIT:2>])<EOL>s.append(p[<NUM_LIT:3>])<EOL><DEDENT>r = rgen.randint(<NUM_LIT:5>,numPatterns)<EOL>s.append(p[r])<EOL>print(r, end='<STR_LIT:U+0020>')<EOL><DEDENT>print()<EOL>return ([s], [ [p[<NUM_LIT:0>], p[<NUM_LIT:1>], p[<NUM_LIT:2>], p[<NUM_LIT:4>]],  [p[<NUM_LIT:1>], p[<NUM_LIT:2>], p[<NUM_LIT:3>]] ])<EOL>

Simple sequences for HL0b. Each pattern in the sequence has a series of 1's
    in a specific set of columns.
      There are 23 patterns, p0 to p22.
      The sequences we want to learn are p1->p2->p3 and p0->p1->p2->p4.
      We create a very long sequence consisting of these two sub-sequences
      intermixed with noise, such as:
            N N p0 p1 p2 p4 N N p1 p2 p3 N N p1 p2 p3
      N is randomly chosen from p5 to p22


def findAcceptablePatterns(tm, t, whichSequence, trainingSequences, nAcceptable = <NUM_LIT:1>):

<EOL>upTo = t + <NUM_LIT:2> <EOL>if tm.doPooling:<EOL><INDENT>upTo += min(tm.segUpdateValidDuration, nAcceptable)<EOL><DEDENT>assert upTo <= len(trainingSequences[whichSequence])<EOL>acceptablePatterns = []<EOL>if len(trainingSequences) == <NUM_LIT:2> and(trainingSequences[<NUM_LIT:0>][<NUM_LIT:0>] == trainingSequences[<NUM_LIT:1>][<NUM_LIT:0>]).all():<EOL><INDENT>if (trainingSequences[<NUM_LIT:0>][t] == trainingSequences[<NUM_LIT:1>][t]).all()and (trainingSequences[<NUM_LIT:0>][t+<NUM_LIT:1>] != trainingSequences[<NUM_LIT:1>][t+<NUM_LIT:1>]).any():<EOL><INDENT>acceptablePatterns.append(trainingSequences[<NUM_LIT:0>][t+<NUM_LIT:1>])<EOL>acceptablePatterns.append(trainingSequences[<NUM_LIT:1>][t+<NUM_LIT:1>])<EOL><DEDENT><DEDENT>acceptablePatterns += [trainingSequences[whichSequence][t]for t in range(t,upTo)]<EOL>return acceptablePatterns<EOL>

Tries to infer the set of acceptable patterns for prediction at the given
time step and for the give sequence. Acceptable patterns are: the current one,
plus a certain number of patterns after timeStep, in the sequence that the TM
is currently tracking. Any other pattern is not acceptable.

TODO:
====
- Doesn't work for noise cases.
- Might run in trouble if shared subsequence at the beginning.

Parameters:
==========
tm                       the whole TM, so that we can look at its parameters
t                        the current time step
whichSequence            the sequence we are currently tracking
trainingSequences        all the training sequences
nAcceptable              the number of steps forward from the current timeStep
                         we are willing to consider acceptable. In the case of
                         pooling, it is less than or equal to the min of the
                         number of training reps and the segUpdateValidDuration
                         parameter of the TM, depending on the test case.
                         The default value is 1, because by default, the pattern
                         after the current one should always be predictable.

Return value:
============
acceptablePatterns       A list of acceptable patterns for prediction.


def TestH2a(sequenceLength, nTests, cellsPerColumn, numCols =<NUM_LIT:100>, nSequences =[<NUM_LIT:2>],<EOL>pctShared = <NUM_LIT>, seqGenMode = '<STR_LIT>',<EOL>shouldFail = False):

print("<STR_LIT>")<EOL>nFailed = <NUM_LIT:0><EOL>subsequenceStartPos = <NUM_LIT:10><EOL>assert subsequenceStartPos < sequenceLength<EOL>for numSequences in nSequences:<EOL><INDENT>print("<STR_LIT>",sequenceLength, end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>",cellsPerColumn,"<STR_LIT>",nTests,"<STR_LIT>", numCols)<EOL>print("<STR_LIT>",numSequences, "<STR_LIT>", pctShared, end='<STR_LIT:U+0020>')<EOL>print("<STR_LIT>", seqGenMode)<EOL>for k in range(nTests): <EOL><INDENT>trainingSet = buildTrainingSet(numSequences = numSequences,<EOL>sequenceLength = sequenceLength,<EOL>pctShared = pctShared, seqGenMode = seqGenMode,<EOL>subsequenceStartPos = subsequenceStartPos,<EOL>numCols = numCols,<EOL>minOnes = <NUM_LIT>, maxOnes = <NUM_LIT>)<EOL>print("<STR_LIT>")<EOL>numFailures3, numStrictErrors3, numPerfect3, tm3 =_testSequence(trainingSet,<EOL>nTrainingReps = <NUM_LIT:10>,<EOL>numberOfCols = numCols,<EOL>cellsPerColumn = cellsPerColumn,<EOL>initialPerm = <NUM_LIT>,<EOL>connectedPerm = <NUM_LIT>,<EOL>minThreshold = <NUM_LIT:12>,<EOL>permanenceInc = <NUM_LIT>,<EOL>permanenceDec = <NUM_LIT:0.1>,<EOL>permanenceMax = <NUM_LIT:1>,<EOL>globalDecay = <NUM_LIT>,<EOL>newSynapseCount = <NUM_LIT:15>,<EOL>activationThreshold = <NUM_LIT:12>,<EOL>doPooling = False,<EOL>shouldFail = shouldFail)<EOL>print("<STR_LIT>")<EOL>numFailures, numStrictErrors, numPerfect, tm2 =_testSequence(trainingSet,<EOL>nTrainingReps = <NUM_LIT:2>,<EOL>numberOfCols = numCols,<EOL>cellsPerColumn = cellsPerColumn,<EOL>initialPerm = <NUM_LIT>,<EOL>connectedPerm = <NUM_LIT>,<EOL>minThreshold = <NUM_LIT:12>,<EOL>permanenceInc = <NUM_LIT>,<EOL>permanenceDec = <NUM_LIT:0>,<EOL>permanenceMax = <NUM_LIT:1>,<EOL>globalDecay = <NUM_LIT>,<EOL>newSynapseCount = <NUM_LIT:15>,<EOL>activationThreshold = <NUM_LIT:12>,<EOL>doPooling = False,<EOL>shouldFail = shouldFail)<EOL>print("<STR_LIT>")<EOL>numFailures1, numStrictErrors1, numPerfect1, tm1 =_testSequence(trainingSet,<EOL>nTrainingReps = <NUM_LIT:1>,<EOL>numberOfCols = numCols,<EOL>cellsPerColumn = cellsPerColumn,<EOL>initialPerm = <NUM_LIT>,<EOL>connectedPerm = <NUM_LIT>,<EOL>minThreshold = <NUM_LIT:12>,<EOL>permanenceInc = <NUM_LIT>,<EOL>permanenceDec = <NUM_LIT:0>,<EOL>permanenceMax = <NUM_LIT:1>,<EOL>globalDecay = <NUM_LIT>,<EOL>newSynapseCount = <NUM_LIT:15>,<EOL>activationThreshold = <NUM_LIT:12>,<EOL>doPooling = False,<EOL>shouldFail = shouldFail)<EOL>segmentInfo1 = tm1.getSegmentInfo()<EOL>segmentInfo2 = tm2.getSegmentInfo()<EOL>if (abs(segmentInfo1[<NUM_LIT:0>] - segmentInfo2[<NUM_LIT:0>]) > <NUM_LIT:3>) or(abs(segmentInfo1[<NUM_LIT:1>] - segmentInfo2[<NUM_LIT:1>]) > <NUM_LIT:3>*<NUM_LIT:15>) :<EOL><INDENT>print("<STR_LIT>")<EOL>print(segmentInfo1)<EOL>print(segmentInfo2)<EOL>print(tm3.getSegmentInfo())<EOL>tm3.trimSegments()<EOL>print(tm3.getSegmentInfo())<EOL>print("<STR_LIT>")<EOL>print(numFailures1, numStrictErrors1, numPerfect1)<EOL>print(numFailures, numStrictErrors, numPerfect)<EOL>print(numFailures3, numStrictErrors3, numPerfect3)<EOL>numFailures += <NUM_LIT:1><EOL><DEDENT>if numFailures == <NUM_LIT:0> and not shouldFailor numFailures > <NUM_LIT:0> and shouldFail:<EOL><INDENT>print("<STR_LIT>", end='<STR_LIT:U+0020>')<EOL>if shouldFail:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>print()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL>nFailed = nFailed + <NUM_LIT:1><EOL>print("<STR_LIT>", numFailures)<EOL>print("<STR_LIT>", numStrictErrors)<EOL>print("<STR_LIT>", numPerfect)<EOL><DEDENT><DEDENT><DEDENT>return nFailed<EOL>

Still need to test:
    Two overlapping sequences. OK to get new segments but check that we can
    get correct high order prediction after multiple reps.


cellsPerColumn, numSequences = x[<NUM_LIT:0>], x[<NUM_LIT:1>]<EOL>nTrainingReps = <NUM_LIT:1><EOL>sequenceLength = <NUM_LIT:10><EOL>numCols = <NUM_LIT:200><EOL>print('<STR_LIT>', cellsPerColumn, numSequences)<EOL>seqGenMode = '<STR_LIT>'<EOL>subsequenceStartPos = <NUM_LIT:5><EOL>trainingSet = buildTrainingSet(numSequences = numSequences,<EOL>sequenceLength = sequenceLength,<EOL>pctShared = <NUM_LIT>, seqGenMode = seqGenMode,<EOL>subsequenceStartPos = subsequenceStartPos,<EOL>numCols = numCols,<EOL>minOnes = <NUM_LIT>, maxOnes = <NUM_LIT>)<EOL>numFailures1, numStrictErrors1, numPerfect1, atHub, tm =_testSequence(trainingSet,<EOL>nTrainingReps = nTrainingReps,<EOL>numberOfCols = numCols,<EOL>cellsPerColumn = cellsPerColumn,<EOL>initialPerm = <NUM_LIT>,<EOL>connectedPerm = <NUM_LIT>,<EOL>minThreshold = <NUM_LIT:11>,<EOL>permanenceInc = <NUM_LIT>,<EOL>permanenceDec = <NUM_LIT:0>,<EOL>permanenceMax = <NUM_LIT:1>,<EOL>globalDecay = <NUM_LIT>,<EOL>newSynapseCount = <NUM_LIT:8>,<EOL>activationThreshold = <NUM_LIT:8>,<EOL>doPooling = False,<EOL>shouldFail = False,<EOL>predJustAfterHubOnly = <NUM_LIT:5>)<EOL>seqGenMode = '<STR_LIT>'<EOL>trainingSet = buildTrainingSet(numSequences = numSequences,<EOL>sequenceLength = sequenceLength,<EOL>pctShared = <NUM_LIT:0>, seqGenMode = seqGenMode,<EOL>subsequenceStartPos = <NUM_LIT:0>,<EOL>numCols = numCols,<EOL>minOnes = <NUM_LIT>, maxOnes = <NUM_LIT>)<EOL>numFailures2, numStrictErrors2, numPerfect2, tm =_testSequence(trainingSet,<EOL>nTrainingReps = nTrainingReps,<EOL>numberOfCols = numCols,<EOL>cellsPerColumn = cellsPerColumn,<EOL>initialPerm = <NUM_LIT>,<EOL>connectedPerm = <NUM_LIT>,<EOL>minThreshold = <NUM_LIT:11>,<EOL>permanenceInc = <NUM_LIT>,<EOL>permanenceDec = <NUM_LIT:0>,<EOL>permanenceMax = <NUM_LIT:1>,<EOL>globalDecay = <NUM_LIT>,<EOL>newSynapseCount = <NUM_LIT:8>,<EOL>activationThreshold = <NUM_LIT:8>,<EOL>doPooling = False,<EOL>shouldFail = False)<EOL>print('<STR_LIT>', end='<STR_LIT:U+0020>')<EOL>print(cellsPerColumn, numSequences, numFailures1, numStrictErrors1, numPerfect1, atHub,numFailures2, numStrictErrors2, numPerfect2)<EOL>return cellsPerColumn, numSequences, numFailures1, numStrictErrors1, numPerfect1, atHub,numFailures2, numStrictErrors2, numPerfect2<EOL>

Worker function to use in parallel hub capacity test below.


from multiprocessing import Pool<EOL>import itertools<EOL>print("<STR_LIT>")<EOL>p = Pool(<NUM_LIT:2>)<EOL>results = p.map(worker, itertools.product([<NUM_LIT:1>,<NUM_LIT:2>,<NUM_LIT:3>,<NUM_LIT:4>,<NUM_LIT:5>,<NUM_LIT:6>,<NUM_LIT:7>,<NUM_LIT:8>], range(<NUM_LIT:1>,<NUM_LIT>,<NUM_LIT:200>)))<EOL>f = open('<STR_LIT>', '<STR_LIT:w>')<EOL>for i,r in enumerate(results):<EOL><INDENT>print('<STR_LIT>' % r, file=f)<EOL><DEDENT>f.close()<EOL>

Study hub capacity. Figure out how many sequences can share a pattern
for a given number of cells per column till we the system fails.
DON'T RUN IN BUILD SYSTEM!!! (takes too long)


print('<STR_LIT>'.join('<STR_LIT:1>' if k != <NUM_LIT:0> else '<STR_LIT:.>' for k in x))<EOL>


def getSimplePatterns(numOnes, numPatterns, patternOverlap=<NUM_LIT:0>):

assert (patternOverlap < numOnes)<EOL>numNewBitsInEachPattern = numOnes - patternOverlap<EOL>numCols = numNewBitsInEachPattern * numPatterns + patternOverlap<EOL>p = []<EOL>for i in range(numPatterns):<EOL><INDENT>x = numpy.zeros(numCols, dtype='<STR_LIT>')<EOL>startBit = i*numNewBitsInEachPattern<EOL>nextStartBit = startBit + numOnes<EOL>x[startBit:nextStartBit] = <NUM_LIT:1><EOL>p.append(x)<EOL><DEDENT>return p<EOL>

Very simple patterns. Each pattern has numOnes consecutive
    bits on. The amount of overlap between consecutive patterns is
    configurable, via the patternOverlap parameter.

    Parameters:
    -----------------------------------------------------------------------
    numOnes:        Number of bits ON in each pattern
    numPatterns:    Number of unique patterns to generate
    patternOverlap: Number of bits of overlap between each successive pattern
    retval:         patterns


def buildOverlappedSequences( numSequences = <NUM_LIT:2>,<EOL>seqLen = <NUM_LIT:5>,<EOL>sharedElements = [<NUM_LIT:3>,<NUM_LIT:4>],<EOL>numOnBitsPerPattern = <NUM_LIT:3>,<EOL>patternOverlap = <NUM_LIT:0>,<EOL>seqOverlap = <NUM_LIT:0>,<EOL>**kwargs<EOL>):

<EOL>numSharedElements = len(sharedElements)<EOL>numUniqueElements = seqLen - numSharedElements<EOL>numPatterns = numSharedElements + numUniqueElements * numSequences<EOL>patterns = getSimplePatterns(numOnBitsPerPattern, numPatterns, patternOverlap)<EOL>numCols = len(patterns[<NUM_LIT:0>])<EOL>trainingSequences = []<EOL>uniquePatternIndices = list(range(numSharedElements, numPatterns))<EOL>for i in range(numSequences):<EOL><INDENT>sequence = []<EOL>sharedPatternIndices = list(range(numSharedElements))<EOL>for j in range(seqLen):<EOL><INDENT>if j in sharedElements:<EOL><INDENT>patIdx = sharedPatternIndices.pop(<NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>patIdx = uniquePatternIndices.pop(<NUM_LIT:0>)<EOL><DEDENT>sequence.append(patterns[patIdx])<EOL><DEDENT>trainingSequences.append(sequence)<EOL><DEDENT>if VERBOSITY >= <NUM_LIT:3>:<EOL><INDENT>print("<STR_LIT>")<EOL>printAllTrainingSequences(trainingSequences)<EOL><DEDENT>return (numCols, trainingSequences)<EOL>

Create training sequences that share some elements in the middle.

    Parameters:
    -----------------------------------------------------
    numSequences:         Number of unique training sequences to generate
    seqLen:               Overall length of each sequence
    sharedElements:       Which element indices of each sequence are shared. These
                            will be in the range between 0 and seqLen-1
    numOnBitsPerPattern:  Number of ON bits in each TM input pattern
    patternOverlap:       Max number of bits of overlap between any 2 patterns
    retval:               (numCols, trainingSequences)
                            numCols - width of the patterns
                            trainingSequences - a list of training sequences


def buildSequencePool(numSequences = <NUM_LIT:10>,<EOL>seqLen = [<NUM_LIT:2>,<NUM_LIT:3>,<NUM_LIT:4>],<EOL>numPatterns = <NUM_LIT:5>,<EOL>numOnBitsPerPattern = <NUM_LIT:3>,<EOL>patternOverlap = <NUM_LIT:0>,<EOL>**kwargs<EOL>):

<EOL>patterns = getSimplePatterns(numOnBitsPerPattern, numPatterns, patternOverlap)<EOL>numCols = len(patterns[<NUM_LIT:0>])<EOL>trainingSequences = []<EOL>for i in range(numSequences):<EOL><INDENT>sequence = []<EOL>length = random.choice(seqLen)<EOL>for j in range(length):<EOL><INDENT>patIdx = random.choice(range(numPatterns))<EOL>sequence.append(patterns[patIdx])<EOL><DEDENT>trainingSequences.append(sequence)<EOL><DEDENT>if VERBOSITY >= <NUM_LIT:3>:<EOL><INDENT>print("<STR_LIT>")<EOL>printAllTrainingSequences(trainingSequences)<EOL><DEDENT>return (numCols, trainingSequences)<EOL>

Create a bunch of sequences of various lengths, all built from
    a fixed set of patterns.

    Parameters:
    -----------------------------------------------------
    numSequences:         Number of training sequences to generate
    seqLen:               List of possible sequence lengths
    numPatterns:          How many possible patterns there are to use within
                            sequences
    numOnBitsPerPattern:  Number of ON bits in each TM input pattern
    patternOverlap:       Max number of bits of overlap between any 2 patterns
    retval:               (numCols, trainingSequences)
                            numCols - width of the patterns
                            trainingSequences - a list of training sequences


def createTMs(includeCPP = True,<EOL>includePy = True,<EOL>numCols = <NUM_LIT:100>,<EOL>cellsPerCol = <NUM_LIT:4>,<EOL>activationThreshold = <NUM_LIT:3>,<EOL>minThreshold = <NUM_LIT:3>,<EOL>newSynapseCount = <NUM_LIT:3>,<EOL>initialPerm = <NUM_LIT>,<EOL>permanenceInc = <NUM_LIT:0.1>,<EOL>permanenceDec = <NUM_LIT:0.0>,<EOL>globalDecay = <NUM_LIT:0.0>,<EOL>pamLength = <NUM_LIT:0>,<EOL>checkSynapseConsistency = True,<EOL>maxInfBacktrack = <NUM_LIT:0>,<EOL>maxLrnBacktrack = <NUM_LIT:0>,<EOL>**kwargs<EOL>):

<EOL>connectedPerm = <NUM_LIT:0.5><EOL>tms = dict()<EOL>if includeCPP:<EOL><INDENT>if VERBOSITY >= <NUM_LIT:2>:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>cpp_tm = BacktrackingTMCPP(numberOfCols = numCols, cellsPerColumn = cellsPerCol,<EOL>initialPerm = initialPerm, connectedPerm = connectedPerm,<EOL>minThreshold = minThreshold, newSynapseCount = newSynapseCount,<EOL>permanenceInc = permanenceInc, permanenceDec = permanenceDec,<EOL>activationThreshold = activationThreshold,<EOL>globalDecay = globalDecay, burnIn = <NUM_LIT:1>,<EOL>seed=SEED, verbosity=VERBOSITY,<EOL>checkSynapseConsistency = checkSynapseConsistency,<EOL>collectStats = True,<EOL>pamLength = pamLength,<EOL>maxInfBacktrack = maxInfBacktrack,<EOL>maxLrnBacktrack = maxLrnBacktrack,<EOL>)<EOL>cpp_tm.retrieveLearningStates = True<EOL>tms['<STR_LIT>'] = cpp_tm<EOL><DEDENT>if includePy:<EOL><INDENT>if VERBOSITY >= <NUM_LIT:2>:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>py_tm = BacktrackingTM(numberOfCols = numCols, cellsPerColumn = cellsPerCol,<EOL>initialPerm = initialPerm, connectedPerm = connectedPerm,<EOL>minThreshold = minThreshold, newSynapseCount = newSynapseCount,<EOL>permanenceInc = permanenceInc, permanenceDec = permanenceDec,<EOL>activationThreshold = activationThreshold,<EOL>globalDecay = globalDecay, burnIn = <NUM_LIT:1>,<EOL>seed=SEED, verbosity=VERBOSITY,<EOL>collectStats = True,<EOL>pamLength = pamLength,<EOL>maxInfBacktrack = maxInfBacktrack,<EOL>maxLrnBacktrack = maxLrnBacktrack,<EOL>)<EOL>tms['<STR_LIT>'] = py_tm<EOL><DEDENT>return tms<EOL>

Create one or more TM instances, placing each into a dict keyed by
    name.

    Parameters:
    ------------------------------------------------------------------
    retval:   tms - dict of TM instances