[GRASS-SVN] r46479 - in grass-addons/raster/mcda: r.in.drsa
r.mcda.regime r.mcda.roughset r.roughset
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue May 31 17:38:07 EDT 2011
Author: gianluca
Date: 2011-05-31 14:38:07 -0700 (Tue, 31 May 2011)
New Revision: 46479
Modified:
grass-addons/raster/mcda/r.in.drsa/r.in.drsa.py
grass-addons/raster/mcda/r.mcda.regime/regime.c
grass-addons/raster/mcda/r.mcda.roughset/r.mcda.roughset.py
grass-addons/raster/mcda/r.roughset/main.c
Log:
Removed some bugs. Build vector output
Modified: grass-addons/raster/mcda/r.in.drsa/r.in.drsa.py
===================================================================
--- grass-addons/raster/mcda/r.in.drsa/r.in.drsa.py 2011-05-31 20:20:21 UTC (rev 46478)
+++ grass-addons/raster/mcda/r.in.drsa/r.in.drsa.py 2011-05-31 21:38:07 UTC (rev 46479)
@@ -70,10 +70,19 @@
'decision':string.join(decision),
'support':int(row[-2].strip('[.,;]')),
'strength':row[-1].strip('[,:]')}
- rules_list.append(rule)
-
+ rules_list.append(rule)
+
+ rls = open('rules',"w")
+ for l in rules_list:
+ factor=l['decision'].strip('()').split(' ')
+ rls.write('%s=%s %s \n' % (l['id_rule'],l['id_rule'], " ".join(factor)))
+ rls.close
return rules_list
+def parser_JAMM_rule(tags):
+ "parser file *.rls from JAMM software and extract information for make new classified raster"
+
+
def parser_mapcalc(rules,i):
"parser to build a formula to be included in mapcalc command"
mapalgebra="if("
@@ -100,7 +109,7 @@
def main():
input_rules = options['input']
- output_map = options['output']
+ outputMap = options['output']
gregion = grass.region()
nrows = gregion['rows']
@@ -112,15 +121,31 @@
tags=input_rules.readlines()
rules=[] #single rule (dictionary) in array
+ maps=[]
rules=parser_4eMka2_rule(tags)
## clean_rules(rules)
for i in range(len(rules)):
mappa="rule"+rules[i]['id_rule']
formula=parser_mapcalc(rules,i)
- print mappa +"="+ formula
grass.mapcalc(mappa +"=" +formula)
-
+ maps.append(mappa)
+ mapstring=",".join(maps)
+
+ for m in maps:
+ grass.run_command("r.to.vect", overwrite='True', flags='s', input=m, output=m, feature='area')
+ grass.run_command("v.db.addcol", map=m, columns='rule varchar(25)')
+ grass.run_command("v.db.update", map=m, column='rule', value=m)
+ grass.run_command("v.patch", overwrite='True', flags='e', input=mapstring, output=outputMap)
+
+ grass.run_command("r.patch", input=maps, flags='-o', output='rulesMap')
+ grass.run_command("r.reclass", input='rulesMap', flags='-o', output='roughMAP', rules='rules')
+ grass.run_command("r.to.vect", input='roughMAP', flags='-o', output=outputMap, feature='area')
+
+ grass.run_command("g.remove", rast=mapstring)
+ grass.run_command("g.remove", vect=mapstring)
+ grass.run_command("g.remove", vect="rulesMap,roughMAP")
+
input_rules.close()
if __name__ == "__main__":
Modified: grass-addons/raster/mcda/r.mcda.regime/regime.c
===================================================================
--- grass-addons/raster/mcda/r.mcda.regime/regime.c 2011-05-31 20:20:21 UTC (rev 46478)
+++ grass-addons/raster/mcda/r.mcda.regime/regime.c 2011-05-31 21:38:07 UTC (rev 46479)
@@ -66,9 +66,7 @@
for (i = 0; i < ncriteria; i++)
{
- double d =
- decision_vol[row1][col1][i] -
- decision_vol[row2][col2][i];
+ double d = decision_vol[row1][col1][i]-decision_vol[row2][col2][i];
if (d > 0)
reg += (1 * weight_vect[i]);
else if (d < 0)
Modified: grass-addons/raster/mcda/r.mcda.roughset/r.mcda.roughset.py
===================================================================
--- grass-addons/raster/mcda/r.mcda.roughset/r.mcda.roughset.py 2011-05-31 20:20:21 UTC (rev 46478)
+++ grass-addons/raster/mcda/r.mcda.roughset/r.mcda.roughset.py 2011-05-31 21:38:07 UTC (rev 46479)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python
############################################################################
#
# MODULE: r.mcda.roughset
@@ -13,7 +13,7 @@
#############################################################################
#%Module
-#% description: Generate a MCDA map from several criteria maps using Dominance Rough Set Approach (**)
+#% description: Generate a MCDA map from several criteria maps using Dominance Rough Set Approach.
#% keywords: raster, Dominance Rough Set Approach
#% keywords: Multi Criteria Decision Analysis (MCDA)
#%End
@@ -94,7 +94,7 @@
examples=[]
MATRIX=[]
for i in range(len(attributes)):
- grass.mapcalc("rast=if(isnull(${decision})!=0,${attribute},null())",
+ grass.mapcalc("rast=if(isnull(${decision})==0,${attribute},null())",
rast="rast",
decision=decision,
attribute=attributes[i])
@@ -112,7 +112,8 @@
for r in range(len(MATRIX)):
for c in range(len(MATRIX[0])):
- outf.write("%s " % round(float(MATRIX[r][c]), 2))
+ outf.write("%s " % (MATRIX[r][c]))
+# outf.write("%s " % round(float(MATRIX[r][c]), 2))
outf.write("\n")
outf.write("**END")
@@ -373,18 +374,6 @@
return best
-
-def CheckMinimalCondition (rules,B,matrix):
- "Check minimal elementary condition from each rule"
- obj_cov_by_rules=[]
- if len(rules)>1:
- for e in rules:
- check=copy.deepcopy(rules)
- check.remove(e)
- obj_cov_by_rules=FindObjectCovered (rules, matrix)
- if set(obj_cov_by_rules).issubset(set(B)):
- rules=check
- return rules
def Type_one_rule (c, e, preference, matrix):
elem={'criterion':c,'condition':e, 'sign':preference[c-1],'class':'', \
@@ -520,66 +509,78 @@
category=[]
maps=[]
stringa=[]
+ out=outputMap
+
for R in RULES:
formula="if("
for e in R[:-1]: #build a mapcalc formula
formula+= "(%s %s %.4f ) && " % (e['label'], e['sign'] , e['condition'] )
formula+= "(%s %s %.4f ),%d,null())" % (R[-1]['label'],R[-1]['sign'], R[-1]['condition'] ,i )
- mappa="%d.%s_%d" % ( i, R[0]['type'], R[0]['class'] ) #build map name for mapcalc output
+ mappa="r%d_%s_%d" % ( i, R[0]['type'], R[0]['class'] ) #build map name for mapcalc output
category.append({'id':i, 'type': R[0]['type'], 'class':R[0]['class']}) #extract category name
maps.append(mappa) #extract maps name
grass.mapcalc(mappa +"=" +formula)
i+=1
- maps=",".join(maps)
- grass.run_command("r.patch", input=maps, flags='-o', output=outputMap)
-# grass.run_command("g.remove", rast=maps)
- rls = file("rules","w")
- for i in category:
- rls.write("%d:%s %s\n" % (i['id'], i['type'], str(i['class'])))
- rls.close
- grass.run_command("r.category", map='classify', rules="rules" )
+ mapstring=",".join(maps)
+
+ print maps
+
+ for m in maps:
+ grass.run_command("r.to.vect", overwrite='True', flags='s', input=m, output=m, feature='area')
+ grass.run_command("v.db.addcol", map=m, columns='rule varchar(25)')
+ grass.run_command("v.db.update", map=m, column='rule', value=m)
+ grass.run_command("v.patch", overwrite='True', flags='e', input=mapstring, output=outputMap)
+
+ grass.run_command("g.remove", rast=mapstring)
+ grass.run_command("g.remove", vect=mapstring)
+
+
return 0
+
def main():
"main function for DOMLEM algorithm"
- start=time()
- attributes = options['criteria'].split(',')
- preferences=options['preferences'].split(',')
- decision=options['decision']
- outputMap= options['outputMap']
- outputTxt= options['outputTxt']
- out=BuildFileISF(attributes, preferences, decision, outputMap, outputTxt)
-
- infosystem=FileToInfoSystem(out)
-
- UnionOfClasses(infosystem)
- DownwardUnionClass=DownwardUnionsOfClasses(infosystem)
- UpwardUnionClass=UpwardUnionsOfClasses(infosystem)
- Dominating=DominatingSet(infosystem)
- Dominated=DominatedSet(infosystem)
-## upward union class
- print "elaborate upward union"
- Lu=LowerApproximation(UpwardUnionClass, Dominating) #lower approximation of upward union for type 1 rules
- Uu=UpperApproximation(UpwardUnionClass,Dominated ) #upper approximation of upward union
- UpwardBoundary=Boundaries(Uu, Lu)
-## downward union class
- print "elaborate downward union"
- Ld=LowerApproximation(DownwardUnionClass, Dominated) # lower approximation of downward union for type 3 rules
- Ud=UpperApproximation(DownwardUnionClass,Dominating ) # upper approximation of downward union
- DownwardBoundary=Boundaries(Ud, Ld)
-
- QualityOfQpproximation(DownwardBoundary, infosystem)
- print "RULES extraction"
- RULES=Domlem(Lu,Ld, infosystem)
-
- Parser_mapcalc(RULES, outputMap)
- Print_rules(RULES, outputTxt)
- end=time()
- print "Time computing-> %.4f s" % (end-start)
+ try:
+ start=time()
+ attributes = options['criteria'].split(',')
+ preferences=options['preferences'].split(',')
+ decision=options['decision']
+ outputMap= options['outputMap']
+ outputTxt= options['outputTxt']
+ out=BuildFileISF(attributes, preferences, decision, outputMap, outputTxt)
+
+ infosystem=FileToInfoSystem(out)
+
+ UnionOfClasses(infosystem)
+ DownwardUnionClass=DownwardUnionsOfClasses(infosystem)
+ UpwardUnionClass=UpwardUnionsOfClasses(infosystem)
+ Dominating=DominatingSet(infosystem)
+ Dominated=DominatedSet(infosystem)
+ ## upward union class
+ print "elaborate upward union"
+ Lu=LowerApproximation(UpwardUnionClass, Dominating) #lower approximation of upward union for type 1 rules
+ Uu=UpperApproximation(UpwardUnionClass,Dominated ) #upper approximation of upward union
+ UpwardBoundary=Boundaries(Uu, Lu)
+ ## downward union class
+ print "elaborate downward union"
+ Ld=LowerApproximation(DownwardUnionClass, Dominated) # lower approximation of downward union for type 3 rules
+ Ud=UpperApproximation(DownwardUnionClass,Dominating ) # upper approximation of downward union
+ DownwardBoundary=Boundaries(Ud, Ld)
+
+ QualityOfQpproximation(DownwardBoundary, infosystem)
+ print "RULES extraction (*)"
+ RULES=Domlem(Lu,Ld, infosystem)
+
+ Parser_mapcalc(RULES, outputMap)
+ Print_rules(RULES, outputTxt)
+ end=time()
+ print "Time computing-> %.4f s" % (end-start)
+ return 0
+ except:
+ print "ERROR in main function!"
+ sys.exit()
- return 0
-
if __name__ == "__main__":
options, flags = grass.parser()
sys.exit(main())
Modified: grass-addons/raster/mcda/r.roughset/main.c
===================================================================
--- grass-addons/raster/mcda/r.roughset/main.c 2011-05-31 20:20:21 UTC (rev 46478)
+++ grass-addons/raster/mcda/r.roughset/main.c 2011-05-31 21:38:07 UTC (rev 46479)
@@ -227,7 +227,7 @@
classify_vect = G_malloc(sizeof(int) * (nrows * ncols)); /* memory allocation */
- rough_analysis(nrows, ncols, output_txt->answer, classify_vect, attributes, dec_txt->answer, strgy, cls); /* extract rules from RSL and generate classified vectpr */
+ rough_analysis(nrows, ncols, output_txt->answer, classify_vect, attributes, dec_txt->answer, strgy, cls); /* extract rules from RSL and generate classified vector */
/* controlling, if we can write the raster */
if ((outfd = G_open_raster_new(result, CELL_TYPE)) < 0)
@@ -245,6 +245,7 @@
{
c = ((CELL *) classify_vect[j]);
((CELL *) outrast)[col] = c;
+ G_message("%d", c);
j++;
}
More information about the grass-commit
mailing list