riccardoscalco / pykov Goto Github PK
View Code? Open in Web Editor NEWPykov is a Python module on finite regular Markov chains.
License: GNU General Public License v3.0
Pykov is a Python module on finite regular Markov chains.
License: GNU General Public License v3.0
File "/Library/Python/2.7/site-packages/pykov.py", line 1087, in walk
result.append(self.move(result[-1]))
File "/Library/Python/2.7/site-packages/pykov.py", line 925, in move
return self.succ(state).choose(random_func)
File "/Library/Python/2.7/site-packages/pykov.py", line 314, in choose
return state
UnboundLocalError: local variable 'state' referenced before assignment
The code has the method:
T.adiacence()
But the documentation has still the method:
T.adjacency()
Now there is an ImportError
when importing pykov in Python 3.
When running Chain.communication_classes()
, a TypeError is thrown: TypeError: unhashable type: 'set'
.
The issue seems to be having nested sets, since the interior sets are mutable. One fix would be to use frozenset()
for the nested sets. Alternatively, the return could be changed to a list of sets, nested lists, or something similar.
Exact error:
In [1]: run pykov.py
In [2]: Chain({(0, 1): 1, (1, 0): 1}).communication_classes()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-cc5c171ee32c> in <module>()
----> 1 Chain({(0, 1): 1, (1, 0): 1}).communication_classes()
/home/rwb/python/Pykov/pykov.py in communication_classes(self)
1347 if row[0, el] == 1:
1348 comm_class.add(pos2el[el])
-> 1349 res.add(comm_class)
1350 return res
1351
TypeError: unhashable type: 'set'
Calling Chain.walk()
with the parameter start=0
chooses a random start state from the chain.
MWE:
chain = pykov.Chain({(0, 1): 1, (1, 0): 1})
chain.walk(0, 0) # Repeat a few times to get a [1].
The relevant line is here:
if not start:
start = self.steady().choose()
This could be fixed by checking for start == None
and stop == None
, I think. If 0 is excluded by design, can a note be added to the documentation?
Hi there. I'm pretty impressed by this library and tried to integrate it to my research code. However I got different results from what I already had and checked the same chains in mathematica to compare results. Results from mathematica agree with mine and disagree with pykov's.
I've seen the code for 'steady' and it didn't look clear at all to me and I don't have access to the books you mentioned.
This difference in result is probably bug in the library implementation. It would be great if there was a test suite added to library with typical and edge cases for different operation on Markov Chains.
I tried the example of the tutorial:
T = pykov.Chain({('A','B'): .3, ('A','A'): .7, ('B','A'): 1.})
T.steady()
here below the error:
In [9]: T.steady()
TypeError Traceback (most recent call last)
/home/hakim/myexperiments/tastp/ in ()
----> 1 T.steady()
/usr/local/lib/python2.7/dist-packages/pykov.pyc in steady(self)
938 m = len(e2p)
939 P = self.dok(e2p).tocsr()
--> 940 Q = ss.eye(m, format='csr') - P
941 e = numpy.zeros(m)
942 e[-1] = 1.
TypeError: eye() takes at least 2 arguments (2 given)
import pykov
import math
init_vector, matrix = pykov.maximum_likelihood_probabilities('99a')
math.exp(matrix.walk_probability('99'))
Out[26]: 0.5
math.exp(matrix.walk_probability('ee'))
Out[27]: 0.0
math.exp(matrix.walk_probability('aa'))
Out[28]: 1.0
matrix
Out[30]: {('9', '9'): 0.5, ('9', 'a'): 0.5, ('a', 'a'): 1.0}
Do you know why you get this extra transition a->a=1 ?
Git commit:
9dc9181
I've seen this here and there in my Organ Donor code.
2_track_contents.txt will become a markov object and a new music object
The Kemeny constant of the track contents transition table is 36.8458389735
A phrase of kemeny length from this transition table is: ['rest', '76', 'rest', '86', 'rest', '52', '76', 'rest', '86', 'rest', '88', 'rest', '52', 'rest', '59', '52', 'rest', '79', 'rest', '81', 'rest', '79', 'rest', '92', 'rest', '59', '61', 'rest', '76', 'rest', '67', 'rest', '57', 'rest', '81', 'rest', '52']
2_track_notes.txt will become a markov object and a new music object
2_rest_durations.txt will become a markov object and a new music object
if there aren't any rests to give a transition table, don't try to write one.
2_note_durations.txt will become a markov object and a new music object
/Library/Python/2.7/site-packages/scipy-0.14.0.dev_7cefb25-py2.7-macosx-10.9-intel.egg/scipy/sparse/compressed.py:686: SparseEfficiencyWarning: Changing the sparsity structure of a csc_matrix is expensive. lil_matrix is more efficient.
SparseEfficiencyWarning)
The Kemeny constant of the note durations transition table is
Traceback (most recent call last):
File "organelle.py", line 1445, in
composer()
File "organelle.py", line 1126, in composer
print "The Kemeny constant of the note durations transition table is", R.kemeny_constant()
File "/Library/Python/2.7/site-packages/pykov.py", line 1402, in kemeny_constant
Z = self.fundamental_matrix()
File "/Library/Python/2.7/site-packages/pykov.py", line 1379, in fundamental_matrix
p = self.steady()._toarray(el2pos)
File "/Library/Python/2.7/site-packages/pykov.py", line 1093, in steady
res.normalize()
File "/Library/Python/2.7/site-packages/pykov.py", line 269, in normalize
for k in self.iterkeys():
RuntimeError: dictionary changed size during iteration
What should I check first?
[x] Bug (Typo)
substracted
, however expect to see subtracted
.precedessors
, however expect to see predecessors
.coloum
, however expect to see column
.Semi-automated issue generated by
https://github.com/timgates42/meticulous/blob/master/docs/NOTE.md
To avoid wasting CI processing resources a branch with the fix has been
prepared but a pull request has not yet been created. A pull request fixing
the issue can be prepared from the link below, feel free to create it or
request @timgates42 create the PR. Alternatively if the fix is undesired please
close the issue with a small comment about the reasoning.
https://github.com/timgates42/Pykov/pull/new/bugfix_typos
Thanks.
Traceback (most recent call last):
File "organelle.py", line 1325, in
composer()
File "organelle.py", line 1044, in composer
phrase_lengths.append(P.kemeny_constant())
File "/usr/local/lib/python2.7/dist-packages/pykov.py", line 1270, in kemeny_constant
Z = self.fundamental_matrix()
File "/usr/local/lib/python2.7/dist-packages/pykov.py", line 1246, in fundamental_matrix
p = self.steady()._toarray(el2pos)
File "/usr/local/lib/python2.7/dist-packages/pykov.py", line 971, in steady
Q = ss.eye(m, format='csr') - P
TypeError: eye() takes at least 2 arguments (2 given)
What should I check first?
Hi,
First, thanks and ๐ for this small and cool project.
It's neat and well written, I love it!
Second, may I suggest that you write a Sphinx documentation and host it on ReadTheDocs please?
Based on the Markdown documentation that is already available in the README.md file, it could be very easy.
GitHub can be connected to ReadTheDocs to build the site automatically, for instance http://pykov.readthedocs.org/
Thanks in advance; I can't do it for you, but if you need any help, I can assist (I did a few docs on ReadTheDocs already, see this example or this one).
I installed "pykov" using pip without error.
Here below the outcome when i tried to import "pykov" in iteractive python:
Any idea? (thanks)
Python 2.7.3 (default, Feb 27 2014, 20:00:17)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import pykov
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/pykov.py", line 34, in
import six
ImportError: No module named six
Hey,
first of all this is my first time using github and also i'm very new to python so be gentle with me..
I have a pykov.Chain where one of the items is None and when i'm trying to use the function chain.move(None) i get the following error:
AttributeError: 'OrderedDict' object has no attribute 'choose'
to fix it i did the following changes in the succ function (i only use positive numbers in my project):
from:
def succ(self, key=None):
try:
if key is not None:
return self._succ[key]
else:
return self._succ
except AttributeError:
self._succ = OrderedDict([(state, Vector()) for state in self.states()])
for link, probability in six.iteritems(self):
self._succ[link[0]][link[1]] = probability
if key is not None:
return self._succ[key]
else:
return self._succ
to:
def succ(self, key=-1):
try:
if key is not -1:
return self._succ[key]
else:
return self._succ
except AttributeError:
self._succ = OrderedDict([(state, Vector()) for state in self.states()])
for link, probability in six.iteritems(self):
self._succ[link[0]][link[1]] = probability
if key is not -1:
return self._succ[key]
else:
return self._succ
My solution works for me, but is there a better way?
Just wondering, why not upload to pypi? It's really simple and makes installing as a dependency slightly easier.
According to the following matrix implemented in a file fn and loaded using readmat function, the steady function randomly return a list of 0. I use python in version 3.7.
R R .5
R N .25
R S .25
N R .5
N N 0
N S .5
S R .25
S N .25
S S .5
P = pykov.readmat(fn)
Pi = P.steady()
assert(Pi.sum()!=0)
Thanks in advance.
When you add a empty string as a key to the chain ex:
P[('','Bb')]=.3
and then call
P.move('')
pykov will thrown an exception:
Traceback (most recent call last):
File "pykoy_tests.py", line 102, in <module>
print P.move('')
File "pykov.py", line 907, in move
return self.succ(state).choose()
AttributeError: 'dict' object has no attribute 'choose'
This is cause by the if statements in def succ() such as: https://github.com/riccardoscalco/Pykov/blob/master/pykov.py#L643
Where it should be comparing for against None.
The use case I found this in is generating chains from English sentences where a empty string can indicate the beginning or end of chain or 'sentence'. Now I could be wrong in my usage but I thought I would bring it to your attention.
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.