Post

2 followers Follow
1
Avatar

Outputing MLR as Report in Python

Hi,

I am trying to get the output of the MLR function into a report and so far I have been unsuccessful.

What I have tried:

I followed the example from this link:
http://support.pcigeomatics.com/hc/communities/public/questions/203566673-Write-report-to-file-in-python

As well as from this link:
http://www.pcigeomatics.com/python-api-doc/pci_nspio.html

So far neither of these have worked for MLR or even CSR; oddly enough the none report functions do print to report, e.g. MLC, CSG2, etc...

Here is the code snippet:

from pci.nspio import Report, enableDefaultReport
from pci.exceptions import *
from pci.mlr import *

rep_file = os.path.splitext(sys.argv[0])[0] + ".txt"
enableDefaultReport(rep_file)
Report.clear()

----------------------------------------------------------------------------------

Use MLR algorithm to create Maximum Liklihood Classification Report

----------------------------------------------------------------------------------

print ""
print "Beginning the MLR algorithm to create classification report"
for ls in landsat:
    for pix in ls:
        file = pix
        print file
        units = "KILO"      # measurement squared map units
        dbic = [6]          # input theme map to channel 6
        dbsa = [5]          # encoded bitmap channel 5
        dbs1 = range(2, 6 + 1)      # Signature segments to be used
        matrix = "YES"      # Create confusion matrix
        mask = []           # process entire channel

        try: mlr(file, units, dbic, dbsa, dbs1, matrix, mask)
        except PCIException, e: print e
        except Exception, e: print e
        print "MLR Algorithm Complete. Classification Report Completed."

 enableDefaultReport("term")

Thanks!

Jean-Sebastien Roussy

Please sign in to leave a comment.

4 comments

0
Avatar

Hi Jean-Sebastian,

it looks like a very small mistake. You call the enableDefaultReport() function to port the report information to your report file before you call the Report.clear() function. Try swapping those calls...

from pci.mlr import mlr 
from pci.nspio import Report, enableDefaultReport

rep = 'my_rep.txt'

Report.clear()
enableDefaultReport(your_report)

for ls in landsat:
    for pix in ls:
        mlr(file=pix, dbic=[6], dbsa=[5], dbs1=range(2, 6+ 1), matrix='yes')

enableDefaultReport('term')

Just a few notes, you can use keywords to pass arguments to Geomatica functions and therefore you do not need to define every argument (just forget about the default args :)). Also, with the way you setup your code, it will write the report information to the same report file for all images in your ls list and append the results to EOF.

Shawn Melamed 0 votes
0
Avatar

Hi Shawn,

So, I tried what you suggested and managed to get the other functions' reports to append to EOF. However, MLR and CSR did not.

If you have a working example could you please post it, plus results. I am really not sure what might be going wrong at this point.

Cheers,

Jean-Sebastien Roussy 0 votes
0
Avatar

You can download the working data and script from here: http://dl.pcigeomatics.com/Marketing/devzone/datapacks/report_to_file.zip

I go through the steps on YouTube: https://www.youtube.com/watch?v=ddkcwRplCFM

The script is pretty short and is as follows:

import os
from pci.mlr import mlr
from pci.nspio import Report, enableDefaultReport

# set input and output files for workflow
working_dir = r'D:\DataPacks\report_to_file'  # set this path to the dir where you extracted the tutorial data
classified_image = working_dir + os.sep + 'irvine.pix'
rep_file = working_dir + os.sep + 'MLC_report.txt'

try: 
    Report.clear()
    enableDefaultReport(rep_file)

    mlr(file=classified_image, dbic=[6], dbs1=[17, 18, 19, 20, 21, 22, 23, 24])
    # You can run other Geomatica functions before closing the report file, which will append to the end of the rep file

finally:
    enableDefaultReport('term')  # this will close the report file

Good Luck!

Shawn Melamed 0 votes
0
Avatar

Another option is to try running it on only one of your Landsat images (omit the for-loops for now). Perhaps your landsat or ls lists are empty and the block of code is not even being called.

Shawn Melamed 0 votes