Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Aug 4, 2015

a simple mechanical angular rate sensor, fulfilled with python simulation

測試一個簡單的單軸角速度計,one-axis angular rate sensor。 此系統是一個質量接上一個彈簧,放在一個轉動速度待測的管子裡。 因為離心力的關係,管子轉動的時候,質量點會往外移動,彈簧會被拉伸。只要解出轉動速度與拉伸距離的關係,我們就可以從拉伸距離反求當下管子的轉動速度。而管子可以是任何物體。

管子從零開始加速轉動,加速轉動到約70度時變成等速轉動。以下是模擬加上有damping的情況。
 
mechanical_gyro from KT Gump on Vimeo.

Jul 14, 2015

opengl的座標空間設定,python mplot3d的座標空間設定

這邊畫出python的3d繪圖套件mplot3d的三維xyz座標空間與opengl的三維xyz座標空間,好讓我以及其他人在學習python與opengl的時候好做轉換的對應及比較,因為常常相同物體的座標位置在兩個座標上的顯示是不一樣的,而這若沒有搞懂此兩座標空間的轉換會白白浪費很多時間在轉來轉去。

預設的python mplot3d的xyz座標是下面這樣擺設的(對觀察者而言)

Dec 23, 2014

在win32 xp上嘗試跑python及opengl

想要用python使用opengl

---
成功紀錄
按照這文章指示http://noobtuts.com/python/opengl-introduction
終於成功在win32 xp上用pyopengl + glut完成了文章裡的簡易demo
noobtuts網站真的厲害
我搞了好久
他用非常耐心清楚的方式
讓我一試就成功
不容易不容易

---
不成功紀錄1
嘗試在conda environment安裝openglcontext
系統環境是windows xp 32位元
conda install pyopengl pyopengl_accelerate "pyvrml97==2.3.0a4" simpleparse numpy "openglContext==2.2.0a3" pydispatcher pillow

但是裝不了openglcontext
因為conda.org上面沒有32位元windows版本
也裝不了pydispatcher
不過可以用pip install pydispatcher裝
但不曉得若沒有跟conda install一起裝會不會漏掉一些linker連結

最終是安裝失敗
linker說少了glut32.dll

真麻煩
不知有沒有強者能跟我說怎麼辦

---
不成功紀錄2

想說看能不能用vispy
建了一個環境envs然後安裝vispy
結果好像我沒有裝Qt,一個gui使用者介面的api
且我的opengl driver好像不夠新
要裝Qt還得去裝visual studio來compile下載的Qt檔案才能build Qt
且因我的opengl好像不夠新 一定要2.1以上  我的是2.0
請看http://vispy.org/installation.html
所以想說先等等  試試其他方式好了
---
不成功紀錄3     2015/July/9
嘗試在windows32 xp上安裝pyopenGL到anaconda

用conda安裝一個virtual environment
conda create -n tonytest -c https://conda.anaconda.org/menpo pyopengl pyopengl-accelerate menpo-pyvrml97 simpleparse numpy pydispatcher pillow
包含所有此網頁上所建議的套件除了openglcontext之外
因為anaconda上面找不到win32的openglcontext
所以如果用上面方法加上openglcontext會無法建立environment
然後再用pip install的方式來裝openglcontext
pip install PyDispatcher PyVRML97 OpenGLContext
這樣是可以安裝成功
沒有錯誤訊息

不過在跑nehe第一個example的時候
會出錯
找不到glutInit
還在找原因當中

按照以下的方法把glut32.dll放到opengl/dlls裡
http://cim.mcgill.ca/~fmannan/comp557/Python%20and%20PyOpenGL%20Installation.html
解決了第一步glutinit undefined問題
並且可以跑此sample

但有第二個問題
glCreateShader, glCreateShaderObjectARB undefined
所以nehe的example還是跑不起來
---

Oct 30, 2014

陀螺儀教學文件的文書排版製作

關於陀螺教學文件的製作:

此文件的排版及其內容及圖片的製作幾乎都是用免費的開源軟體所完成。唯一使用的付費軟體為Scientific Workplace,我因其TEX的快速指令鍵而受惠,因此使用它來編輯tex文件。由於有些軟體公司在學校便宜的大量提供校園版權而進而讓學生產生對付費軟體的過度依賴,這似乎會導致在畢業以後學生會因為此依賴而對原本不需要的東西不自知產生需要,造成畢業學生不自知的付出過多的隱藏成本,因此這邊也做了一個示範。這邊用到的免費開源軟體皆為許多人多年的心血結晶,因此我也特別在此一一列出所用到的軟體。

1. 由於需要排出中英混雜,並且帶有許多數學公式的文件,而Scientific Workplace無法達到我的排版要求,這裡用上排版規則最為完整的排版引擎Latex,完整的說是TexLive 2013中的XeLatex排版引擎。其中也非常感謝套件xeCJK,其中文排版能力非常專業。
2. 所有向量圖片皆由開源軟體Python Matplotlib與其pgf套件,PSTricks與其套件pst-node所製作,這些軟件製作向量圖形都非常成熟。
3. 模擬程式是由開源軟體Python及其3D繪圖程式套件mplot3D所製作完成。Python軟體的強大已不用我在此贅述。
4. latex的minted套件,非常方便的就可以給出漂亮專業的程式碼區塊。
5. Sphinx,自動擷取程式說明行並自動排版成漂亮的說明書。我用它來作陀螺儀程式的說明書文件。這樣我就不用每次改了程式就要去改說明書,now sphinx will take care of that for me!

如果沒有這些軟體作者無私的貢獻,沒有這些強大的免費套件軟體,我根本無法完成此文件的排版,說不定還是幾張亂糟糟的計算紙。並且,我認為正確美觀的排版在知識的傳承與創意的迸發上有其重要的角色,這也是為什麼我願意花了近半年的時間來學習這些軟體並且應用它們。

這邊提到的開源軟體雖然強大,不過免費的畢竟會有其不方便的地方,我接下來就列舉我所遇到需要克服的困難,以及如何解決他們。
1. 使用Scientific Workplace 5.0編輯中文文件,但使用Xelatex來做排版。由於Scientific Workplace產生的中文文件使用其特殊的unicode編碼,因此要讓Xelatex讀到正確的中文字,要做個簡單的編碼轉換,我在我的blog中這篇文章有詳述如何轉換。我是寫了一個Python小程序來達成這個動作。這裡我學習到了電腦文字unicode與程式語言中的regular expression。
  • (2016/六月: 由於此份文件已經開始越來越大,目前已近70頁,若每次更改後要看結果都要全部編譯後才看的到那會很麻煩會等很久,因此我必須將整個latex擋拆開成一個章節一個tex檔,並且每個章節都可以獨立編譯,獨立看到章節的pdf輸出結果,這樣就可以一個章節修改後很快看到結果。然後最後在全部編譯。目前我是用python程式語言配合windows batch執行檔來達成此目標,即在batch檔案可以選擇只編譯章節或編譯整份文件,然後用python去調整preamble,最後batch在執行xelatex。
    不過後來我學到latex本身有一些套件可以達到此目的,目前考慮用import package,這樣才可以獨立章節編譯。還有想用另一個套件使得subdocument中可以用相對於subdoc所處在的subfolder路徑。)
2. 由於不想買以前學校裡學習時常用的向量繪圖軟體Adobe Illustrator,Acdsee Canvas等昂貴的向量繪圖軟體,我花了一些時間去衡量如何經濟地劃出精美精準的向量繪圖圖形。不過最後還是決定使用程式語言土法煉鋼來完成此需求,其實不會太花時間。因為付費的的繪圖軟體其實很多功能都不需要用到,常用的就那幾個,自己寫比較快。因此我使用Python與其強大的繪圖庫Matplotlib,Matplotlib製作的圖形的精美程度已經是國際期刊等級,有此可見其作者群之用心與藝術程度。我在這一篇blog中有些說明。不過,不方便的是,Python Matplotlib的EPS轉換還不完整,而點陣圖形在latex處理後產生的PDF檔又會有圖案模糊錯位的問題。因此,我找到Matplotlib中有前輩撰寫的PGF套件,可以完美的跟latex做結合,來產生精美的PDF圖形檔案。只能說非常感謝PGF套件的提供者,真的是非常好用阿!
3. minted套件有一個問題,就是他不會自動分割程式區塊,所以跨頁的程式區塊就要稍微用點技巧,參考這邊的解說,使用mdframed套件,並且在minted中不要使用背景顏色,讓mdframed來負責背景顏色的處理,然後將listing環境捨棄,這樣就可以跨頁了。
4. Sphinx中的autodoc套件,似乎還沒有辦法自動取得物件的屬性(autoattribute我無法用成功),我得用autoclass加:member:指令來達到相同的目的。
5. 若有太多大的pgf圖片,約400KB each,似乎會造成Tex compile時超過其記憶體上限500000,不之是否跟我有用shaded有關,我後來沒有去細究,就直接用python將資料存成資料檔,然後用PStricks的3D套件去存取資料檔並劃出來3D圖形。
以下為2016/三月更新
6. GIT版本管理(請見我的另一篇部落格文章)
7. Django,我用python的套件在pythonanywhere.com建立了一個初步網站,想要能夠慢慢脫離google blogger的ubiquity。初步紀錄

