# Ajayrama Kumaraswamy, 2017, LMU Munich

"""
Description:        This script is used to generate a parameter file required to run pcaBased.py. This parameter file
                    should contain a json string of a list of dictionaries. Each dictionary is one 'pcaBased' job
                    that contains 'pcaBased' parameters.
                    See core/RegMaxSPars.py for the description of the parameters.

Usage:              python constructPCABasedRegParFile.py

Action:             creates a parameter file for pcaBased.py

Usage guidelines:   There are a couple of cases with examples shown below.
                    Read the comments therein.
                    Essentially edit the values of some variables in this script and run it.


"""

import os
import json
from regmaxsn.core.RegMaxSPars import pcaBasedParNames

temp = os.path.split(os.path.abspath(__file__))[0]
temp1 = os.path.split(temp)[0]


# **********************************************************************************************************************

# Default parameters
# distances in um, angles in radians
gridSizes = [40.0, 20.0, 10.0]
usePartsDir = True

# **********************************************************************************************************************

# # User defined parameters
# # distances in um, angles in radians
# gridSizes = [40.0, 20.0, 10.0]
# usePartsDir = False

# **********************************************************************************************************************

# # Case 1: Default or user defined parameters above and
# # refSWC: the reference SWC
# # testSWC: the test SWC
# # resFile: the registered version of testSWC will be written into this file path when RegMaxS.py is run with the
# # parameter file generated by this script
# # parFile: the parameter file will be generated at this file path.

# # Usage: Replace refSWC, testSWC, resFile and parFile as required and run this file to generate parFile
# # Then run python <...>/pcaBased.py parFile
#
# refSWC = os.path.join(temp1, 'TestFiles', 'HSNR', 'HSN-fluoro01.CNG.swc')
# testSWC = os.path.join(temp1, 'TestFiles', 'HSNR', 'HSN-fluoro04.CNG.swc')
# resFile = os.path.join(temp1, 'Results', 'PCABased', 'HSNR', 'HSN-fluoro04.CNG.swc')
# parFile = os.path.join(temp1, 'ParFiles', 'PCABased', 'HSNR1-4.json')
#
#
# obtains the list of variables in the current work space
# ns = vars()
# forms the dictionary of parameters to be saved into the parameter file.
# pars = [{k: ns[k] for k in pcaBasedParNames}]
# **********************************************************************************************************************

# # Case 2: Default or user defined parameters above, one reference SWC and several test SWCs, all in the same folder.
# # dirPath: directory containing the reference SWC and the test SWCs
# # refSWC: the reference SWC
# # testSWCFiles: file names of test SWC files in dirPath
# # parameter file generated by this script
# # parFile: the parameter file will be generated at this file path.
# # resPath: the testSWCs registered to the reference SWC will be generated in this directory when RegMaxS.py is run
# # with the parameter file generated by this running this script.

# # Usage: Replace dirPath, refSWC, expNames, resPath and parFile as required and run this file to generate parFile
# # Then run python <...>/pcaBased.py parFile
# -----------------------------------------------------------------------------------
# dirPath = os.path.join(temp1, 'TestFiles', 'HSNR')
# refSWC = os.path.join(dirPath, 'HSN-fluoro01.CNG.swc')
# expNames = [
#             'HSN-fluoro01.CNG',
#             'HSN-fluoro04.CNG',
#             'HSN-fluoro05.CNG',
#             'HSN-fluoro07.CNG',
#             'HSN-fluoro09.CNG',
#             ]
# resPath = os.path.join(temp1, 'Results', 'PCABased', 'HSNR')
# parFile = os.path.join(temp1, 'ParFiles', 'PCABased', 'HSNR.json')
#
# pars = []
# for sfr in expNames:
#     testSWC = os.path.join(dirPath, sfr + '.swc')
#     resFile = os.path.join(resPath, sfr + '.swc')
#     # obtains the list of variables in the current work space
#     ns = vars()
#     # forms the dictionary of parameters to be saved into the parameter file.
#     pars.append({k: ns[k] for k in pcaBasedParNames})

# -----------------------------------------------------------------------------------
# dirPath = os.path.join(temp1, 'TestFiles', 'HSNL')
# refSWC = os.path.join(dirPath, 'HSN-fluoro02.CNG.swc')
# expNames = [
#             'HSN-fluoro02.CNG',
#             'HSN-fluoro03.CNG',
#             'HSN-fluoro06.CNG',
#             'HSN-fluoro08.CNG',
#             'HSN-fluoro10.CNG',
#             ]
# resPath = os.path.join(temp1, 'Results', 'PCABased', 'HSNL')
# parFile = os.path.join(temp1, 'ParFiles', 'PCABased', 'HSNL.json')
#
# pars = []
# for sfr in expNames:
#     testSWC = os.path.join(dirPath, sfr + '.swc')
#     resFile = os.path.join(resPath, sfr + '.swc')
#     # obtains the list of variables in the current work space
#     ns = vars()
#     # forms the dictionary of parameters to be saved into the parameter file.
#     pars.append({k: ns[k] for k in pcaBasedParNames})
# # -----------------------------------------------------------------------------------
dirPath = os.path.join(temp1, 'TestFiles', 'LLC')
refSWC = os.path.join(dirPath, 'Gad1-F-000062.CNG.swc')
expNames = [
            'Gad1-F-000062.CNG',
            'Cha-F-000012.CNG',
            'Cha-F-300331.CNG',
            'Gad1-F-600000.CNG',
            'Cha-F-000018.CNG',
            'Cha-F-300051.CNG',
            'Cha-F-400051.CNG',
            'Cha-F-200000.CNG'
            ]
resPath = os.path.join(temp1, 'Results', 'PCABased', 'LLC')
parFile = os.path.join(temp1, 'ParFiles', 'PCABased', 'LLC.json')

pars = []
for sfr in expNames:
    testSWC = os.path.join(dirPath, sfr + '.swc')
    resFile = os.path.join(resPath, sfr + '.swc')
    # obtains the list of variables in the current work space
    ns = vars()
    # forms the dictionary of parameters to be saved into the parameter file.
    pars.append({k: ns[k] for k in pcaBasedParNames})
# -----------------------------------------------------------------------------------
# **********************************************************************************************************************

# write the parameters into the parameter file.
with open(parFile, 'w') as fle:
    json.dump(pars, fle)
