X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 09 Jun 2018 03:42 AM by  Alireza Taravat
ForwardPCATransform Parameters
 2 Replies
Sort:
You are not authorized to post a reply.
Author Messages

Alireza Taravat



New Member


Posts:1
New Member


--
08 Jun 2018 02:42 AM
    I would like to apply "Forward PCA Rotation New Statistics and Rotate" model in ENVI on 500 images using "ForwardPCATransform" task, but I could not find any document about the "ForwardPCATransform" task parameters. For Example how to define "Mask Band" or how to define "Stats Subset" or "output State file name" or calculate using "Covariance Matrix or Correlation matrix" or "Number of Output PC Bands".

    Any idea?
    Thanks

    MariM



    Veteran Member


    Posts:2396
    Veteran Member


    --
    08 Jun 2018 09:23 AM
    Masks are defined using MaskRaster and the pixel state:
    http://www.harrisgeospatial.com/docs/masks.html

    A stats subset would be defined by subsetting the input data using a sub_rect.on open or using SubsetRaster.
    Statistics can be calculated separately using ENVIRasterStatisticsTask and calculating the covariance, which is what the old PC_ROTATE did: http://www.harrisgeospati.../docs/PC_ROTATE.html

    There is no option to use covariance stats on the forward PCA in the task. It is less commonly used so I don't think they included this parameter.

    Alireza Taravat



    New Member


    Posts:1
    New Member


    --
    09 Jun 2018 03:42 AM
    Thanks for the reply. I wrote the below code snippet which is for the PCA calculation and the PCA statistics. I thought it is better to share it here for anyone how might need it.



    PRO PCA
    e = ENVI()
    ; Define the filepath and the filter
    filepath = 'D:\masterEO/Scores.tif'
    ; Number of PCA, if you put 0 or minus value, the PCA will be equel to the number of bands.
    PCA_Dimention = 5
    ; Opening the images
    Raster = e.OpenRaster(filepath)
    data = raster.GetData()
    dim_data = Size(data, /Dimensions)
    bands = FltArr(dim_data[2], dim_data[0] * dim_data[1])
    FOR I=0,dim_data[2]-1 DO bands[I,*] = Reform(data(*,*, I), 1, dim_data[0] * dim_data[1])
    dim_bands = Size(bands, /Dimensions)
    means = TOTAL(bands, 2)/dim_bands[1]
    bands = bands - REBIN(means, dim_bands[0], dim_bands[1])
    ;Test the PCA Dimention
    IF (PCA_Dimention GT dim_bands[0]) OR (PCA_Dimention LE 0) Then PCA_Dimention = dim_bands[0]
    ;Compute the principal components.
    result = PCOMP(bands, COEFFICIENTS = coefficients, /DOUBLE, $
    EIGENVALUES=eigenvalues, VARIANCES=variances, /COVARIANCE, NVARIABLES = PCA_Dimention)
    Correlate = CORRELATE(bands)
    Raster_Result = FltArr(dim_data[0], dim_data[1], PCA_Dimention)
    FOR I=0, PCA_Dimention-1 DO Raster_Result[*,*,I] = Reform(result(I,*,*), dim_data[0], dim_data[1])
    ; Create output image
    METADATA_PCA = ENVIRasterMetadata()
    METADATA_PCA.AddItem, 'BAND NAMES', string(indgen(PCA_Dimention, START=1))+":PCA"
    PCA_Raster = ENVIRaster(Raster_Result, URI=StrSplit(filepath, '.tif', /EXTRACT, /REGEX)+"_PCA.dat", SPATIALREF=Raster.SPATIALREF, METADATA=METADATA_PCA)
    PCA_Raster.Save
    ; Print the Stat file
    Write_format = STRJOIN(["('Band#',",'I2,',strcompress(dim_bands[0],/REMOVE_ALL),"(F10.6))"])
    filepat='D:\test\stat.txt'
    OPENW, lun, filepat, /get_lun ;Write the Pat file ready for NN
    PRINTF, lun, 'Coefficients: '
    FOR mode=0,dim_bands[0]-1 DO PRINTF, lun, mode+1, coefficients[*,mode], FORMAT=Write_format
    eigenvectors = coefficients/REBIN(eigenvalues, dim_bands[0], dim_bands[0])
    PRINTF, lun
    PRINTF, lun, 'Correlations: '
    FOR mode=0,dim_bands[0]-1 DO PRINTF, lun, mode+1, Correlate[*,mode], FORMAT=Write_format
    PRINTF, lun
    PRINTF, lun, 'Eigenvectors: '
    FOR mode=0,dim_bands[0]-1 DO PRINTF, lun, mode+1, eigenvectors[*,mode], FORMAT=Write_format
    PRINTF, lun
    PRINTF, lun, 'Energy conservation: ', TOTAL(bands^2), $
    TOTAL(eigenvalues)*(dim_bands[0]-1)
    PRINTF, lun
    PRINTF, lun, ' Mode Eigenvalue PercentVariance'
    FOR mode=0,dim_bands[0]-1 DO PRINTF, lun, $
    mode+1, eigenvalues[mode], variances[mode]*100
    FREE_LUN, lun
    ; Get the collection of data objects currently available in the Data Manager
    DataColl = e.Data
    ; Add the output to the Data Manager
    DataColl.Add, PCA_Raster
    ; Display the result
    View1 = e.GetView()
    Layer1 = View1.CreateLayer(PCA_Raster)
    END
    You are not authorized to post a reply.