top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Finding Relative Maxima in Python3

0 votes
549 views

The last few days I've been working on a script to manipulate some scientific data. One thing I would like to be able to do is find relative maxima in a data set.
I'm using numpy in python3 (which I think I can't do without because of utf16 encoding of my data source) and a series of np.arrays. When looking around the web and some forums I came across the scipy function argrelextrema, which seemed to do just what I wanted. The problem is that I can't get the function to work, probably because scipy in python3 does not yet support the argrelextrema function. I can however, not find a reference to this really being the problem, and was wondering if someone here could maybe help me out.
The code I used is shown below. The function to return the maximum values is called by a different script. Then, when running, it returns an error like the one below the code.

Script:
import numpy as np
import scipy as sp

def max_in_array_range(MaxArray, Column, LowBound):

 "Finding the max value an array with a predefined in a certain column and from a threshold index.
 The complete array is loaded through Max Array. The column is first selected. 
 The, the LowBound is called (the data is only interesting from a certain point onward."
 TempArray = MaxArray[:, Column] # Creation of Temporary Array to ensure 1D Array and specification of the LowBound in the next line.
 return sp.argrelextrema(TempArray[LowBound:], np.greater)

Error message:
Traceback (most recent call last):
 File "MyScript.py", line 15, in 
 Varrr = FD.max_in_array_range(CalcAndDiffArray, 5 ,MyBound)
 File "/MyPath/Script.py", line 82, in max_in_array_range
 return sp.argrelmax(TempArray[LowBound:], np.greater)
AttributeError: 'module' object has no attribute 'argrelmax'
posted May 31, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

he docs would seem to indicate that that function resides in the "signal" submodule of scipy:
http://docs.scipy.org/doc/scipy-dev/reference/generated/scipy.signal.argrelmax.html#scipy.signal.argrelmax
Hence, it would be sp.signal.argrelmax() as opposed to just sp.argrelmax()

answer May 31, 2013 by anonymous
Similar Questions
+2 votes

I wanna simulate C style integer division in Python3.

So far what I've got is:

# a, b = 3, 4

import math
result = float(a) / b
if result > 0:
 result = math.floor(result)
else:
 result = math.ceil(result)

I found it's too laborious. Any quick way?

+1 vote

I seem to stumble upon a situation where "!=" operator misbehaves in python2.x. Not sure if it's my misunderstanding or a bug in python implementation. Here's a demo code to reproduce the behavior -

From __future__ import unicode_literals, print_function

class DemoClass(object):
   def __init__(self, val):
   self.val = val

   def __eq__(self, other):
   return self.val == other.val

  x = DemoClass('a')
  y = DemoClass('a')

  print("x == y: {0}".format(x == y))
  print("x != y: {0}".format(x != y))
  print("not x == y: {0}".format(not x == y))

In python3, the output is as expected:

x == y: True
x != y: False
not x == y: False

In python2.7.3, the output is:

x == y: True
x != y: True
not x == y: False

which is not correct!!

0 votes

I'm starting a new project from scratch so I think its finally a time to switch to the latest and greatest Python 3.4.

But I'm puzzled with MySQL support for Python 3. So far the only stable library I've found it pymysql.

All others are either abandoned work-in-progress projects or do not support Python 3:

  • mysqldb - Python 2.x only
  • mysql-ctypes - Python 2.x only
  • amysql - Python 2.x only
  • ultramysql - Python 2.x only
  • MySQL Connector/Python - new guy in block. Does anyone use it?
  • WebScaleSQL + MySQLdb1 - still in development, state unknown?

So what library do you use for MySQL access in Python 3? I'm specifically interested in async support (like in psycopg2 for PostgreSQL) since I'm planning to use Tornado.

0 votes

I have read that asyncio is a great addition to Python 3. I have looked around and saw the related PEP which is quite big BTW but couldn't find a simple explanation for why this is such a great addition. Any simple example where it can be used?

It can be used to have a queue of tasks? Like threads? Maybe light weight threads? Those were my thoughts but the library reference clearly stated that this is single-threaded. So there should be some waiting time in between the tasks. Then what is good?

These are just jumbled thoughts that came into my mind while trying to make sense of usefulness of asyncio. Anyone can give a better idea?

0 votes

I need a command that will make threads created by "multiprocessing.Process()" wait for each other to complete. For instance, I want to do something like this:

job1 = multiprocessing.Process(CMD1())
job2 = multiprocessing.Process(CMD2())

jobs1.start(); jobs2.start()

PY_FUNC()

The command "PY_FUNC()" depends on the end result of the actions of CMD1() and CMD2(). I need some kind of wait command for the two threads that will not let the script continue until job1 and job2 are complete. Is this possible in Python3?

...