Grok vs. Django for RESTful API?

2012-06-24 Thread TG
Just hoping to get some opinions: Grok vs Django for REST? I've started 
evaluating TastyPie with Django. Is there something similar for Grok?

I'm working on a project that will mostly be mobile-app based. Though there 
will be a web interface, the mobile part is more important to us. Our data 
model is pretty straight-forward so far (it's a social networking type app), 
but may become a bit more involved if we begin to add information/event streams.
So finding a framework that works well with a dynamically changing data model 
is somewhat important (hopefully we won't change it too often). Having a good 
security/authentication/authorization framework is important too.

I consider myself pretty strong with Python and other languages, but web 
programming is new to me. I'm comfortable though with HTTP/SQL/etc.
-- 
http://mail.python.org/mailman/listinfo/python-list


numpy : efficient sum computations

2007-10-16 Thread TG
Hi there.

I want to do some intensive computations with numpy, and I'm
struggling a bit to find my wayy. Here is the problem :

m and d are two matrices :

 m.shape = (x,y,a,b)
 d.shape = (a,b)

I want to return
 i.shape = (x,y)
with
 i[x,y] = sum(m[x,y] * d)

I already found that
 m[:,:] * d
will give me a matrix of shape (x,y,a,b) containing the products.

Now I want to sum up on axis 2 and 3. If I do :
 (m[:,:] * d).sum(axis=3).sum(axis=2)
it seems like I get my result.

I'm wondering : is this syntax leading to efficient computation, or is
there something better ?

thanks

Thomas

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: numpy : efficient sum computations

2007-10-16 Thread TG
Okay, another one which I don't have answer for.

it is the reverse case, sort of :

 phi.shape (x,y)
 d.shape (a,b)

I want to return m :
 m.shape = (x,y,a,b)
with
m[x,y] = d * phi[x,y]

currently, my code is :
 m = empty(phi.shape + d.shape)
 m[:,:] = d

this repeats the matrix d x*y times, which is what I want. But now, if
I do :
 m[:,:] = d * phi[:,:]
in order to multiply each d by a specific value of phi, it doesn't
work.

type 'exceptions.ValueError': shape mismatch: objects cannot be
broadcast to a single shape

Right now I'm stuck here. If anyone can help, I would be glad.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boost python : get the shape of a numpy ndarray in C++ code.

2007-05-10 Thread TG
What I'm trying to say here : a numpy array is supposed to have it's
shape stored as a tuple. What I want to do is to access this
information from my C++ code, in order to do some validity check.

So, by looking around in the doc of boost/python/numeric.hpp I was
able to do this :

void
Layer::set_potentials (numeric::array array)
{
  for (int h=0; hmap-height; h++){
for (int w=0; wmap-width; w++){
  units[w+h*map-width]-potential =
extractfloat(array[make_tuple(w,h)]);
}
  }
}

which is fairly simple and actually works. Now, if I look further, I
see there is a method called getshape() in array class, which gives
back an object - I guess this object is a tuple, because the
documentation is quite poor.

So my idea is to get this object and use extract in order to get the
actual dimensions as integers.

but when I add this :

void
Layer::set_potentials (numeric::array array)
{
  object shape = array.getshape();

  [...]
}

It compiles, and then on execution I get this error :

AttributeError: 'numpy.ndarray' object has no attribute 'getshape'

Does it still have nothing to do with Boost.Python ?

-- 
http://mail.python.org/mailman/listinfo/python-list


Boost python : get the shape of a numpy ndarray in C++ code.

2007-05-09 Thread TG
Hi there.

I'm strugling here with some boost python code (damn I hate C++) :

All I want to do is to initialize the content of an array with a numpy
ndarray parameter. I have this, which actually works. But I want to
add some kind of data check such as :

* is array two dimensional ?
* are the dimensions corresponding to map's width / height ?
* is array field with floats or ints ?

void
Layer::set_potentials (numeric::array array)
{
  for (int h=0; hmap-height; h++){
for (int w=0; wmap-width; w++){
  units[w+h*map-width]-potential =
extractfloat(array[make_tuple(w,h)]);
}
  }
}


Some help is very welcome here ... thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


numpy performance and list comprehension

2007-04-03 Thread TG
Hi there.

Reading the page on python performance ( http://scipy.org/PerformancePython
) made me realize that I can achieve tremendous code acceleration with
numpy just by using u[:,:] kind of syntax the clever way.

Here is a little problem (Oja's rule of synaptic plasticity)

* W is a matrix containing the weights of connections between elements
i
and j
* V is an array containing the values of elements

I want to make W evolve with this rule :

dW[i,j] / dt = alpha * (V[i] * V[j] - W[i,j] * V[i]^2)

(don't pay attention to the derivate and stuff)

So, how would you write it in this nifty clever way ?

As a begining I wrote this :

  W += V.flatten().reshape((V.size,1)) *
V.flatten().reshape((1,V.size))

But it is not complete and, I guess, not efficient.

-- 
http://mail.python.org/mailman/listinfo/python-list


pasting numpy array into bigger array

2006-07-26 Thread TG
hi.

let's say I have :

from numpy import *
x = identity(5)
y = zeros((7,7))

I want to paste x into y, starting at coordinates (1,1) in order to
change y to something like this :

0 0 0 0 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 0

how would you do that ?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pasting numpy array into bigger array

2006-07-26 Thread TG
Thanks, that's exactly what I needed.

Tim Heaney wrote:
 You can use Python slice notation for each dimension
 
   y[1:6,1:6] = x


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: access to submodules

2006-07-20 Thread TG
okay, thanks everyone. this is much clearer now.

-- 
http://mail.python.org/mailman/listinfo/python-list


access to submodules

2006-07-19 Thread TG
hi.

This is my first try on modules.

I've got :

tom/
__init__.py
core.py
ui.py
data.py

then, when I'm in my ipython shell :

? from tom import *


this works, it loads core, ui and data
but when I do this :


? import tom

? tom.core
AttributeError: 'module' object has no attribute 'core'


Well, i guess I missed something, but I don't see what ...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: access to submodules

2006-07-19 Thread TG
I've just found this :

If I add :

import core, data, ui inside my  tom/__init__.py file, it will
work. But this line does not seems to exist in other files (after
having a look at several files inside /usr/lib/python2.4).

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: access to submodules

2006-07-19 Thread TG
I know this is a bad habit ... I was just doing it to show what is
disturbing me.

Obviously the star syntax finds the submodules because they are
loaded, but when I properly load the module alone with import tom,
the dot syntax does not find tom.core.

BartlebyScrivener wrote:
   from tom import *

 You CAN do this, but it's a bad habit.
 
 Try:
 
 import tom
 
 Then call by tom.function()
 
 rd

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: access to submodules

2006-07-19 Thread TG
BartlebyScrivener wrote:
 then you no longer need tom, you imported all of his FUNCTIONS (never
 heard of submodule).

my mistake, I was using the wrong name

tom/ -- package
   __init__.py
   core.py  
   data.py  these are modules contained in tom/
   ui.py  


if I import tom, it is supposed to load functions defined in
tom/__init__.py and make all the modules inside accessible through the
dot syntax.

Therefore, this is supposed to work :

? import tom
? help(tom.core)

AttributeError: 'module' object has no attribute 'core'

But if I use the bad star syntax

? from tom import *
? help(core)

this will work. So that means star loaded functions of __init__.py AND
modules contained, whereas dot syntax does not give me access to
modules inside. The issue is not about using or not using the import *
...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: access to submodules

2006-07-19 Thread TG
okay,

so only when I have inside __init__.py

__all__ = [core]

this works

? from tom import *
? help(core)

but (in a brand new interpretor)

? import tom
? help(tom.core)

AttributeError: 'module' object has no attribute 'core'

got it. But ...

? import numpy
? help(numpy.core)

this will work, even if core is a subpackage of numpy. and i don't have
to explicitly import numpy.core.

I must be missing something ... :-/

-- 
http://mail.python.org/mailman/listinfo/python-list


solving equation system

2006-07-17 Thread TG
Hi there.

Anyone knows how to use numpy / scipy in order to solve this ?

* A is an array of shape (n,)
* X is a positive float number
* B is an array of shape (n,)
* O is an array of shape (n,) containing only zeros.

A.X - B = O
min(X)

thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: solving equation system

2006-07-17 Thread TG

Ben C wrote:
 On 2006-07-17, TG [EMAIL PROTECTED] wrote:
  Hi there.
 
  Anyone knows how to use numpy / scipy in order to solve this ?
 
  * A is an array of shape (n,)
  * X is a positive float number
  * B is an array of shape (n,)
  * O is an array of shape (n,) containing only zeros.
 
  A.X - B = O
  min(X)

 Are we solving for A, B or X?  And what do you mean by min(X)?

 If we're solving for X there will be many combinations of A and B for
 which there is no solution.

Sorry for the poor explanation. I'm trying to put it clear now.

i've got A and B. I'm looking for X. I made a mistake in my equation
:-/

It's more like :

A.X - B = O

Well, maybe it will be much more simple if I explain the underlying
problem :

I have an array of N dimensions (generally 2).
- A first calculation gives me a set of integer coordinates inside this
array, which I will call the point W.
- After several other calculations, I've got a set of coordinates in
this N dimensional space that are floating values, and not bound to the
limits of my original N-array. This is the point L.

What I want to do is to translate the point L along the vector LW in
order to get a point L' which coordinates are inside the original
N-dimensional array. Then it will be easy to get the closest integer
coordinates from L'.


I'm not sure this is clear ... pretty hard to talk about maths in
english.

-- 
http://mail.python.org/mailman/listinfo/python-list


multinormal distribution

2006-07-07 Thread TG
hi there.

I'm struggling with a function of numpy. Here it is :


import numpy as NP
mean = NP.array([0,0])
cov = NP.array([[1,0.25],[0.25,1]])
v = NP.random.multivariate_normal(mean,cov)

Quite simple code : it is supposed to generate an array of two random
values taken from a multinormal distribution, with respect to means and
covariance matrix given as parameters.

What happens on my computer is simple : it freezes ! I can't even stop
the process with ctrl-C in my python interpreter i have to kill it from
outside.

I'm running : 
- python 2.4.1
- numpy 0.9.8

-- 
http://mail.python.org/mailman/listinfo/python-list


numpy : argmin in multidimensional arrays

2006-07-06 Thread TG
Hi there.

I am working with multi-dimensional arrays and I need to get
coordinates of the min value in it.

using myarray.argmin() returns the index in the flatten array, which is
a first step, but I wonder if it is possible to get the coordinates
directly as an array, rather than calculating them myself by using this
flat index and the shape of the array.

well, in fact i'm not sure to understand how argmin(myarray) works,
when myarray is multidimensional.

Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: numpy : argmin in multidimensional arrays

2006-07-06 Thread TG
thanks. unravel_index do the trick.

Travis E. Oliphant wrote:
 TG wrote:
  Hi there.
 
  I am working with multi-dimensional arrays and I need to get
  coordinates of the min value in it.
 
  using myarray.argmin() returns the index in the flatten array, which is
  a first step, but I wonder if it is possible to get the coordinates
  directly as an array, rather than calculating them myself by using this
  flat index and the shape of the array.
 
  well, in fact i'm not sure to understand how argmin(myarray) works,
  when myarray is multidimensional.


 By default, the argmin method flattens the array and returns the flat
 index.  You can get the corresponding element using

 myarray.flat[index]

 Alternatively, you can use the function unravel_index

 unravel_index(flat_index, myarray.shape)

 to return an N-dimensional index.


 If you give an axis argument, then the minimum is found along the
 specified dimension and you get an N-1 dimensional array of indices that
 will all be between 1 and myarray.shape[axis]
 
 
 -Travis

-- 
http://mail.python.org/mailman/listinfo/python-list


Numeric N-dimensional array initialization

2006-06-22 Thread TG
Hi there !

I'm just starting to use Numeric here, and I'm wondering : how can I
efficiently initialize every values of a N-dimensional array, given I
don't know the number of dimensions ?

I'm looking for something like a map function, or a way to conveniently
iterate through the whole N-array, but I didn't find anything ... yet.
If anyone has a clue, I'm listening.

Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric N-dimensional array initialization

2006-06-22 Thread TG
I tried to use Numeric.fromfunction, but there seems to be a problem :

the function called must have the right number of args (hint : the
number of dimensions, which I don't know). So i tried to use a function
like :

def myfunc(*args, **kw):
 return 0

and then i get :

 Numeric.fromfunction(myfunc,(5,5))
0

I'm a bit puzzled here

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric N-dimensional array initialization

2006-06-22 Thread TG
Thanks for your precious advices. The flat iterator is definitely what
i need.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Subclassing array

2006-05-05 Thread TG
That's great, thanks !

To put it short, when I create a Stimulus object, it first seek
__new__() method. But if I don't define it, it looks for the one
defined in Vector. This raises a problem because the parameters passed
to Stimulus(params) aren't fitting with Vector parameters, raising an
exception.

That's why I have to use this *arg **kw syntax in order to allow my
subclasses having any arguments without causing troubles. Am I right ?

-- 
http://mail.python.org/mailman/listinfo/python-list


Subclassing array

2006-05-04 Thread TG
Hi.

i've already something about inheriting from array a few weeks ago and
had my answer. But again, there is something that I don't understand.
Here is my vector class, which works quite well :

class Vector(array):
def __new__(cls,length,data=None):
return super(Vector,cls).__new__(cls,'f')

def __init__(self,length,data=None):
if data == None:
for _ in xrange(length):
self.append(0.0)
else:
for i in xrange(length):
self.append(data[i])



Now, i want to inherit from this vector class :

class Stimulus(Vector):
def __init__(self,width,height,label,data=None):
Vector.__init__(self,width*height,data)
self.width = width
self.height = height
self.label = label

This doesn't seem to work :
 s = Stimulus(10,10,data)
TypeError: __new__() takes at most 3 arguments (4 given)

In order to make it work, it seems that I have to redefine __new__
again, like this.

def __new__(cls,width,height,label,data=None):
return super(Stimulus,cls).__new__(cls,width*height)

Why is that ?
When I call Vector.__init__() in Stimulus, doesn't it also call __new__
? I don't understand the detail of callings to __new__ and __init__ in
python inheritance ...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inherit from array

2006-04-27 Thread TG
Hmm ... I'm definitely not a python wizard, but it seems to be quite a
special case that breaks the rules ... unpythonic, isn't it ?

Has anyone seen a PEP on this subject ?

Just in case a troll reads this message : i'm not saying python sucks
or has huge design flaws here ...

-- 
http://mail.python.org/mailman/listinfo/python-list


Inherit from array

2006-04-26 Thread TG
Hi there.

I'm trying to create a simple class called Vector which inherit from
array.

class Vector(array):
def __init__(self,length):
initialize a vector of random floats of size length. floats
are in interval [0;1]
array.__init__(self,'f')
for _ in xrange(length):
self.apprend(random())

but then :
 v = Vector(10)
TypeError: array() argument 1 must be char, not int

Well, I guess it means array's __init__ method is not called with
proper arguments ... It seems there is a problem with __init__
overloading, like when I call Vector(x), it directly calls __init__
method from array rather than the one defined in Vector class. Anyone
got an idea on this ?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inherit from array

2006-04-26 Thread TG
from array import array
class Vector(array):
def __init__(self,size):
print pouet
array.__init__('f')
print pouet

v = Vector('c')
print repr(v)

will output :

pouet
pouet
array('c')

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inherit from array

2006-04-26 Thread TG
Obviously, there is something I didn't catch in python's inheritance.

from array import array
class Vector(array):
def __init__(self,size):
print self.typecode
array.__init__(self,'f')

 v = Vector('c')
c

Here, it says the typecode is 'c' - I thought such an information was
initalized during the array.__init__(self,'f') but obviously I was
wrong.

Maybe the typecode is defined before, during the call to __new__ method
... But here i'm getting lost.

-- 
http://mail.python.org/mailman/listinfo/python-list