레이블이 python인 게시물을 표시합니다. 모든 게시물 표시
레이블이 python인 게시물을 표시합니다. 모든 게시물 표시

11/28/2013

scipy optimize curve_fit fitting to cumtom function


ref:  http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

>>> import numpy as np
>>> from scipy.optimize import curve_fit
>>> def func(x, a, b, c):
...     return a*np.exp(-b*x) + c
>>> x = np.linspace(0,4,50)
>>> y = func(x, 2.5, 1.3, 0.5)
>>> yn = y + 0.2*np.random.normal(size=len(x))
>>> popt, pcov = curve_fit(func, x, yn)

4/12/2012

pylab - adjust the graph region, make x-axis ticks formatted

1. adjusting the margin of graph region

pylab.gcf().subplots_adjust(bottom=0.2)


2. x-axis ticks formatting
(ref: http://stackoverflow.com/questions/3677368/matplotlib-format-axis-offset-values-to-whole-numbers-or-specific-number )

from matplotlib.ticker import ScalarFormatter, FormatStrFormatter


# Plot the data...
fig = plt.figure()
ax = fig.add_subplot(111)

# Force the y-axis ticks to use 1e-9 as a base exponent
ax.yaxis.set_major_formatter(FixedOrderFormatter(-9))

# Make the x-axis ticks formatted to 0 decimal places
ax.xaxis.set_major_formatter(FormatStrFormatter('%0.0f'))

4/02/2012

scipy 0.7 to 0.9 arpack interface change

http://docs.scipy.org/doc/scipy-0.9.0/reference/release.0.9.0.html?highlight=arpack#arpack-interface-changes


Other changes

ARPACK interface changes

The interface to the ARPACK eigenvalue routines in scipy.sparse.linalg was changed for more robustness.
The eigenvalue and SVD routines now raise ArpackNoConvergence if the eigenvalue iteration fails to converge. If partially converged results are desired, they can be accessed as follows:
import numpy as np
from scipy.sparse.linalg import eigs, ArpackNoConvergence

m = np.random.randn(30, 30)
try:
    w, v = eigs(m, 6)
except ArpackNoConvergence, err:
    partially_converged_w = err.eigenvalues
    partially_converged_v = err.eigenvectors
Several bugs were also fixed.
The routines were moreover renamed as follows:
  • eigen –> eigs
  • eigen_symmetric –> eigsh
  • svd –> svds

12/10/2011

python interface with arduino, send key to x

http://www.stealthcopter.com/blog/2010/02/python-interfacing-with-an-arduino/




$ sudo apt-get install xdotool
$ xdotool key Control+Alt+Left

11/03/2011

calc python macro: print cell range

## set tmpRange => from (0, 0) to (5, 40)  <= (Col, Row)
tmpRange = oSheet.getCellRangeByPosition(0, 0, 8, 78)  

## oSheet1.setPrintAreas( Array() )
oSheet1.setPrintAreas( (tmpRange.RangeAddress, ) )

## set print margin

oFamilies = oDoc.getStyleFamilies()

oPageStyles = oFamilies.getByName("PageStyles") 

Style = oPageStyles.getByName(oSheet.PageStyle) 

Style.BackColor = &HCCCCFF 
Style.BottomMargin = 1000 
Style.TopMargin = 1000 
Style.LeftMargin = 1000 
Style.RightMargin = 500 

Style.PageScale = 77

Style.HeaderIsOn = False
Style.FooterIsOn = False

11/02/2011

python calc macro: create textbox (TextShape) on a sheet

oTextShape = oDoc.createInstance("com.sun.star.drawing.TextShape")  

oSheet.DrawPage.add(oTextShape)  

aa = oSheet.DrawPage.getByindex( n )   


## set string...  
aa.String = "babo hehe"   

## set Size...  
tmpSize = aa.getSize()  
tmpSize.Width = 9999  
aa.setSize(tmpSize)    

## set RotateAngle  
aa.RotateAngle = 9000  ## rotate 90 degree    

## paragraph adjust 
aa.ParaAdjust = 1  ## set right align 


4/08/2011

python-dialog example code..

python-dialog example code..

The usage is quite straightforward... very simple and easy to use.



import dialog
import time

d = dialog.Dialog()
d.setBackgroundTitle('what the hell')

## choice menu
aa = d.menu("asdf", choices=[('1', '1 means 1'), ('2', '2 means 2')])
print aa

## file or directory selection
aa = d.fselect("/home/junseok/", 10, 30)
print aa

## displaying a progress bar (gauge)
d.gauge_start('babo', percent=0)
for loopi in range(11):
d.gauge_update(loopi*10)
time.sleep(1)
d.gauge_stop()

1/14/2011

HOWTO sort sheets using python


import uno

def getReady():
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = resolver.resolve( "uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
oDoc = desktop.getCurrentComponent()

return oDoc

oDoc = getReady()
oSheets = oDoc.getSheets()

## getting all sheet name
sheetNames = []
for loopi in range(oSheets.Count):
tmpSheet = oSheets.getByIndex(loopi)
sheetNames.append(tmpSheet.Name)

## get sorting
sheetNames.sort(reverse=True)

for tmpName in sheetNames :
oSheets.moveByName(tmpName, 0)

12/22/2010

enabling 한글 address in http django..

When 한글 address are given to django, ascii error occurs .

Solution :


$ cat /usr/local/lib/python2.6/site-packages/sitecustomize.py
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
ref1: http://bbs.python.or.kr/viewtopic.php?p=64687&highlight=&sid=cee81191b135b182f8f28a25b37d42f7
ref2: http://blog.codeguruz.com/tag/python

Then,
$ vi /usr/lib/pymodules/python2.6/django/http/__init__.py
goto line 309 (or something..)
def _convert_to_ascii(self, *values):
"""Converts all values to ascii strings."""
for value in values:
if isinstance(value, unicode):
try:
#value = value.encode('us-ascii') ## remove this line
value = value.encode('utf-8') ## add this
except UnicodeError, e:
e.reason += ', HTTP response headers must be in US-ASCII format'
raise
else:
value = str(value)
if '\n' in value or '\r' in value:
raise BadHeaderError("Header values can't contain newlines (got %r)" % (value))
yield value

11/11/2010

Converting a date value read from a calc cell into readable format using python

When a date value, such as "2010-10-12" or etc., is in a cell,

oCell.getValue() and oCellRange.DataArray methods give just a number, not date format,
while oCell.getString() gives a date format string

The code below shows how to convert a date value into date format.


#!/usr/bin/python
import datetime, uno

# .... some lines to get oCell

# cell value shown in calc -> 2010-10-12
dateValue = oCell.getValue()
# get date value from a cell with getValue() method,
# note that I did NOT used the getString() method.

print dateValue # this gives 40463.0 <- This means the elapsed date from 1899-12-30

DateValue = datetime.timedelta(dateValue) + datetime.date(1899, 12, 30) # converting
# DateValue -> datetime.date(2010, 10, 12)

print DateValue # this gives 2010-10-12.



* The starting date 1899-12-30 can be changed in option > openoffice Calc > calculate > date

11/06/2010

Control Calc chart using python

ref: http://wiki.services.openoffice.org/wiki/Documentation/BASIC_Guide/Structure_of_Charts

ref: http://www.oooforum.org/forum/viewtopic.phtml?p=56037


Legend show/hide

oChart = oSheet.Charts.getByIndex(0).EmbeddedObject
oChart.HasLegend = False


Set main title

oChart.HasMainTitle = True
oChart.Title.String = "main-title-string"


Set XAxis title

oDiagram = oChart.Diagram
oDiagram.HasXAxisTitle = True
oDiagram.XAxisTitle.String = "x-title"


Set XAxis label text rotation

oDiagram.XAxis.TextRotation = 90 * 100 # for 90 degree rotation

Set cell border

oBorderLine = createUnoStruct("com.sun.star.table.BorderLine2")
oBorderLine.InnerLineWidth = 2
oBorderLine.Color = 255 # blue

oCell.LeftBorder = oBorderLine
oCell.RightBorder = oBorderLine

Set cellRange border (apply to every cells)

oCellRange = oSheet.getCellRangeByPosition(1, 2, 3, 4)
oCellRange.BottomBorder = oBorderLine

Set cellRange TableBorder (apply to only edge lines not inner lines)

oCellRange.TableBorder -> com.sun.star.table.TableBorder

oTableBorder = createUnoStruct("com.sun.star.table.TableBorder")

# there are 2 types: BorderLine and BorderLine2. Confusing, be careful to choose.
oBorderLine = createUnoStruct("com.sun.star.table.BorderLine")
#oBorderLine2 = createUnoStruct("com.sun.star.table.BorderLine2")

# make oTableBorder object
oTableBorder.BottomLine = oBorderLine
oTableBorder.TopLine = oBorderLine
oTableBorder.LeftLine = oBorderLine
oTableBorder.RightLine = oBorderLine

# ... something like this ...
oTableBorder.IsTopLineValid = True
oTableBorder.IsBottomLineValid = True
oTableBorder.IsLeftLineValid = True
oTableBorder.IsRightLineValid = True

# set
oCellRange.TableBorder = oTableBorder
### Sometimes the code above does not work.
### Be sure that "oTableBorder.IsTopLineValid = True" line is correct.



.....

10/20/2010

python encoding error solution

/usr/lib/python2.6/site.py

find 'ascii' and change to 'utf-8'

7/15/2010

openoffice calc macro : Copy a chart to GDI meta format by python macro


def myAddChart(oDataRangeAddress, oSheet, oChartPositionCell):
tmpChartName = "tmpChartName"
#--------- make a chart
oCharts = oSheet.Charts

X = oChartPositionCell.Position.X
Y = oChartPositionCell.Position.Y

oCharts.addNewByName(tmpChartName, makeRectangle( X, Y, 8000, 3500 ), Array( oDataRangeAddress ), True, True)
oChart = oCharts.getByName(tmpChartName)

#==========================================================
#--------- Copy the Chart to GDI meta image into the position on where the chart is
#==========================================================
oDoc = getReady()
oController = oDoc.getCurrentController()

oDrawPage = oSheet.getDrawPage()
nNumShapes = oDrawPage.getCount()

### select the chart.
### No way for direct selecting a chart. So, get shapes and find the chart from shapes.
for loopi in range(nNumShapes) :
oShape = oDrawPage.getByIndex( loopi )
if "CLSID" in dir(oShape) : # only charts have the CLSID attribute
if oShape.CLSID == "12DCAE26-281F-416F-a234-c3086127382e" : # this is the CLSID of charts
oController.select( oShape )
break

# PropertyValue format=3 means the PasteSpecial format to be the GDI meta file format
PropertyValue = uno.createUnoStruct("com.sun.star.beans.PropertyValue")
PropertyValue.Name = "Format"
PropertyValue.Value = 3

serviceMgr = uno.getComponentContext().ServiceManager
dp = serviceMgr.createInstance('com.sun.star.frame.DispatchHelper')
#--- copy the selected object (chart)
dp.executeDispatch( oController.getFrame(), ".uno:Copy", "", 0, ())
#--- remove the selected object
oCharts.removeByName(tmpChartName)
#--- now select the cell where the GDI meta image will be on
oController.select(oChartPositionCell)
#--- paste
dp.executeDispatch( oController.getFrame(), ".uno:PasteSpecial", "", 0, (PropertyValue,)) # <- The last (PropertyValue,) option is important.




..

7/06/2010

python code snippets for openoffice calc

getReady() -> return oDoc (calc document)
gotoOffset -> similar to Range.Offset(x, y) in excel VBA
gotoLeftEnd -> Range.End(xlToLeft) in VBA for excel


import uno

def gotoOffset(oCell, coffset, roffset):
currentColumn = oCell.getCellAddress().Column
currentRow = oCell.getCellAddress().Row

newColumn = currentColumn + coffset
newRow = currentRow + roffset

if newColumn < 0 : newColumn = 0
if newRow < 0 : newRow = 0

tmpCell = oCell.getSpreadsheet().getCellByPosition(newColumn, newRow)
return tmpCell


def gotoRightEnd(oCell):
tmpCell = oCell
while True :
tmpCell = gotoOffset(tmpCell, 1, 0)
if tmpCell.getString() == '' :
tmpCell = gotoOffset(tmpCell, -1, 0)
break
return tmpCell

def gotoLeftEnd(oCell):
tmpCell = oCell
while True :
tmpCell = gotoOffset(tmpCell, -1, 0)
if tmpCell.getRangeAddress().StartColumn == 0 :
break
if tmpCell.getString() == '' :
tmpCell = gotoOffset(tmpCell, 1, 0)
break
return tmpCell

def gotoTop(oCell):
tmpCell = oCell
while True :
tmpCell = gotoOffset(tmpCell, 0, -1)
if tmpCell.getRangeAddress().StartRow == 0 :
break
if tmpCell.getString() == '' :
tmpCell = gotoOffset(tmpCell, 0, 1)
break
return tmpCell

def gotoBottom(oCell):
tmpCell = oCell
while True :
tmpCell = gotoOffset(tmpCell, 0, 1)
if tmpCell.getString() == '' :
tmpCell = gotoOffset(tmpCell, 0, -1)
break
return tmpCell



def getReady():
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = resolver.resolve( "uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
oDoc = desktop.getCurrentComponent()

return oDoc

7/02/2010

Searching a text in openoffice calc sheet using python code


sd = oSheet.createSearchDescriptor()
sd.setSearchString("SearchString")

fc = oSheet.findFirst(sd)

while True :
if fc == None : break
fc.CellBackColor = 12341234
fc = oSheet.findNext(fc, sd)

Set optimal width for all columns of a sheet in openoffice calc with python script code

oSheet.Columns.OptimalWidth = True

.

set image size in openoffice calc with python code

1. resizing selected images


def ResizingSelectedImages(height = 10000, width = 8000):
oDoc = getReady()
cSelection = oDoc.getCurrentSelection()
for loopi in range(cSelection.Count) :
tmpImage = cSelection.getByIndex(loopi)
tmpSize = tmpImage.Size
tmpSize.Width = width
tmpSize.Height = height

tmpImage.setSize(tmpSize)



2. Resizing the first images (getByIndex(0)) in the selected sheet.



oSheet = oDoc.getSheets().getByIndex(0)
oDraw = oSheet.getDrawPage()
oImage = oDraw.getByIndex(0)

newSize = oImage.Size
newSize.Height = 20000
newSize.Width = 20000

oImage.setSize(newSize)

select (activate) a sheet in openoffice calc by python code


import uno
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
# note the port number is 8100, where the default value is 2002
ctx = resolver.resolve( "uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
oDoc = desktop.getCurrentComponent()

# sheet to be activated
oSheet = oDoc.getSheets().getByIndex(2)

# activate the sheet
oController = oDoc.getCurrentController()
oController.setActiveSheet(oSheet)

# set first visible row
oController.setFirstVisibleRow(3)

7/01/2010

openoffice start option enable python macro

openoffice calc should be run with the command


$ ooffice -calc --accept="socket,host=localhost,port=8100;urp;StarOffice.ServiceManager"


or edit /usr/bin/ooffice (in ubuntu)


$ cat /usr/bin/ooffice
#!/bin/sh
/usr/lib/openoffice/program/soffice "$@" --accept="socket,host=localhost,port=8100;urp;StarOffice.ServiceManager"
$

2/10/2010

Deciding if a Point is Inside a Polygon - Pure Python

Method using matplotlib.nxutils

from matplotlib.nxutils import points_inside_poly
points_inside_poly([[0, 0], [0, 1]], [[1, 0], [1, 1], [0, 1]])


Using python-shapely (http://trac.gispython.org/lab/wiki/Shapely)
(http://gispython.org/shapely/manual.html#polygons)

$ sudo apt-get install python-shapely


in python,

from shapely.geometry import Polygon, Point
polygon2 = Polygon(((0, 0), (0, 1), (1, 1), (1, 0)))
print polygon2.contains(Point(0.1, 0.1)) # print True



another method :

ref : http://www.ariel.com.au/a/python-point-int-poly.html


#!/usr/bin/python
# determine if a point is inside a given polygon or not
# Polygon is a list of (x,y) pairs.
def point_inside_polygon(x,y,poly):

n = len(poly)
inside =False

p1x,p1y = poly[0]
for i in range(n+1):
p2x,p2y = poly[i % n]
if y > min(p1y,p2y):
if y <= max(p1y,p2y):
if x <= max(p1x,p2x):
if p1y != p2y:
xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
if p1x == p2x or x <= xinters:
inside = not inside
p1x,p1y = p2x,p2y

return inside


.