attempt to get argmax of an empty sequence - python

when tried to execute this code, it is showing 'attempt to get argmax of an empty sequence' error.
code:
import re
import numpy as np
code:
import re
import numpy as np
output_directory = './fine_tuned_model'
lst = os.listdir(model_dir)
lst = [l for l in lst if 'model.ckpt-' in l and '.meta' in l]
steps=np.array([int(re.findall('\d+', l)[0]) for l in lst])
last_model = lst[steps.argmax()].replace('.meta', '')
last_model_path = os.path.join(model_dir, last_model)
print(last_model_path)
!python /content/models/research/object_detection/export_inference_graph.py \
--input_type=image_tensor \
--pipeline_config_path={pipeline_fname} \
--output_directory={output_directory} \
--trained_checkpoint_prefix={last_model_path}

Think the error is saying exactly what is happening. You are creating steps with no data so argmax() won't run. Perhaps you need to adjust how it's loaded so data is in steps. Hard to say based on info provided
Simplified Example to Demonstrate Issue:
steps = np.array([1,2,3])
#Works fine with data
print(steps.argmax())
#argmax() throws an error since the array is empty
emptySteps = np.delete(steps,[0,1,2])
print(emptySteps.argmax())
Possible Workaround
Appears you are searching a directory. If there are no files available, you may not want an error. Could achieve this with a simple check before running to see if there are files to process
if steps.size > 0:
print("Do something with argmax()")
else:
print ("No data in steps array")

Related

Scopus Abstract Retrieval - Value and Type Error only when too many entries are parsed

