I'm trying to call a MATLAB function from python and continue with it's result in python. My script is as simple as this:
import librosa
import numpy as np
import matlab
import matlab.engine
eng = matlab.engine.start_matlab()
eng.addpath('TFDs/', '-end')
audio = librosa.chirp(0, 5000, duration=1, linear=True)
audio = matlab.double(audio.tolist())
choi_williams = eng.dtfd_nonsep(audio ,'cw',{30})
print(choi_williams)
I'm creating a linear chirp and expect to calculate it's time frequency distribution with the help of the fast_TFDs libary. The fast_TFDs folder is part of the current directory I'm executing the script from. But I'm recieving this error message:
Undefined function 'dtfd_nonsep' for input arguments of type 'cell'.
Traceback (most recent call last): File "cw.py", line 12, in
choi_williams = eng.dtfd_nonsep(audio ,'cw',{30}) File "/home/dunkeljo/tmp/anaconda3/envs/keras-gpu/lib/python3.8/site-packages/matlab/engine/matlabengine.py",
line 70, in call
return FutureResult(self._engine(), future, nargs, _stdout, File "/home/dunkeljo/tmp/anaconda3/envs/keras-gpu/lib/python3.8/site-packages/matlab/engine/futureresult.py",
line 67, in result
return self.__future.result(timeout) File "/home/dunkeljo/tmp/anaconda3/envs/keras-gpu/lib/python3.8/site-packages/matlab/engine/fevalfuture.py",
line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None,
out=self._out, err=self._err) matlab.engine.MatlabExecutionError:
Undefined function 'dtfd_nonsep' for input arguments of type 'cell'.#
So what am I doing wrong? I'm not even sure whether the issue is the input arguments or that the function cannot be found. Is the use of the function wrong? Maybe even the import of them? Or is there a problem with the input parameters? Anyone got a clue?
The solution was as wwweagle suggested: Simply add cell2mat an it works:
import librosa
import numpy as np
import matlab
import matlab.engine
eng = matlab.engine.start_matlab()
eng.addpath('TFDs/', '-end')
audio = librosa.chirp(0, 5000, duration=1, linear=True)
audio = eng.cell2mat(audio.tolist())
audio = eng.double(audio)
choi_williams = eng.dtfd_nonsep(audio, 'cw', {eng.double(30)})
print(choi_williams)
Related
I am trying to prepare a VM Resizing Recommendations Report using a Python 3.7 script. My code is as follows:
import datetime
import logging
from google.cloud import bigquery
from google.cloud import recommender
from google.cloud.exceptions import NotFound
from googleapiclient import discovery
def main(event, context):
client = recommender.RecommenderClient()
recommender_type = "google.compute.instance.MachineTypeRecommender"
projects=list_projects() #This gives list of projects
# I hard-code the zones below:
zones = ["us-east1-b","us-east1-c","us-east1-d","us-east4-c","us-east4-b","us-east4-a"
,"us-central1-c","us-central1-a","us-central1-f","us-central1-b"
,"us-west1-b","us-west1-c","us-west1-a"]
for zone in zones:
parent = client.recommender_path("my-project", zone, recommender_type)
for element in client.list_recommendations(parent): #In this line I am getting this error and these are logs
****Parent**** projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender
Traceback (most recent call last):
File "main.py", line 187, in main
x=list(client.list_recommendations(parent))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/google/cloud/recommender_v1/services/recommender/client.py", line 734, in list_recommendations
request = recommender_service.ListRecommendationsRequest(request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/proto/message.py", line 441, in __init__
raise TypeError(
TypeError: Invalid constructor input for ListRecommendationsRequest: 'projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender'
During handling of the above exception, another exception occurred:
"projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender" is the parameter being passed to the list_recommendations function. I am not sure what is wrong in the constructor as I am getting this: Invalid constructor input for ListRecommendationsRequest
I am new to this Google api. What can I try next?
I have checked the code on my end an it seems to be a syntax error when calling the method. As per the library documentation the first parameter should be a request object or None and the parent parameter has to be passed by name. Changing the line as follows I didn't have the error:
client.list_recommendations(parent=parent)
I am completely new to python.
I am trying to pass the location of Input and output using User interface as shown in this particular discussion [1]:How to give the location of "Input" and "Output" for a python code using User Interface and run the code from UI itself?
But here, I am calling an external command and trying to run it from my python code by passing location of input and output as in the above mentioned case.
from tkinter import *
from tkinter import filedialog
import numpy as np
import gdal
gdal.UseExceptions()
import os
def your_code(input_file, intermediate_file, output_file):
cmd = "gpt F:\saikiran\myGraph.xml -Psource=input_file - Ptarget=intermediate_file"
os.system(cmd)
ds = gdal.Open(intermediate_file)
band = ds.GetRasterBand(1)
……………………………………………...
#gen_map_button.place(x=230, y=300)
gen_map_button.pack()
root.mainloop()
But I encountered with this error :
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\User\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\User\GUI-pywt.py", line 145, in gen_map
your_code(input_filename, intermediate_filename, output_filename)
File "C:\Users\User\GUI-pywt.py", line 15, in your_code
ds = gdal.Open(intermediate_file)
File "C:\Users\User\Anaconda3\lib\site-packages\osgeo\gdal.py", line 3251, in Open
return _gdal.Open(*args)
RuntimeError: F:/saikiran/ddd: No such file or directory
What mistake did i do ?
Your cmd is not correct.
Concatenate string with values
cmd = "gpt F:\saikiran\myGraph.xml -Psource=" + input_file + " - Ptarget=" + intermediate_file
or use string formatting
cmd = "gpt F:\saikiran\myGraph.xml -Psource={} - Ptarget={}".format(input_file, intermediate_file)
With Python 3.6 or 3.7 you can use f-string
cmd = f"gpt F:\saikiran\myGraph.xml -Psource={input_file} - Ptarget={intermediate_file}"
Current cmd
"gpt F:\saikiran\myGraph.xml -Psource=input_file - Ptarget=intermediate_file"
will create file with name literally
intermediate_file
not
F:/saikiran/ddd
and it can make problem in gdal.Open()
Up until last week, I had a piece of code for calculating the odds of something occurring for a series of events, but starting this week the code doesn't work.
The code is the following:
import numpy as np
import pysal
import os
dbf = pysal.open('file.csv','r')
y = np.array([dbf.by_col('result')]).T
names_to_extract = ['dist', 'angle']
x = np.array([dbf.by_col(name) for name in names_to_extract]).T
id = np.array([dbf.by_col('incidenceId')]).T
model = pysal.spreg.probit.Probit(y, x, w=None, name_y='result', name_x=['dist','angle'], name_w=None, name_ds=None)
fin = np.column_stack((id, model.y, model.predy))
os.chdir("/destinyfolder")
np.savetxt('xg.csv', fin, delimiter=',', fmt='%d, %d, %f')
And I get this error:
/usr/local/lib/python3.7/site-packages/pysal/__init__.py:65: VisibleDeprecationWarning: PySAL's API will be changed on 2018-12-31. The last release made with this API is version 1.14.4. A preview of the next API version is provided in the `pysalnext` package. The API changes and a guide on how to change imports is provided at https://migrating.pysal.org
), VisibleDeprecationWarning)
Traceback (most recent call last):
File "xg.py", line 14, in <module>
model = pysal.spreg.probit.Probit(y, x, w=None, name_y='result', name_x=['dist','angle'], name_w=None, name_ds=None)
File "/usr/local/lib/python3.7/site-packages/pysal/spreg/probit.py", line 807, in __init__
n = USER.check_arrays(y, x)
File "/usr/local/lib/python3.7/site-packages/pysal/spreg/user_output.py", line 359, in check_arrays
if not spu.spisfinite(i):
File "/usr/local/lib/python3.7/site-packages/pysal/spreg/sputils.py", line 267, in spisfinite
return np.isfinite(a.sum())
File "/usr/local/lib/python3.7/site-packages/numpy/core/_methods.py", line 36, in _sum
return umr_sum(a, axis, dtype, out, keepdims, initial)
TypeError: cannot perform reduce with flexible type
Usually, I only got the DeprecationWarning, but the code worked. Since today, the code doesn't work. Consider that I am currently using pysal 1.14.4, numpy 1.16.2 and scipy 1.2.1. I have not updated my code to pysal 2.0 because I couldn't figure out how to port this code to the new version (and that's why I got the DeprecationWarning in the first place).
Here's the file: file.csv
Can you help me to make this work?
Entries in your x are strings, convert them to float:
x = np.array(...).T.astype(np.float)
This will fail at first because of "NULL" values in file.csv, you need to either filter these out or specify what float value they should be converted to.
Before posting a question, try first a simple google search for an immediate answer. If you google this error along with "pysal 1.14.4", you will immediately find this solution:
TypeError: cannot perform reduce with flexible type
I wrote the following code, which results in an error and I don't know how to fix it to work.
The code is:
# Name: ClipGDBtoNewGDB.py
# Description: Take an input GDB, create a list, iterate through each
feature class, clipping it and writing it to a new GDB.
# Author: tuilbox
# Import system modules
import arcpy, os
from arcpy import env
# Set workspace
env.workspace = arcpy.GetParameterAsText(0)
arcpy.env.overwriteOutput=True
# Set local variables
fclist = arcpy.ListFeatureClasses()
clip_features = arcpy.GetParameterAsText(1)
output_directory=arcpy.GetParameterAsText(2)
xy_tolerance = ""
outgdb=os.path.join(output_directory, arcpy.GetParameterAsText(3))
if not arcpy.Exists(outgdb):
arcpy.CreateFileGDB_management(output_directory,
arcpy.GetParameterAsText(3))
# Execute Clip within for loop
for fc in fclist:
arcpy.Clip_analysis(fc, clip_features, os.path.join(outgdb, fc))
The error is: Traceback (most recent call last):
File "F:/GIS_Joseph/Lab10_Joseph/ClipGDBtoNewGDB.py", line 17, in <module>
arcpy.CreateFileGDB_management(output_directory, arcpy.GetParameterAsText(3))
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\management.py", line 18878, in CreateFileGDB
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000735: File GDB Location: Value is required
ERROR 000735: File GDB Name: Value is required
Failed to execute (CreateFileGDB).
Any help would be appreciated. Thank you.
With this type of question it would be helpful to let us know what parameters you are passing into your script. Have you passed a valid parameter in position 3? Use arcpy.AddMessage to double check what value you are attempting to pass to arcpy.CreateFileGDB_management.
I'm running python2.5 and trying to use the astLib library to analyse WCS information in astronomical images. I try and get the object instanciated with the following skeleton code:
from astLib import astWCS
w = astWCS.WCS('file.fits') # error here
where file.fits is a string pointing to a valid fits file.
I have tried using the alternate method of passing a pyfits header object and this fails also:
import pyfits
from astLib import astWCS
f = pyfits.open('file.fits')
header = f[0].header
f.close()
w = astWCS.WCS(header, mode='pyfits') # error here also
The error is this:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/astro/phrfbf/build/lib/python2.6/site-packages/astLib/astWCS.py", line 79, in __init__
self.updateFromHeader()
File "/home/astro/phrfbf/build/lib/python2.6/site-packages/astLib/astWCS.py", line 119, in updateFromHeader
self.WCSStructure=wcs.wcsinit(cardstring)
File "/home/astro/phrfbf/build/lib/python2.6/site-packages/PyWCSTools/wcs.py", line 70, in wcsinit
return _wcs.wcsinit(*args)
TypeError: in method 'wcsinit', argument 1 of type 'char *'
When I run in ipython, I get the full error here on the pastebin
I know the astWCS module is a wrapped version of WCStools but i'd prefer to use the Python module as the rest of my code is in Python
Can anyone help with this problem?
Just found out the updated version of this library has fixed the problem, thanks for everyone's help
Oh sorry, I should have seen. Looking at the pastebin in more detail, the only error I can think of is that, for some reason the header has unicode in it. It can't be converted to char *, and you get the error. I tried searching for something in the header, but everything looks okay. Can you do this and post the output in another pastebin?
import pyfits
f = pyfits.open('file.fits')
header = f[0].header
f.close()
for x, i in enumerate(header.iteritems()):
if len(str(i[1])) >= 70:
print x, str(i[1])
cardlist = header.ascardlist()
cardstring = ""
for card in cardlist:
cardstring = cardstring + str(card)
print repr(cardstring)
Or, if you can check the header of your fits file for "funny" characters, getting rid of them should solve the issue.