I created a f2py python interface for PITCON7 (pitcon7.f90). The interface exposes two functions pitcon and dge_slv from pitcon7.f90. dge_slv is passed as a callback to pitcon. However on executing a sample problem that invokes pitcon I get the error
Traceback (most recent call last):
File "pitcon_problem01.py", line 91, in <module>
result = run(30)
File "pitcon_problem01.py", line 58, in run
ierror, iwork, rwork, xr = pitcon(df, fpar, fx, ierror, ipar, iwork, liw, nvar, rwork, lrw, xr, dge_slv)
TypeError: interface.pitcon() argument 12 must be tuple, not fortran
The pitcon7.pyf file is posted to pastebin
The sample problem pitcon_propblem01.py has also been posted to pastebin . This is the python equivalent of the Fortran pitcon7_test1.f90 from the same upstream source, linked above.
I do not see why the f2py interface expects the callback to be a tuple, since there does not seem to be any argument mismatch.
Related
I'm currently trying to select a layer. In Qgis 2, this was done by doing
from qgis import processing
lyrConsumer = processing.getObject('contours-iris-2014')
But now, the documentation says that I have to use QgsProcessingUtils.mapLayerFromString() in Qgis3. Apparently, I need to put a second argument now, as I get this error.
Traceback (most recent call last):
File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
TypeError: QgsProcessingUtils.mapLayerFromString(): not enough arguments
What is the second argument?
The second parameter is a QgsProcessingContext(), which lets the algorithm know what is the context in which it will run.
You can set it in this way:
context = QgsProcessingContext()
context.setProject(QgsProject.instance())
QgsProcessingUtils.mapLayerFromString('my_layer', context)
However, since you said you're trying to select a layer, if you're attempting to get a layer from the QGIS layer tree, you can have a look at Getting layer by name in PyQGIS?.
Visit QGIS API Documentation you will find the answer.
I'm trying to execute this code with mpi4py:
from mpi4py import MPI
import numpy
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
inp = numpy.random.rand(size)
senddata = inp[rank]
recvdata=comm.reduce(senddata,None,root=0,op=MPI.MINLOC)
print 'on task',rank,'reduce: ',senddata,recvdata
recvdata=comm.allreduce(senddata,None,op=MPI.MINLOC)
print 'on task',rank,'allreduce: ',senddata,recvdata
With this command:
$ mpirun -np 4 python ./reduce_minlock.py
But instead of the expected result I'm getting this message:
Traceback (most recent call last):
Traceback (most recent call last):
File "./reduce_minlock.py", line 11, in <module>
Traceback (most recent call last):
File "./reduce_minlock.py", line 11, in <module>
recvdata=comm.reduce(senddata,None,root=0,op=MPI.MINLOC)
File "MPI/Comm.pyx", line 1298, in mpi4py.MPI.Comm.reduce (src/mpi4py.MPI.c:109386)
TypeError: reduce() got multiple values for keyword argument 'op'
recvdata=comm.reduce(senddata,None,root=0,op=MPI.MINLOC)
File "MPI/Comm.pyx", line 1298, in mpi4py.MPI.Comm.reduce (src/mpi4py.MPI.c:109386)
TypeError: reduce() got multiple values for keyword argument 'op'
Traceback (most recent call last):
File "./reduce_minlock.py", line 11, in <module>
recvdata=comm.reduce(senddata,None,root=0,op=MPI.MINLOC)
File "MPI/Comm.pyx", line 1298, in mpi4py.MPI.Comm.reduce (src/mpi4py.MPI.c:109386)
TypeError: reduce() got multiple values for keyword argument 'op'
File "./reduce_minlock.py", line 11, in <module>
recvdata=comm.reduce(senddata,None,root=0,op=MPI.MINLOC)
File "MPI/Comm.pyx", line 1298, in mpi4py.MPI.Comm.reduce (src/mpi4py.MPI.c:109386)
TypeError: reduce() got multiple values for keyword argument 'op'
I got this code from this Tutorial. What I don't understand is why there is a type error for reduce when I'm using the exact number of parameters. I wonder if MPI.MINLOC is supported by mpi4py. I did not find any warning about this operation on the documentation. These are my system configurations:
$ mpirun --version
mpirun (Open MPI) 1.10.3
Report bugs to http://www.open-mpi.org/community/help/
$ python --version
Python 2.7.12
$ cat /etc/fedora-release
Fedora release 24 (Twenty Four)
Any help?
Reading more carefully into the error messages and trying to understand them could save a lot of potential trouble.
TypeError: reduce() got multiple values for keyword argument 'op'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a purely Pythonic run-time error and has nothing to do with MPI per se. It should prompt you to look up the correct signature of MPI.Comm.reduce() first and only after checking with it state that the number of arguments is exact. And indeed, a look into Comm.pyx reveals that reduce() takes only three arguments (one required and two defaulted) besides the self reference:
def reduce(self, sendobj, op=SUM, int root=0):
You are providing two arguments as positional and two as name-value pairs. The second positional argument None and the second named one both provide values for op, therefore the type error. Similarly, one could check that allreduce() takes only two arguments and not three.
The conclusion is that the tutorial is wrong and probably based on an earlier version of mpi4py and the number of arguments you are passing to reduce() and allreduce() is actually not exact. You should drop the None argument from both calls.
Recently I managed to compile newest opencv 3.1 with cuda support.
After some tinkering I properly converted most of my python code from 2.4.x to 3.1.x wihout any problems.
But when it came time to try out the stereCalibrate capability, the error occured:
Exception in thread Thread-5:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "./stereo_compute.py", line 245, in calibrate
flags)
TypeError: an integer is required
Here is how I call the function itself:
criteria = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS,
30, 1e-56)
flags = (cv2.CALIB_FIX_ASPECT_RATIO +
cv2.CALIB_ZERO_TANGENT_DIST +
cv2.CALIB_SAME_FOCAL_LENGTH)
(value,
self.np_calib_data['lmtx'], self.np_calib_data['ldist'],
self.np_calib_data['rmtx'], self.np_calib_data['rdist'],
self.np_calib_data['R'], self.np_calib_data['T'],
self.np_calib_data['E'], self.np_calib_data['F']
) = cv2.stereoCalibrate(
object_points,
l_image_points,
r_image_points,
(image_size[1], image_size[0],),
self.np_calib_data['lmtx'],
self.np_calib_data['ldist'],
self.np_calib_data['rmtx'],
self.np_calib_data['rdist'],
self.np_calib_data['R'],
self.np_calib_data['T'],
self.np_calib_data['E'],
self.np_calib_data['F'],
flags,
criteria)
Everything runs in a thread, that's why it's mentioned in the exception.
I can't get the correct set of parameters.
In addition the call worked for me under 2.4.x version with the same set of data.
Please help!
I have noticed that with Python bindings for OpenCV, if a function has a parameter with default value, say, None, you often can't explicitly use this parameter with its default value. This is quite against normal Python conventions and expected behaviour.
For example, function cv2.goodFeaturesToTrack has parameter blockSize with default value None, so you would expect that calling
cv2.goodFeaturesToTrack(image=img, maxCorners=10, qualityLevel=0.1, minDistance=10, mask=None, blockSize=None)
would be the same as
cv2.goodFeaturesToTrack(image=img, maxCorners=10, qualityLevel=0.1, minDistance=10, mask=None)
but in fact, first way of using this function will result in
TypeError: an integer is required
So, with OpenCV you have to either not provide and argument, or provide correct value (and the one which is default, according to Python function/method signature, may not be correct).
You will have to check C++ sources to find actual default values.
I am porting CPython to Emscripten, and it builds successfully. However, when I try to run the python.asm.js through Node.js, I get a very strange error inside the Py_InitializeEx(0) call:
Traceback (most recent call last):
File "/lib/python2.7/site.py", line 62, in <module>
import os
File "/lib/python2.7/os.py", line 44, in <module>
from posix import *
TypeError: 'NotImplementedType' object does not support indexing
The error is generated from PySequence_GetItem in Objects/abstract.c, but I don't understand how the execution gets there. If I do import posix before the line that causes the error, the import posix statement finish successfully, and I can call functions in the posix module. Thus, the error is related to from <module> import * line. How is PySequence_GetItem related to from <module> import * statement, and what could be the reasons for the error?
If you want to reproduce the problem, I released the code on GitHub
While investigating what is going wrong, I switched off the optimization (compiled and linked with -O0). The resulting JS executable also failed, but with a different error:
Invalid function pointer '495' called with signature 'iii'. Perhaps this is
an invalid value (e.g. caused by calling a virtual method on a NULL pointer)?
Or calling a function with an incorrect type, which will fail? (it is worth
building your source files with -Werror (warnings are errors), as warnings can
indicate undefined behavior which can cause this)
This pointer might make sense in another type signature:
ii: _dict_keys iiii: 0 i: undefined iiiii: 0 viii: 0 vii: 0 vi: 0 v: 0
495
495
I looked through Emscripten's settings.js for options related to function pointers, and found EMULATE_FUNCTION_POINTER_CASTS which fixed the problem.
Im working with Agilent IVI drivers in Python 2.7.9 and can't seem to get 'proven' code to work on a particular Windows 7 machine. It executes successfully on other machines.
While this issue seems rather limited to one instrument, it appears to be a broader Python issue, so I turn to Stack Overflow for help. Any help or insight would be great.
The following code
# Import the TLB
from comtypes.client import GetModule, CreateObject
GetModule('AgInfiniium.dll')
# Pull in the coefficients and classes, we'll need those
from comtypes.gen.AgilentInfiniiumLib import *
# Instantiate the driver
OScope = CreateObject('AgilentInfiniium.AgilentInfiniium')
# Make constants of our settings
Address = "TCPIP0::10.254.0.222::INSTR"
resetOScope = False
# Open a connection to the scope
OScope.Initialize(Address,False,resetOScope,'')
# Make a measurement
print OScope.Measurements.Item("Channel1").ReadWaveformMeasurement(
MeasFunction=AgilentInfiniiumMeasurementAmplitude, MaxTime=10)
yields the following error:
Traceback (most recent call last):
File "P:\Aperture\Validation\WMI_BGA_Board\TestMatrixCode\scopeTest2.py", line 29, in <module>
print OScope.Measurements.Item("Channel1").ReadWaveformMeasurement(MeasFunction=AgilentInfiniiumMeasurementAmplitude ,MaxTime=10)
File "C:\Python27\lib\site-packages\comtypes-1.1.0-py2.7.egg\comtypes\__init__.py", line 656, in call_with_inout
rescode = list(rescode)
TypeError: 'c_double' object is not iterable
In my limited debugging attempts, I have seen that this call_with_inout
function tries to convert my Python arguments into arguments for the following C++ function:
public void ReadWaveformMeasurement(
AgilentInfiniiumMeasurementEnum MeasFunction,
AgilentInfiniiumTimeOutEnum MaxTime,
ref double pMeasurement)
It's creating some kind of variable for the pMeasurement pointer that ends up being type c_double, and then complains that it's not iterable.
At this point, this seems like it's local to this machine. I've gone to the extent of uninstalling Python, reinstalling the Agilent driver, and trying two versions of comtypes (1.1.0 and 1.1.1). Yet the problem persists. Any ideas? Thanks.