Arcpy.da.UpdateCursor SQL error using Arcmap 10.1 or higher - arcpy

I am attempting to update the column "DR1cond" (which means driver 1 condition).
Currently, the driver conditions states "drinking" or "sleeping" or "manyotherconditions." I want to update get row so "drinking" = 1 or "sleeping" = 2.
But I am getting an sql error: Here is what I have so far:
#Update Cursor to replace current string data with numbers
import arcpy
arcpy.env.workspace = r'C:\Crash\Fatal03-12.shp'
updateFC = 'DR1cond'
print 'Creating update cursor for',updateFC,' in ',arcpy.env.workspace
updateRows = arcpy.da.UpdateCursor(updateFC,["DR1cond"])
#Loop through each Driver conidition and determine replace
print 'Updating DR1cond class field for get row..."
for row in updateRows:
ShapeIndex = row[0]
if(shapeIndex = 'Drinking'):
DR1cond = "1"
elif(shapeIndex = 'fatigue' or 'Asleep'):
DR1cond = "2"
elif(shapeIndex = 'Normal'):
DR1cond = "3"
elif(shapeIndex = 'Drug use'):
DR1cond = "4"
elif(shapeIndex = 'Sick'):
DR1cond = "5"
elif(shapeIndex = 'Cell'):
DR1cond = "6"
elif(shapeIndex = 'Distracted'):
DR1cond = "7"
else(shapeIndex = 'Unknown'):
DR1cond = "99"
row[1] = DR1cond
updateRows.updateRow(row)
print'All done updating'
del updateRows
del row

Use 2 '=' for sure.
Include your new code field in the list of fields when you create your cursor, the cursor won't find row[1].
And, if the code field is short or other numeric type (that would make sense), drop the '"'.

Related

Error in coxModelFrame.coxph(object) : invalid object set x=TRUE in the call to coxph

The following example is for anyone who is building a Cox Proportional Hazards models and trying to produce prediction error curves, but get an error stating:
Error in coxModelFrame.coxph(object) : invalid object
set x=TRUE in the call to coxph.
Here is the code to reproduce the error:
LIBRARIES
library(survival)
library(survminer)
library(pec)
library(Hmisc)
library(rms)
library(riskRegression)
#install.packages("doMC", repos="http://R-Forge.R-project.org")
library(doMC)
The Data
#Load and store the data
lcOrig <- read.csv("cancer.csv")
#Replace all the 1's with 0's (censored)
lcOrig$status <- gsub(pattern = "1", replacement = "0", x = lcOrig$status, fixed = TRUE)
#Replace all the 2's with 1's (death)
lcOrig$status <- gsub (pattern = "2", replacement = "1", x = lcOrig$status, fixed = TRUE)
#Do the same thing for sex (0 = Males, 1 = Females)
lcOrig$sex <- gsub(pattern = "1", replacement = "0", x = lcOrig$sex, fixed = TRUE)
lcOrig$sex <- gsub(pattern = "2", replacement = "1", x = lcOrig$sex, fixed = TRUE)
#Change the class of these variables to integer.
lcOrig$status <- as.integer(lcOrig$status)
lcOrig$sex <- as.integer(lcOrig$sex)
lcOrig$ph.ecog <- as.integer(lcOrig$ph.ecog)
#Remove missing values and column with over 20% missing data.
apply(lcOrig, 2, function(x) sum(is.na(x))/length(x))
lcOrig <- lcOrig[, c(1:9, 11)]
lc <- lcOrig[complete.cases(lcOrig), ]
Cox Proportional Hazards
fitform1 <- Surv(time, status) ~ inst + age + sex + ph.ecog + ph.karno + pat.karno + wt.loss
cox1 <- coxph(fitform1, data = lc)
PREDICTION ERROR CURVES
extends <- function(...) TRUE
library("doMC")
registerDoMC()
set.seed(0692)
fitpec1 <- pec(list("CPH" = cox1), data = lc, formula = fitform1, splitMethod = "cv10", B = 5, keep.index = TRUE, keep.matrix = TRUE)
The last line of code results in the following error:
Error in coxModelFrame.coxph(object) : invalid object
set x=TRUE in the call to coxph
SOLUTION
Change:
cox1 <- coxph(fitform1, data = lc)
To:
cox1 <- coxph(fitform1, data = lc, x = TRUE)
This did not use to be a requirement 2 years ago, but is now. I hope this helps save you some time!

How to create a select clause with AND query that matches tx_news items with two or more assotiated categories