我使用的PSTricks的參考書 (非常推薦!): The LaTeX Graphics Companion (2nd Edition) Paperback – August 12, 2007 by Michel Goossens (Author), Frank Mittelbach

相關部落格文章:
http://whymranderson.blogspot.tw/2014/11/latex-minted-package-pygmentize-not.html


Memo for myself:
http://www.chinabaike.com/t/30826/2014/0517/2213116.html

Jul 21, 2014

Use python matplotlib to create 3D vector-based graphics, 3D vector graphics DIY

I drew the following graphs using Python and it's Matplotlib. If you are interested in the python code, they are here (not including the first gyroscope figure, will be added later). I am trying to improve the code into many functions/modules so that drawing can be convenient and automated. The part related to plotting starts from line 128, because this is just a portion of another bigger project. Just want to show this is not as time-consuming as one may think it is. We can create or draw beautiful vector-based graphs without buying expensive software like Adobe Illustrator or Acdsee Canvas. However as you can guess with this method you will need the numerical x,y,z locations of everything! (which is generated from yet another portion of the program!)

Mar 9, 2014

Typeset mix Chinese and English TeX documents with Scientific Workplace 5.0 and TEX live 2013

  You can typeset Chinese documents with Scientific Workplace (5.0) if you have just a few Chinese paragraphs. But if you have a long Chinese document or a document with mix Chinese and English characters, the software will suffer. The SWP's compiler for international characters, Omega, is just not good enough for this purpose. You will run into a lot of overfull hbox problems during typesetting. We will need another latex compiler for long documents of mix Chinese and English. But we will want to use the convenient editing features of Scientific Workplace, what should we do?

Here is something you can try. Still prepare your international document with SWP. But typeset it with another latex compiler. Here I use a popular one, XeLATEX in TEX live 2013. But there are a few problems. First, the file prepared by SWP has a character encoding that is not recognized by other latex compiler. So I wrote a small Python program to convert the encoding. Second, the figure insertion syntax generated by SWP is not recognized  by XeLatex. But this can be done by inserting TEX fields containing the correct Xelatex figure insertion commands into the SWP file by using SWP's TEX OBJECTS buttons. You won't be able to preview the figures SWP window but that doesn't really hurt. Here are step-by-step details:

  • How to convert SWP 5.0 special unicode format file to unicode correct characters:
SWP version 5.0 produced Chinese character in its special unicode codepoint format (if you open the tex file with any text editor, you will see every Chinese character has a form like \U{6211} instead of displaying 我 in the tex file.) This is due to the extra parenthesis {} after \U. So I wrote a small python program to automatically remove all {} in the tex file. So that the file can then be prepared by other tex programs (Tex Life 2013 in my case).

The program is very simple:
import re
import codecs
def dashrepl(matchobj):
    return chr(int(matchobj.group(1),16))
f1 = open('rotationV4pngtest.tex', 'r')   # name of your origianl file
f2 = codecs.open('rotationV4pngtest.tex.temp', "w", "utf-8")   #  name of the new file
p= re.compile(r"\\U{([\w]{,4})}")
for line in f1:
    m = re.sub(p,dashrepl,line)
##    print(m)
    f2.write(m)
f1.close()
f2.close()


  • For the SWP tex file to be readable by other compiler, you will also need to change the preamble (the packages and stuff)  in the beginning of the tex document so that other tex program can use it. Here is my case:
Original preamble generated by SWP:
\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{sw2unicode}
\usepackage[UT1]{fontenc}
\usepackage{pmingliu}
\usepackage[left=0.95in,right=0.95in,top=2cm,bottom=2.54cm]{geometry}
Change the above to the following so that Xelatex can complie:
\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{fontspec}
\usepackage{xeCJK}
\setmainfont[Mapping=tex-text]{Times New Roman} % rm
\setsansfont[Mapping=tex-text]{Arial}           % sf
\setmonofont{Courier New}                       % tt
\setCJKmainfont{微軟正黑體}
\usepackage[left=0.95in,right=0.95in,top=2cm,bottom=2.54cm]{geometry}
\usepackage{unicode-math}
\usepackage{graphicx}
\usepackage[hidelinks]{hyperref}


  • The file generated by SWP has symbols that use SWP's "tcilatex" macro, so you need to copy tcilatex.tex file into the same directory as your tex file. The location of tcilatex.tex in SWP's path has the following structure "D:\swp50\TCITeX\TeX\LaTeX\SWmacros\". Locate this file and copy it into your working directory. Then make sure your tex file has the \input{tcilatex.tex} line. You should have this line because you prepared your file with SWP.

  • Change the TEX figure insertion command:

The figure insertion command for Xelatex should look like this:
\begin{figure}[th]
\caption{{}}
\label{firstfig}
\begin{center}
\fbox{\includegraphics[scale=0.7]{cordtrans.JPG}}
\end{center}
\end{figure}

So open the SWP tex file with Notepad and replace all things look like the following with the above

\FRAME{fhF}{5.5097in}{3.7135in}{0pt}{}{}{cordtrans.JPG}{\special{language
"Scientific Word";type "GRAPHIC";maintain-aspect-ratio TRUE;display
"USEDEF";valid_file "F";width 5.5097in;height 3.7135in;depth
0pt;original-width 9.135in;original-height 6.1436in;cropleft "0";croptop
"1";cropright "1";cropbottom "0";filename
'
cordtrans.JPG';file-properties "XNPEU";}}

There could be other commands you need to change before you can run your tex document with TEX life Xelatex. Here I only show three points, which is enough for me to typeset this document. Good luck!

Mar 1, 2014

使用Scientific Workplace 5.0 + XeLatex軟體做中英文混雜並夾帶數學公式的長篇圖文文章電腦排版

  這邊介紹如何利用Scientific Workplace 5.0的編輯方便性,加上XeLatex (TeX life 2013)對中英文混雜的完美支援,來對中英文混雜並夾帶數學公式的長篇圖文文章做自動排版。

Feb 25, 2013

tracking of my python anaconda conda and packages setup

2016/Jan 22
current conda version and packages

C:\Documents and Settings\The One>conda list
# packages in environment at C:\Documents and Settings\The One\Anaconda:
#
_license                  1.1                      py27_0    
anaconda                  1.8.0                np17py27_0    
apptools                  4.2.0                    py27_0    
astropy                   0.2.5                np17py27_0    
atom                      0.3.4                    py27_0    
beautiful-soup            4.3.1                    py27_0    
beautifulsoup4            4.3.1                    
binstar                   0.3.1                    py27_1    
biopython                 1.62                 np17py27_0    
bitarray                  0.8.1                    py27_0    
blaze                     0.3                  np17py27_0    
bokeh                     0.2                  np17py27_1    
boto                      2.15.0                   py27_0    
casuarius                 1.1                      py27_0    
chaco                     4.3.0                np17py27_1    
colorama                  0.2.7                    py27_0    
conda                     3.19.0                   py27_0    defaults
conda-env                 2.4.5                    py27_0    defaults
configobj                 4.7.2                    py27_0    
cubes                     0.10.2                   py27_3    
cython                    0.19.2                   py27_0    
dateutil                  2.1                      py27_2    
distribute                0.6.45                   py27_0    
docutils                  0.11                     py27_0    
dynd-python               0.5.0                np17py27_0    
enable                    4.3.0                np17py27_0    
enaml                     0.8.3                    py27_0    
envisage                  4.3.0                    py27_0    
flask                     0.10.1                   py27_1    
gevent                    0.13.8                   py27_0    
gevent-websocket          0.3.6                    py27_2    
gevent-zeromq             0.2.2                    
gevent_zeromq             0.2.5                    py27_2    
greenlet                  0.4.1                    py27_0    
grin                      1.2.1                    py27_2    
h5py                      2.2.0                np17py27_0    
imaging                   1.1.7                    py27_2    
ipython                   1.1.0                    py27_0    
ipython-notebook          1.1.0                    py27_0    
ipython-qtconsole         1.1.0                    py27_0    
itsdangerous              0.23                     py27_0    
jinja2                    2.7.1                    py27_0    
keyring                   3.2                      py27_0    
launcher                  0.1.2                    py27_0    
libpython                 1.0                      py27_0    
llvmmath                  0.1.1                np17py27_2    
llvmpy                    0.12.0                   py27_0    
lxml                      3.2.3                    py27_0    
markupsafe                0.18                     py27_0    
matplotlib                1.3.1                np17py27_0    
mayavi                    4.3.0                np17py27_2    
mdp                       3.3                  np17py27_0    
menuinst                  1.3.2                    py27_0    defaults
meta                      0.4.2.dev                py27_0    
mingw                     4.7                           1    
msvc_runtime              1.0.1                     vc9_0  [vc9]  anaconda
networkx                  1.8.1                    py27_0    
nltk                      2.0.4                np17py27_0    
nose                      1.3.0                    py27_1    
numba                     0.11.0               np17py27_0    
numexpr                   2.2.2                np17py27_0    
numpy                     1.10.1                   py27_0    defaults
openpyxl                  1.6.2                    py27_0    
pandas                    0.12.0               np17py27_0    
patsy                     0.2.1                np17py27_0    
pep8                      1.4.6                    py27_0    
pil                       1.1.7                    
pip                       8.0.1                    py27_0    defaults
ply                       3.4                      py27_0    
psutil                    1.1.2                    py27_0    
py                        1.4.17                   py27_0    
pycosat                   0.6.1                    py27_0    defaults
pycparser                 2.9.1                    py27_0    
pycrypto                  2.6.1                    py27_0    
pyface                    4.3.0                    py27_0    
pyflakes                  0.7.3                    py27_0    
pygments                  2.0.2                    py27_0    defaults
pykit                     0.1.0                np17py27_0    
pyopengl                  3.1.1a1             np110py27_0    defaults
pyparsing                 1.5.6                    py27_0    
pyreadline                2.0.dev                  py27_0    
pysal                     1.6.0                np17py27_1    
pyside                    1.2.1                    py27_0    
pytables                  3.0.0                np17py27_1    
pytest                    2.4.2                    py27_0    
python                    2.7.5                         2    
python-dateutil           1.5                      
pytz                      2013b                    py27_0    
pywin32                   218.4                    py27_0    
pyyaml                    3.11                     py27_2    defaults
pyzmq                     2.2.0.1                  py27_0    
requests                  2.9.1                    py27_0    defaults
rope                      0.9.4                    py27_0    
scikit-image              0.9.3                np17py27_0    
scikit-learn              0.14.1               np17py27_0    
scipy                     0.13.0               np17py27_0    
setuptools                19.4                     py27_0    defaults
six                       1.4.1                    py27_0    
sphinx                    1.1.3                    py27_4    
spyder                    2.2.5                    py27_0    
spyder-app                2.2.5                    py27_0    
sqlalchemy                0.8.3                    py27_0    
statsmodels               0.5.0                np17py27_0    
sympy                     0.7.3                    py27_0    
tables                    3.0.0                    
tornado                   3.1.1                    py27_0    
traits                    4.3.0                    py27_0    
traitsui                  4.3.0                    py27_0    
vtk                       5.10.1                   py27_0    
werkzeug                  0.9.4                    py27_0    
wheel                     0.26.0                   py27_1    defaults
xlrd                      0.9.2                    py27_0    
xlwt                      0.7.5                    py27_0    

C:\Documents and Settings\The One>

this setting can run GS Simulation software as is currently(v1,developing v2).

----
2015/July/9
嘗試在windows32 xp上安裝pyopenGL到anaconda

用conda安裝一個virtual environment
conda create -n tonytest -c https://conda.anaconda.org/menpo pyopengl pyopengl-accelerate menpo-pyvrml97 simpleparse numpy pydispatcher pillow
包含所有此網頁上所建議的套件除了openglcontext之外
因為anaconda上面找不到win32的openglcontext
所以如果用上面方法加上openglcontext會無法建立environment
然後再用pip install的方式來裝openglcontext
pip install PyDispatcher PyVRML97 OpenGLContext
這樣是可以安裝成功
沒有錯誤訊息

不過在跑nehe第一個example的時候
會出錯
找不到glutInit
還在找原因當中

按照以下的方法把glut32.dll放到opengl/dlls裡
http://cim.mcgill.ca/~fmannan/comp557/Python%20and%20PyOpenGL%20Installation.html
解決了第一步glutinit undefined問題
並且可以跑此sample

但有第二個問題
glCreateShader, glCreateShaderObjectARB undefined

----
2013/2/25
Python 3.3 + Matplotlib 1.2.0 + numpy 1.7.0 + mpl_toolkits
FFmpeg needs to be installed on computer for creating animation mp4 films.
----

Nov 9, 2012

Scientific Workplace + Endnote Tricks

Last update 2/13/2013

Share some recent discoveries with the software Scientific Workplace 5.0
  1. To work with Endnote. - this includes adding a 'Label' to the bib file using a convenient small Matlab function and in the Endnote the export 'bibtex style' you can remove the unwanted fields (like the 'note' field) that you don't want it to appreare in the typesetting bibliography library.
 Alright it is time to give the full story. Sorry for the late reply :p! Anyway. I don't know if bibDB in SWP(scientific workplace) fixed this problem yet. If not, then I guess it is still useful for me to write this down for those people fighting with this problem. If they did fix it, that's even better. Actually after I checked Mackichan website I found this written in 2006, which means they haven't fix it or it is still not a complete solution. So I think my post will still be useful.