I am trying to retrieve abstracts via Scopus Abstract Retrieval. I have a file with 3590 EIDs.
import pandas as pd
import numpy as np
file = pd.read_excel(r'C:\Users\Amanda\Desktop\Superset.xlsx', sheet_name='Sheet1')
from pybliometrics.scopus import AbstractRetrieval
for i, row in file.iterrows():
q = row['EID']
ab = AbstractRetrieval(q,view='META_ABS')
file.at[i,"Abstract"] = ab.description
print(str(i) + ' ' + ab.description)
print(str(''))
I get a value error -
In response to the value error, I altered the code.
from pybliometrics.scopus import AbstractRetrieval
error_index_valueerror = {}
for i, row in file.iterrows():
q = row['EID']
try:
ab = AbstractRetrieval(q,view='META_ABS')
file.at[i,"Abstract"] = ab.description
print(str(i) + ' ' + ab.description)
print(str(''))
except ValueError:
print(f"{i} Value Error")
error_index_valueerror[i] = row['Title']
continue
When I trialed this code with 10-15 entries, it worked well and I retrieved all the abstracts. However, when I ran the actual file with 3590 EIDs, the output would be a series of 10-12 value errors before a type error ('can only concatenate str (not "NoneType") to str surfaces.
I am not sure how to tackle this problem moving forward. Any advice on this matter would be greatly appreciated!
(Side note: When I change view='FULL' (as recommended by the documentation), I still get the same outcome.)
Without EIDs to check, it is tough to point to the precise cause. However, I'm 99% certain that your problem are missing abstracts in the .description property. It's sufficient when the first call is empty, because it will turn the column type into float, to which you wish to append a string. That's what the error says.
Thus your problem has nothing to do with pybliometrics or Scopus, but with the way you bild the code.
Try this instead:
import pandas as pd
import numpy as np
from pybliometrics.scopus import AbstractRetrieval
def parse_abstract(eid):
"""Retrieve Abstract of a document."""
ab = AbstractRetrieval(q, view='META_ABS')
return ab.description or ab.abstract
FNAME = r'C:\Users\Amanda\Desktop\Superset.xlsx'
df = pd.read_excel(FNAME, sheet_name='Sheet1')
df["abstract"] = df["EID"].apply(parse_abstract)
Instead of appending values one-by-one in a loop, which is slow and error-prone, I use pandas' .apply() methods.
Also note how I write ab.description or ab.abstract. https://pybliometrics.readthedocs.io/en/stable/classes/AbstractRetrieval.html states that both should yield the same but can be empty. With this statement, if ab.description is empty (i.e., falsy), it will use ab.abstract instead.

Kernel dies with strange behavior

I have a simple function exhibiting strange behavior. I already searched for explanations, but couldn't find any.
def myfunc(frame):
lol = []
for i in range(frame.shape[0]):
if frame.iloc[i,3] == 3:
lol.append(frame.iloc[i,7])
return np.asarray(lol,dtype=np.int32)
print('before')
x = myfunc(x)
print('after')
The result of the above code is
before
Kernel died
def myfunc(frame):
lol = []
for i in range(frame.shape[0]):
if frame.iloc[i,3] == 3:
lol.append(frame.iloc[i,7])
print('myfunc')
return np.asarray(lol,dtype=np.int32)
print('before')
x = myfunc(x)
print('after')
how ever, simply adding a single print statement gives
before
myfunc
after
Kernel died
The print statement is the only difference and I've tested this, maybe, 50 times. Disregarding my other problems (Kernel died), I have no idea why this is happening. I would appreciate any insight.
I tried to reproduce the error with the sample data, and it worked fine for me.
You can try the same with your data in colab environment and check, if the error doesn't appear then the issue might be one of the following.
Your data is large and it does not fit into memory, you can try specifying engine='python' while loading csv file df = pd.read_csv('input.csv', engine='python').
You can alternatively split the dataframe into multiple dataframes and merge it later.
You can use Tensorflow's tf.data to load CSV files and to build input pipelines which is more efficient for large files.
Below is the working code with sample data.
import tensorflow as tf
import pandas as pd
import numpy as np
df = pd.read_csv("/content/sample_data/mnist_train_small.csv")
def myfunc(frame):
lol = []
for i in range(frame.shape[0]):
if frame.iloc[i,3] == 3:
lol.append(frame.iloc[i,7])
return np.asarray(lol,dtype=np.int32)
print('before')
x = myfunc(df)
print('after')
Result:
before
after

What is the invalid syntax error in this def?

I am trying to use this Anki add-on written in python.
However, on Anki 2.1 startup, an error is returned.
May anyone have a quick look at the short code in the link and spot the error? Maybe it is incompatible with a recent update of python?
# Anki 2.0 addon
# Author EJS
# https://eshapard.github.io/
#
# Sets the learning steps sequence of each deck options group.
from anki.hooks import addHook
from aqt import mw
#from aqt.utils import showInfo
#import time
ignoreList = ['Default', 'OnHold', 'Parent Category'] #Deck options groups to ignore
# run this on profile load
def updateLearningSteps():
#find all deck option groups
dconf = mw.col.decks.dconf
#cycle through them one by one
for k in dconf:
if dconf[k]["name"] not in ignoreList:
#showInfo(dconf[k]["name"])
ease = dconf[k]["new"]["initialFactor"]/1000.0
#create learning steps
tempList = [15]
for i in range(10):
l = int(1440*ease**i)
if l < 28800:
tempList.append(l)
else:
gradInts = [(l/1440),(l/1440)]
break
#showInfo(str(tempList))
#showInfo(str(gradInts))
mw.col.decks.dconf[k]["new"]["delays"] = tempList
mw.col.decks.dconf[k]["new"]["ints"] = gradInts
mw.col.decks.save(mw.col.decks.dconf[k])
mw.res
et()
# add hook to 'profileLoaded'
addHook("profileLoaded", updateLearningSteps)
This is the error: https://user-images.githubusercontent.com/52420923/66923073-ba2df980-f017-11e9-8b66-c8799db29850.png
The code you've posted doesn't match the error. The error has def updateLearningSteps() without a colon at the end of the line. That is indeed a syntax error.

I want to extract the MAC address of the following python-wifi 0.6.1 code

The following code tries to extract the MAC address of all nearby APs and then saves them in a matrix, i dont know what is wrong. it uses the library python-wifi 0.6.1. Here is the code and error:
`
import errno
import sys
import types
import pythonwifi.flags
from pythonwifi.iwlibs import Wireless, WirelessInfo, Iwrange, getNICnames, getWNICnames
i=0
ArregloMAC=[20][30]
wifi= Wireless('wlan0')
results = wifi.scan()
(num_channels, frequencies) = wifi.getChannelInfo()
print "%-8.16s Scan completed :" % (wifi.ifname, )
for ap in results:
index = 1
ArregloMAC[i][index-1]= str("%d-%s" % (_, ap.bssid))
index = index+1
print ArregloMAC`
IndexError: list index out of range
This line
ArregloMAC=[20][30]
will give you an index out of range straight off. What it says is, create a list of one element, [20], then take the 31st element of that list, and assign it to ArregloMAC. Since the list has only one element you will inevitably get an error.
It looks like you are trying to declare a 2-dimensional array. That is not how Python lists work.

Python: Reading Data from Multiple CSV Files to Lists

I'm using Python 3.5 to move through directories and subdirectories to access csv files and fill arrays with data from those files. The first csv file the code encounters looks like this:
The code I have is below:
import matplotlib.pyplot as plt
import numpy as np
import os, csv, datetime, time, glob
gpheight = []
RH = []
dewpt = []
temp = []
windspd = []
winddir = []
dirpath, dirnames, filenames = next(os.walk('/strm1/serino/DATA'))
count2 = 0
for dirname in dirnames:
if len(dirname) >= 8:
try:
dt = datetime.datetime.strptime(dirname[:8], '%m%d%Y')
csv_folder = os.path.join(dirpath, dirname)
for csv_file2 in glob.glob(os.path.join(csv_folder, 'figs', '0*.csv')):
if os.stat(csv_file2).st_size == 0:
continue
#create new arrays for each case
gpheight.append([])
RH.append([])
temp.append([])
dewpt.append([])
windspd.append([])
winddir.append([])
with open(csv_file2, newline='') as f2_input:
csv_input2 = csv.reader(f2_input,delimiter=' ')
for j,row2 in enumerate(csv_input2):
if j == 0:
continue #skip header row
#fill arrays created above
windspd[count2].append(float(row2[5]))
winddir[count2].append(float(row2[6]))
gpheight[count2].append(float(row2[1]))
RH[count2].append(float(row2[4]))
temp[count2].append(float(row2[2]))
dewpt[count2].append(float(row2[3]))
count2 = count2 + 1
except ValueError as e:
pass
I have it set up to create a new array for each new csv file. However, when I print the third (temperature) column,
for n in range(0,len(temp)):
print(temp[0][n])
it only partially prints that column of data:
-70.949997
-68.149994
-60.449997
-63.649994
-57.449997
-51.049988
-45.349991
-40.249985
-35.549988
-31.249985
-27.149994
-24.549988
-22.149994
-19.449997
-16.349976
-13.25
-11.049988
-8.949982
-6.75
-4.449982
-2.25
-0.049988
In addition, I believe a related problem is that when I simply do,
print(temp)
it prints
with the highlighted section the section that belongs to this one csv file, and should therefore be in one array. There are also additional empty arrays at the end that should not be there.
I have (not shown) a section of code before this that does the same thing but with different csv files, and that works as expected, separating each file's data into a new array, with no empty arrays. I appreciate any help!
The issue had been my use of try and pass. All the files that matched my criteria were met, but some of those files had issues with how their contents were read, which caused the errors I was receiving later in the code. For anyone looking to use try and pass, make sure that you are able to safely pass on any exceptions that block of code may encounter. Otherwise, it could cause problems later. You may still get an error if you don't pass on it, but that will force you to fix it appropriately instead of ignoring it.

Categories

Resources