I try to create a query to get all news items, that are flagged with at least two different categories and they have to match the AND clause.
I need this query to make a decision, if the following code should be rendered, or not. E.g. If there is no news item with category A and category B, do nothing. Else show tx_news LIST view.
lib.field_dmnewsplugin.5 = CONTENT
lib.field_dmnewsplugin.5 {
table = tx_news_domain_model_news
select {
pidInList = 124
max = 9
orderBy = uid DESC
leftjoin = sys_category_record_mm ON (sys_category_record_mm.uid_foreign = tx_news_domain_model_news.uid)
#andWhere = sys_category_record_mm.uid_local IN (14,16)
#where = sys_category_record_mm.uid_local = 14
andWhere = sys_category_record_mm.uid_local = 14 AND sys_category_record_mm.uid_local = 16
}
renderObj = COA
renderObj {
1 = TEXT
1.value = Aktuelles
1.wrap = <h2>|</h2>
2 = TEXT
2.field = title
2.crop = 50|...|1
2.wrap = <h3>|</h3>
3 = TEXT
3.field = teaser
3.crop = 500|...|1
3.wrap = <p>|</p>
}
}
My code is the result of some testings. With the "andWhere" clause, the result is empty. without any where clause, I get double entries for all news items, because all of them have at least two different categories.
My goal is to get unique results for each news item, that is flagged with category A and category B (and maybe as an universal solution additional categories).
What do I have to do?
Thank you in advance,
Ralf
Try to put the WHERE clause into the ON part of the JOIN and use a groupBy to get a counter.
select {
selectFields = count(*) AS counter
leftjoin = sys_category_record_mm ON (sys_category_record_mm.uid_foreign = tx_news_domain_model_news.uid) AND sys_category_record_mm.uid_local IN (14,16)
pidInList = 124
max = 9
groupBy = uid
orderBy = uid DESC
where = counter > 1
}
After I had to realize, that Jo's solution does not work for me, I had another idea:
lib.field_dmnewsplugin = COA
lib.field_dmnewsplugin {
10 = CONTENT
10 {
table = tx_news_domain_model_news
select {
selectFields = title, teaser, count(uid) AS counter
leftjoin = sys_category_record_mm ON (sys_category_record_mm.uid_foreign = tx_news_domain_model_news.uid) AND sys_category_record_mm.uid_local IN ({14,###maincat###)
pidInList = 124
max = 1
groupBy = uid
orderBy = counter DESC, crdate DESC
#where = counter > 1
markers {
maincat.value = 16
}
}
renderObj = COA
renderObj {
10 = COA
10 {
stdWrap {
if {
value = 1
isGreaterThan.data = field:counter
#equals.data = field:counter
}
required = 1
wrap = <h2>Some Headline</h2>
}
10 = USER
10 {
userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
extensionName = News
pluginName = Pi1
vendorName = GeorgRinger
switchableControllerActions {
News {
1 = list
}
}
settings < plugin.tx_news.settings
settings {
cropMaxCharacters = 164 | [...] | 1
categoryConjunction = and
categories = 14,16
excludeAlreadyDisplayedNews = 1
archiveRestriction = active
[...]
The problem is, that we cannot use the alias "counter" in the where clause and I have no idea, how I can solve the problem with typoscript. With native SQL there might be a better way.
But I'm able to get the value of "counter" to create an "if" rule. And additionally, I can sort the query by "counter". So, if the query returns at least one hit with "counter" greater than 1, I can decide to render a COA-Object like a news list view with headline.
I'm satisfied with this solution. But maybe, somebody has a special trick for me?
Thank's for your help,
Ralf

arguments imply differing number of rows : excel sheet

I have a DF "petit.col" from an excel sheet, with subjects in row and arguments in column.
I use the survfit package
surv.petitcol<-survfit(formula = Surv(delai_recidive,recidive) ~ figo_initiale_tot, data = petitcol)
but when I run the survplot :
ggsurvplot(
surv.petitcol,
pval = TRUE,
conf.int = FALSE,
conf.int.style = "ribbon",
xlab = "Time in month",
break.time.by = 12,
ggtheme = theme_light(),
risk.table = "abs_pct",
risk.table.y.text.col = T,
risk.table.y.text = FALSE,
ncensor.plot = FALSE,
surv.median.line = "hv",
legend.labs =
c("Ia1","Ia2","Ib1"),
palette =
c("#E7B800", "#2E9FDF","green"),
ylab = "Disease-free survival",
xlim=c(0,100))
I get this error message :
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 58, 0, 174
It looks like it might be an NAs-linked error but I do not see why since NAs were introduced automaticaly. Thanks and tell me what details you may need.

Odoo 8 what is best way to create records with reference to each other

After Ive created a method for importing external product categories into my odoo db, I had observed, that the method create of orm model needs a lot of time, when I loop for the reference to each other like this:
#api.multi
def action_confirm_site(self):
account = self
ebay_site = self.env['ebay.sites'].browse(account.site.id)
call_data = dict(LevelLimit=3, DetailLevel='ReturnAll', CategorySiteID=ebay_site.ebay_id)
error_msg = 'Cant get the Site Categories for site %s' % account.site
reply = self.call(account, 'GetCategories', call_data, error_msg).response.reply
cat_model = self.env['product.category']
top_parent = cat_model.create({'name': account.site.name, 'type': 'ebay',
'ebay_cat_id': account.site.ebay_id}).id
ebay_categories = reply.CategoryArray.Category
cats = []
for category in ebay_categories:
cat = dict(
CategoryID=category.CategoryID,
CategoryLevel=category.CategoryLevel,
CategoryName=category.CategoryName,
CategoryParentID=category.CategoryParentID
)
cats.append(cat.copy())
cats.sort(key=lambda x: x['CategoryLevel'])
id_map = {}
for cat in cats:
# parent_id is either the DB id of the parent, or the ROOT_ID
if cat['CategoryParentID'] in id_map:
parent_id = id_map[cat['CategoryParentID']]
elif cat['CategoryParentID'] == cat['CategoryID']:
parent_id = top_parent
else:
assert False, "This should not happen if the raw data is consistent"
record = cat_model.search([('ebay_cat_id', 'in', [cat['CategoryID']])]).id
if record:
id_map[cat['CategoryID']] = record
continue
record = cat_model.create({'name': cat['CategoryName'], 'type': 'ebay', 'parent_id': parent_id,
'ebay_cat_id': cat['CategoryID']}).id
id_map[cat['CategoryID']] = record
return
That takes a lot of time and in that case of grabbing 6 Level of Ebay categories with 16651 categories it isnt usable.
Ive read that for the import method from frontend it is possible to pass external references.
In my case, external references like category.CategoryParentID references to its parent with category.CategoryID.
How can I change my method to create records in one loop like:
for category in ebay_categories
cat_model.create({'id': (reference[category.CategoryID]),'name': cat['CategoryName'], 'type': 'ebay', 'parent_id': (reference[category.CategoryParentID]), 'ebay_cat_id': category.CategoryID})
One likely culprit is the recomputation of the parent_left and parent_right fields. You could try disabling it by using a context with 'defer_parent_store_computation': True and then calling self._parent_store_compute(cr) when all the categories have been computed.

getting distinct users from the resultset and string concat on other fields

I have a Sql ResultSet from a query. It has following fields: username, department, ..responsibility.
There are repeated username with different values for deptmt and responsblty. I want to get a map/list of unique usernames with a list of departments and responsibilities for each user in the resultset
So if
resultset = (Sam, dept1,.. resp1),
(Tom, dept1,..resp3),
(Sam,dept2,..resp2),
(Tom,dept3,..resp3)...etc
the output should be
finalList = [["Sam", "dept1,dept2", "resp1,resp2"],["Tom", "dept1,dept3", "resp3"]]
Things to note: 1) There may be n columns which are variable (column list read from a property file) so the code to be adaptable. _ not reqired for now, its a fixed list right now
2)All values enclosed in double quotes and seperated by comma.
3) For tom responsibility contains only resp3 since its repeated...i.e unique values in the list.
How do I convert the resultset?
Thanks
A shorter alternative to Bill's solution would be:
def data = [ ['Sam', 'dept1','resp1'],
['Tom', 'dept1','resp3'],
['Sam', 'dept2','resp2'],
['Tom', 'dept2','resp4'] ]
def output = data.groupBy { it[0] }.collect { name, vals ->
$/"$name", "${vals*.getAt( 1 ).join(',')}", "${vals*.getAt( 2 ).join(',')}"/$
}
output.each {
println it
}
Which prints:
"Sam", "dept1,dept2", "resp1,resp2"
"Tom", "dept1,dept2", "resp3,resp4"
This should get you pretty close. Note that the final output doesn't have the quotes in it (Groovy doesn't print those for strings). Looking at the code, you can see that the "output" is a Collection of Lists, each with 3 items: the name, the comma-sep list of department strings, then the comma-sep list of responsibility strings.
BTW, you may want to use something other than comma in the inner-strings, if you want to create a comma-sep list as the final output.
def data = [ ['Sam', 'dept1','resp1'],
['Tom', 'dept1','resp3'],
['Sam', 'dept2','resp2'],
['Tom', 'dept2','resp4'] ]
def tmpmap = [:]
data.each { inputrow ->
def name = inputrow[0]
def curlist = tmpmap[name]
if ( curlist == null ) { // first time seeing this name
curlist = [name,inputrow[1],inputrow[2]]
tmpmap.put( name, curlist );
} else { // seen this name before
curlist[1] += ',' + inputrow[1]
curlist[2] += ',' + inputrow[2]
}
}
output = tmpmap.values()
println output

Resources