Save variables history in workspace and display/scope

950 views
Skip to first unread message

Andrea Amorosi

unread,
Jun 7, 2011, 4:59:33 PM6/7/11
to spyder
Hi to all,
I'm a matlab/simulink user and I'm searching to use python with Spyder
as an alternative to create dynamic models.
I would like to know how to save variables history (data at each
execution step) in variable workspace. Should I define the variable
global? Moreover I would like to know if it is possible to connect a
variable in the model to a graphical display or scope simular to the
ones available in simulink.
Thank you,
Andrea

David Verelst

unread,
Jun 8, 2011, 9:06:41 AM6/8/11
to spyd...@googlegroups.com
Hi,

First of all, I am not a Simulink user so I do not know exactly what kind of functionality you are referring to. As far as I know, however, there is no real open source alternative to the power of Simulink. You will not be able to use spyder as you use Simulink now. It will require a quite different working approach.

You can save any variable at any moment in your script. For instance, you can use pickle (python build in function), numpy.save or pytables (depending on what data you need to save) to save whatever variables you want to the hard disk. This will not give you right away a variable history, but if you tweak around your saving algorithm I guess it could be made in such a way. Note that I am not an expert, maybe this is just a silly suggestion for your application.

I have no experience with graphical programming with python, but I think it shouldn't be too hard to create some graphical application with Qt which simply prints out some variables. There are plenty of tutorials around which will point you in the right direction. Some random picks from google:
http://www.wellho.net/mouth/2746_Model-View-Controller-demo-Sqlite-Python-3-Qt4.html
http://sector.ynet.sk/qt4-tutorial/my-first-qt-gui-application.html

Hope this helps,

David


--
You received this message because you are subscribed to the Google Groups "spyder" group.
To post to this group, send email to spyd...@googlegroups.com.
To unsubscribe from this group, send email to spyderlib+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spyderlib?hl=en.


Andrea.A...@gmail.com

unread,
Jun 8, 2011, 5:12:07 PM6/8/11
to spyd...@googlegroups.com
Hi,
first of all, thank you for your help!
I agree with you that there is no valid simulink open source alternative
(at least at the moment), so I will change my working approach since for
me there is no difference between programming in a visual language like
Simulink or in an imperative one like Python (I've written only some
little program, but I want to learn it better).
Since Spyder has a gui and functionalities similar to matlab, I've
chosen it as IDE and the most important (for what I want to do)
functionalities of matlab/simulink which I've not found in spyder are:
1) the possibility to read/write data in the workspace from a script (in
matlab files it is sufficient to define the variables as global)
2) the possibility to look at variables value at runtime using graphs or
numerical displays (this is a Simulink, not matlab, specific functionality).
Can anybody give me more hints or idea on how to eventually implement them.
Thank you,
Andrea

Just for reference this is the Matworks help page describing the display
block in Simulink:
http://www.mathworks.com/help/toolbox/simulink/slref/display.html

and this is the page describing the scope block which draw a graph of
the variable at runtime:
http://www.mathworks.com/help/toolbox/simulink/slref/scope.html

Il 08/06/2011 15:06, David Verelst ha scritto:
> Hi,
>
> First of all, I am not a Simulink user so I do not know exactly what
> kind of functionality you are referring to. As far as I know, however,
> there is no real open source alternative to the power of Simulink. You
> will not be able to use spyder as you use Simulink now. It will require
> a quite different working approach.
>
> You can save any variable at any moment in your script. For instance,
> you can use pickle (python build in function), numpy.save or pytables
> (depending on what data you need to save) to save whatever variables you
> want to the hard disk. This will not give you right away a variable
> history, but if you tweak around your saving algorithm I guess it could
> be made in such a way. Note that I am not an expert, maybe this is just
> a silly suggestion for your application.
>
> I have no experience with graphical programming with python, but I think
> it shouldn't be too hard to create some graphical application with Qt
> which simply prints out some variables. There are plenty of tutorials
> around which will point you in the right direction. Some random picks
> from google:
> http://www.wellho.net/mouth/2746_Model-View-Controller-demo-Sqlite-Python-3-Qt4.html
> http://sector.ynet.sk/qt4-tutorial/my-first-qt-gui-application.html
>
> Hope this helps,
>
> David
>
> On 7 June 2011 22:59, Andrea Amorosi <andrea.a...@gmail.com

> <mailto:andrea.a...@gmail.com>> wrote:
>
> Hi to all,
> I'm a matlab/simulink user and I'm searching to use python with Spyder
> as an alternative to create dynamic models.
> I would like to know how to save variables history (data at each
> execution step) in variable workspace. Should I define the variable
> global? Moreover I would like to know if it is possible to connect a
> variable in the model to a graphical display or scope simular to the
> ones available in simulink.
> Thank you,
> Andrea
>
> --
> You received this message because you are subscribed to the Google
> Groups "spyder" group.
> To post to this group, send email to spyd...@googlegroups.com

> <mailto:spyd...@googlegroups.com>.


> To unsubscribe from this group, send email to
> spyderlib+...@googlegroups.com

> <mailto:spyderlib%2Bunsu...@googlegroups.com>.

David Verelst

unread,
Jun 10, 2011, 4:40:17 AM6/10/11
to spyd...@googlegroups.com
Hi,

Coming back to your questions:

1) the possibility to read/write data in the workspace from a script

The scope of variables in Python depends on the context. A script can
have global variables (accessible for al including classes and
functions) but they will not be available by default inside modules
you load from elsewhere, unless you pass the variables on of course.
Note that you can always pass on variables to whatever function or
class you load, whether they are living inside your script or not.
There are many good books available about how to deal with python in a
scientific context and which will also explain how to deal with the
scope of and life time of your variables. I strongly recommend to take
some time to go through them. On a personal note I can tell you that I
would have saved myself a lot of misery if only I would have invested
the time in reading a good book (or taking a course for that matter)
about Python!

Some course suggestions:
* Python Scientific Lecture Notes
http://scipy-lectures.github.com/
* 6.189 A Gentle Introduction to Programming Using Python
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2008/
A book suggestion:
* Python Scripting for Computational Science (3th edition 2008)
http://www.springerlink.com/content/978-3-540-73915-9
http://www.springer.com/mathematics/numerical+and+computational+mathematics/book/978-3-540-73915-9

2) I don't have any experience with visualising variables during
runtime other than including print statements now and then to see if
all is going well. I suppose there are more intelligent ways of doing
that, but I personally never pursued, interesting as it is, the idea
myself. A short googling on "visualize data runtime python" and
derivatives learns me there is no dead simple answer available. I
would also like to refer you to the topical software list on scipy:
http://www.scipy.org/Topical_Software, which gives an (incomplete) but
extensive overview of python modules relevant in the scientific
context. Than there is also the Python Package Index:
http://pypi.python.org/pypi for an even more extensive and general
list.

So, in conclusion, I suggest you to take some time to dive into python
and, to my knowledge, problem nr 2 is not straight forward to solve
(back to suggestion 1).

Regards,
David

On 8 June 2011 23:12, Andrea.A...@gmail.com

Uwe Fechner

unread,
Jun 10, 2011, 4:41:10 AM6/10/11
to spyd...@googlegroups.com
On 08.06.2011 23:12, Andrea.A...@gmail.com wrote:
> Hi,
> first of all, thank you for your help!
> I agree with you that there is no valid simulink open source alternative
> (at least at the moment),
Did you have a look at XCOS :
http://www.scilab.org/products/xcos

It improved quite a lot in the last year. It has not as many libraries
as Simulink, but you
can do a lot of modeling with the included modelica block.

Best regards:

Uwe Fechner

Andrea.A...@gmail.com

unread,
Jun 10, 2011, 6:53:25 PM6/10/11
to spyd...@googlegroups.com, Uwe Fechner
I've tried XCOS, but it is not very clear since it doesn't have the
possibility to name inport and outports like in simulink.
This fact trasforms complex models in something not easy to understand.
So I prefer to change develop pardigm.
Andrea

Andrea.A...@gmail.com

unread,
Jun 10, 2011, 7:06:01 PM6/10/11
to spyd...@googlegroups.com
You are right.
I will study python more.
Meanwhile I've updated spyder to the latest release and now at least
I've the possibility to use the plotting commands with the graph which
appears automatically (as I previously saw on the poject documentation
page). Maybe I can simply refresh it with the updated data at each time
step (I suppose that such an approach will be to much heavy).
Andrea
Reply all
Reply to author
Forward
0 new messages