I get this error
Traceback (most recent call last):
File "C:\Users\User1\Desktop\cellh5_scripts\ewa_pnas_fate.py", line 90, in <module>
ec.combine_classifiers("Event labels combined")
File "C:\Users\User1\Desktop\cellh5_scripts\ewa_pnas_fate.py", line 53, in combine_classifiers
pnas_class[pnas_class==3] = 1
TypeError: 'numpy.int32' object does not support item assignment
by runing the code
def combine_classifiers(self, output_name):
all_combined_classes = []
for _, (plate_name, w, p, t1, t2, track_ids, track_labels) in self.mapping[['Plate',
'Well',
'Site',
'Gene Symbol',
'siRNA ID',
'Event track ids',
'Event track labels']].iterrows():
combined_classes = []
ch5_file_handle = self.cellh5_handles[plate_name]
ch5_pos = ch5_file_handle.get_position(w, str(p))
for track_id, track_label in zip(track_ids, track_labels):
h2b_class = track_label.copy()
print(track_id)
pnas_class = ch5_pos.get_class_prediction('secondary__expanded')[track_id]['label_idx'] + 1
print(pnas_class)
inter_idx = h2b_class == 1
pnas_class[pnas_class==3] = 1
pnas_class[pnas_class==2]+=2
combined_class = h2b_class
combined_class[inter_idx] = pnas_class[inter_idx]
combined_classes.append(combined_class)
all_combined_classes.append(combined_classes)
self.mapping[output_name] = pandas.Series(all_combined_classes)
I print pnas_class which is 1, and track_id which is 50708. I'm wondering what the designer of code want to do in the part:
inter_idx = h2b_class == 1
pnas_class[pnas_class==3] = 1
pnas_class[pnas_class==2]+=2
combined_class = h2b_class
combined_class[inter_idx] = pnas_class[inter_idx]
How can I change that to have the same meaning?
pnas_class is a an integer so you can't select item from an integer by [pnas_class==3] = 1.
Maybe you are trying to affect 1 to pnas_class if it's equal to 3. In this case try this:
pnas_class= 1*(pnas_class == 3) + pnas_class*(pnas_class != 3 )
Ok I found the mistake. You arer right the pnas_class should not be an integer and I know why is it integer instead of array.
Related
I am defining a main_meal_model function to list a lunch plan that takes in a day value. I have previously defined a random_main_meal_dataset function that creates a database of all the foods that are to be eaten at lunch which also takes in the same day value. For some reason i can't omit a forced declaration of the day value in the random_main_meal_dataset function, else it either gives a KeyError or NameError
I have tried every day of the week and it seems perfect as much as my forced declaration is the same as the day value i send when calling the main_meal_model function but as soon as i try to make this an automatic correspondance it sends KeyError: 'Monday' or NameError: name 'day' is not defined per day_data = data[day]
Error messages:
Full Errorr messages:
Traceback (most recent call last):
File "c:\Users\Leonix\Desktop\CS50 Final Project\test.py", line 104, in <module>
print(main_meal_model('Monday', 70, 2000, data, 'Lunch'))
File "c:\Users\Leonix\Desktop\CS50 Final Project\test.py", line 72, in main_meal_model
day_data = data[day]
KeyError: 'Monday
or
Traceback (most recent call last):
File "c:\Users\Leonix\Desktop\CS50 Final Project\test.py", line 103, in <module>
print(main_meal_model('Monday', 70, 2000, data, 'Lunch')) File "c:\Users\Leonix\Desktop\CS50 Final Project\test.py", line 71, in main_meal_model
day_data = data[day] NameError: name 'day' is not defined
Here is the part of the code I suppose is causing the problem
https://pastebin.com/w8XQ8rTn
split_values_day = np.linspace(0, len(data), 8).astype(int)
split_values_day[-1] = split_values_day[-1]-1
def random_main_meal_dataset(data, day):
data = data[data['meal'].str.contains('Main Dishes|Condiments|Side Dishes', na=False)]
frac_data = data.sample(frac=1).reset_index().drop('index', axis=1)
day_data = []
for s in range(len(split_values_day)-1):
day_data.append(
frac_data.loc[split_values_day[s]:split_values_day[s+1]])
return dict(zip(day, day_data))
# define a lunch / dinner model that takes in prob, kg, calories, data and makes a lunch / dinner plan for the day
def main_meal_model(day, kg, calories, data, meal):
data = random_main_meal_dataset(data, day=['Monday'])
G = extract_gram(build_nutritional_values(kg, calories))
E = G['Carbohydrates Grams']
F = G['Fat Grams']
P = G['Protein Grams']
day_data = data[day]
day_data = day_data[day_data.calories != 0]
food = day_data.name.tolist()
c = day_data.calories.tolist()
x = pulp.LpVariable.dicts(
"x", indices=food, lowBound=0, upBound=1.5, cat='Continuous', indexStart=[])
e = day_data.carbohydrate.tolist()
f = day_data.total_fat.tolist()
p = day_data.protein.tolist()
div_meal = meal_split[meal]
prob = pulp.LpProblem("Diet", LpMinimize)
prob += pulp.lpSum([x[food[i]]*c[i] for i in range(len(food))])
prob += pulp.lpSum([x[food[i]]*e[i] for i in range(len(x))]) >= E*0.35
prob += pulp.lpSum([x[food[i]]*f[i] for i in range(len(x))]) >= F*0.35
prob += pulp.lpSum([x[food[i]]*p[i] for i in range(len(x))]) >= P*0.35
prob.solve(PULP_CBC_CMD(msg=0))
variables = []
values = []
for v in prob.variables():
variable = v.name
value = v.varValue
variables.append(variable)
values.append(value)
values = np.array(values).round(2).astype(float)
sol = pd.DataFrame(np.array([food, values]).T,
columns=['Food', 'Quantity'])
sol['Quantity'] = sol.Quantity.astype(float)
sol = sol[sol['Quantity'] != 0.0]
sol.Quantity = sol.Quantity*100
sol = sol.rename(columns={'Quantity': 'Quantity (g)'})
return sol
print(main_meal_model('Monday', 70, 2000, data, 'Lunch'))`
I am a newbie in programming. I am writing a Python script that extracts data from a pdf. I am having trouble with tuple. I am not able to provide its argument. I think it is my logic that is not correct including indentation, sequence, or something else.
I am hoping to get some explanation on why I am getting the error.
This is the sample of my PDF (i have to block some sensitive info)
This is what I am trying to achieve
I am getting this error:
Traceback (most recent call last):
File "/Users/jeff/PycharmProjects/extractFreightInvoice/main.py", line 79, in <module>
lines.append(Line('invDate, invNumber, poNumber, contactName, jobNumber, '
TypeError: <lambda>() missing 11 required positional arguments: 'invNumber', 'poNumber', 'contactName', 'jobNumber', 'jobName', 'invDescription', 'siteAddress', 'invItemsDesc', 'invItemsQty', 'invItemsUnitPrice', and 'invItemsAmount'
My code is as per below:
# This is a pdf extractor
import re
import pdfplumber
import pandas as pd
from collections import namedtuple
Line = namedtuple('Line', 'invDate, invNumber, poNumber, contactName, jobNumber, '
'jobName, invDescription, siteAddress, invItemsDesc, invItemsQty, invItemsUnitPrice, '
'invItemsAmount ')
invDate_re = re.compile(r'(Clever Core NZ Limited\s)(\d{1,2}/\d{1,2}/\d{4})(.+)')
invNumber_re = re.compile(r'(IN\d{6})')
poNumber_re = re.compile(r'\d{4}')
contactNameBen_re = re.compile(r'(Jordan\s.+)')
contactNameCraig_re = re.compile(r'(Lorna\s.+)')
jobNumber_re = re.compile(r'(J[\d]{6})')
jobName_re = re.compile(r'(Job Name)')
invDescription_re = re.compile(r'(Invoice Description)')
siteAddress_re = re.compile(r'(Site address.*)')
colHeading_re = re.compile(r'((Description)(.* Quantity.* Unit Price.*))')
invItems_re = re.compile(
r'(.+) (([0-9]*[.])?[0-9]+) (([0-9]*[.])?[0-9]+) (\d*\?\d+|\d{1,3}(,\d{3})*(\.\d+)?)')
# quoteLines_re = re.compile(r'(.+)(:\s*)(.+)')
# clevercorePriceLine_re = re.compile(r'(.* First .*\s?)(-\s?.*\$)(\s*)(.+)')
file = 'CombinedInvoicePdf.pdf'
lines = []
with pdfplumber.open(file) as myPdf:
for page in myPdf.pages:
text = page.extract_text()
lines = text.split('\n')
index = 0
for i in range(len(lines)):
line = lines[i]
invDateLine = invDate_re.search(line)
invNumberLine = invNumber_re.search(line)
poNumberLine = poNumber_re.search(line)
contactNameJordanLine = contactNameJordan_re.search(line)
contactNameLornaLine = contactNameLorna_re.search(line)
jobNumberLine = jobNumber_re.search(line)
jobNameLine = jobName_re.search(line)
invDescriptionLine = invDescription_re.search(line)
colHeadingLine = colHeading_re.search(line)
siteAddressLine = siteAddress_re.search(line)
invItemsLine = invItems_re.search(line)
if invDateLine:
invDate = invDateLine.group(2)
if invNumberLine:
invNumber = invNumberLine.group(1)
if poNumberLine and len(line) == 4:
poNumber = poNumberLine.group(0)
if contactNameBenLine:
contactName = 'Jordan Michael'
if contactNameCraigLine:
contactName = 'Lorna Tolentin'
if jobNumberLine:
jobNumber = lines[i]
if jobNameLine:
jobName = (lines[i + 1])
if invDescriptionLine:
invDescription = lines[i + 1]
if siteAddressLine:
if len(lines[i + 1]) > 0 and len(lines[i + 1]) == 0:
siteAddress = lines[i + 1]
elif len(lines[i + 1]) > 0 and len(lines[i + 1]) > 0:
siteAddress = lines[i + 1] + ' ' + lines[i + 2]
else:
siteAddress = 'check invoice'
if invItemsLine and invItemsLine[2] != '06':
invItemsDesc = invItemsLine.group(1)
invItemsQty = invItemsLine.group(2)
invItemsUnitPrice = invItemsLine.group(4)
invItemsAmount = invItemsLine.group(6)
lines.append(Line('invDate, invNumber, poNumber, contactName, jobNumber, '
'jobName, invDescription, siteAddress, invItemsDesc, invItemsQty, invItemsUnitPrice, '
'inItemsAmount'))
df = pd.DataFrame(lines)
print(df)
print(df.head())
df.to_csv('freightCharges.csv')
Line is a tuple subclass with parameters and fields
You need to fill them with separate parameters, not a single string
lines.append(Line('invDate', 'invNumber', 'poNumber', 'contactName', 'jobNumber', 'jobName', 'invDescription',
'siteAddress', 'invItemsDesc', 'invItemsQty', 'invItemsUnitPrice', 'inItemsAmount'))
I'm trying to get PMI count from work part file which is opened in NX software in Python, but getting error:
TypeError: object of type 'NXOpen.Annotations.PmiCollection' has no len()
Code:
lw = theSession.ListingWindow
lw.Open()
theSession = NXOpen.Session.GetSession()
theParts = theSession.Parts
theWorkPart = theParts.Work
allPMIObjects = theWorkPart.PmiManager.Pmis
count1 = len(allPMIObjects)
lw.WriteLine(count1)
lw.Close()
Documentation link: https://docs.plm.automation.siemens.com/data_services/resources/nx/11/nx_api/custom/en_US/nxopen_python_ref/NXOpen.Annotations.PmiCollection.html
Equivalent vb code: http://nxjournaling.com/content/find-out-if-part-has-any-pmi
Below is the code that gives count of PMIs from active part and assert if count is changed (here 3):
lw = theSession.ListingWindow
lw.Open()
# Custom code starts to get PMI count in part and check
theSession = NXOpen.Session.GetSession()
theParts = theSession.Parts
theWorkPart = theParts.Work
allPMIObjects = theWorkPart.PmiManager.Pmis
i = 0
for p in allPMIObjects:
i = i + 1
lw.WriteLine(str(i))
lw.Close()
#if PMI count is changed from 3, raise AssertionError:
assert i == 3
My scirpt :
if 'recheck' == msg.lower():
with open('Output.txt','r') as rr:
contactArr = rr.readlines()
for v in xrange(len(contactArr) -1,0,-1):
num = re.sub(r'\n', "", contactArr[v])
contacts.append(num)
pass
contacts = list(set(contacts))
for z in range(len(contacts)):
arg = contacts[z].split('|')
if arg[1] == receiver.id :
userList.append(arg[0])
timelist.append(arg[2])
uL = list(set(userList))
# print uL
for ll in range(len(uL)):
try:
getIndexUser = userList.index(uL[ll])
timeSeen.append(strftime("%H:%M:%S", localtime(int(timelist[getIndexUser]) / 1000)))
recheckData.append(userList[getIndexUser])
except IndexError:
conName.append('nones')
pass
contactId = client._getContacts(recheckData)
for v in range(len(recheckData)):
dataResult.append(contactId[v].displayName + '['+timeSeen[v]+']')
pass
# # print len(recheckData)
tukang = "V=ON Members=V\n[*]"
grp = '\n[*] '.join(str(f) for f in dataResult)
receiver.sendMessage("%s %s" % (tukang, grp))
But error in terminal :
Traceback (most recent call last):
File "echobot.py", line 117, in <module>
if arg[1] == receiver.id :
IndexError: list index out of range
Can you help me?
The error is coming from
arg = contacts[z].split('|')
if arg[1] == receiver.id :
userList.append(arg[0])
timelist.append(arg[2])
you should double check that your contacts are all formatted correctly.
According to this code, each contact should be formatted like
user|id|time
If each contact in contacts is not formatted exactly that way, this error will be thrown. It looks like your split('|') fucntion isn't finding any '|' to split on.
I`ve obtain this error
File "/class.py", line 246, in __init__
if d and self.rf == 2 and d["descriptionType"] in ["900000000000003001"] and d["conceptId"] in konZer.zerrenda:
TypeError: 'Desk' object is not subscriptable
I created this object
class Desk:
descriptionId = ""
descriptionStatus = ""
conceptId = ""
term = ""
And I called it in another class
class DescriptionList():
def deskJ(self,line):
er = line.strip().split('\t')
desc = Desk()
if er[1] == "0":
desc.descriptionId = er[0]
desc.descriptionStatus = er[1]
desc.conceptId = er[2]
desc.term = er[3]
return description
Then I called the function "deskJ" at init and I get the error at this part (I've deleted some parts of the function):
def __init__(self,fitx,konZer,lanZer={}):
with codecs.open(fitx,encoding='utf-8') as fitx:
lines = fitx.read().split('\n')[1:-1]
for line in lines:
d = self.deskJ(line)
if d and self.rf == 2 and d["descriptionType"] in ["900000000000003001"] and d["conceptId"] in konZer.zerrenda:
c = konZer.zerrenda[d["conceptId"]]
c["fullySpecifiedName"] = d["term"]
What am I doing wrong?
Using d["descriptionType"] is trying to access d with the key "descriptionType". That doesn't work, though, because d is a Desk object that doesn't have keys. Instead, get the attributes:
if d and self.rf == 2 and d.descriptionType in ["900000000000003001"] and d.conceptId in konZer.zerrenda: