# Ajayrama Kumaraswamy, 2017, LMU Munich

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

Usage:              python <path to file>constructRegMaxSParFile.py

Action:             creates a parameter file for RegMaxS.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.


"""
from numpy import pi, deg2rad
import os
import json
from regmaxsn.core.RegMaxSPars import RegMaxSParNames

# obtaining the directory path containing the folder containing this file
temp = os.path.split(os.path.abspath(__file__))[0]
temp1 = os.path.split(temp)[0]


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

# Default parameters
# distances in um, angles in radians
gridSizes = [80.0, 40.0, 20.0, 10.0]
transBounds = [[-30, 30], [-30, 30], [-30, 30]]
transMinRes = 1
rotBounds = [[-pi / 6, pi / 6], [-pi / 6, pi / 6], [-pi / 6, pi / 6]]
rotMinRes = deg2rad(1).round(4)
scaleBounds = [[0.5, 1 / 0.5], [0.5, 1 / 0.5], [0.5, 1 / 0.5]]
minScaleStepSize = 1.005
retainTempFiles = False
inPartsDir = None
outPartsDir = None
nCPU = 6

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

# # User defined parameters
# # distances in um, angles in radians
# gridSizes = [40.0, 20.0, 10.0]
# transBounds = [[-30, 30], [-30, 30], [-30, 30]]
# transMinRes = 1
# rotBounds = [[-pi / 6, pi / 6], [-pi / 6, pi / 6], [-pi / 6, pi / 6]]
# rotMinRes = deg2rad(1).round(4)
# scaleBounds = [[0.5, 1 / 0.5], [0.5, 1 / 0.5], [0.5, 1 / 0.5]]
# minScaleStepSize = 1.005

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

# # 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 <...>/RegMaxS.py parFile
#
# -------------------------------------------
# # Example 1
# refSWC = os.path.join(temp1, 'TestFiles', 'HSN-fluoro01.CNG.swc')
# testSWC = os.path.join(temp1, 'TestFiles', 'HSN-fluoro01.CNGRandTrans1.swc')
# resFile = os.path.join(temp1, 'Results', 'Tests', 'HSN-fluoro01.CNGRandTrans1.swc')
# parFile = os.path.join(temp1, 'ParFiles', 'Reg-MaxS', 'HSN-fluoro01.CNGRandTrans1.json')
# -------------------------------------------
# # Example 2
# refSWC = os.path.join(temp1, 'TestFiles', 'HSN-fluoro01.CNG.swc')
# testSWC = os.path.join(temp1, 'TestFiles', 'HSN-fluoro01.CNGRandRot0.swc')
# resFile = os.path.join(temp1, 'Results', 'Tests', 'HSN-fluoro01.CNGRandRot0.swc')
# parFile = os.path.join(temp1, 'ParFiles', 'Reg-MaxS', 'HSN-fluoro01.CNGRandRot0.json')
# -------------------------------------------
# # Example 3
refSWC = os.path.join(temp1, 'TestFiles', 'HSN-fluoro01.CNG.swc')
testSWC = os.path.join(temp1, 'TestFiles', 'HSN-fluoro01.CNGRandTranslate0.swc')
resFile = os.path.join(temp1, 'Results', 'Tests', 'HSN-fluoro01.CNGRandTranslate0.swc')
parFile = os.path.join(temp1, 'ParFiles', 'Reg-MaxS', 'HSN-fluoro01.CNGRandTranslate0.json')
# -------------------------------------------
# # Example 4
# refSWC = os.path.join(temp1, 'TestFiles', 'HSN-fluoro01.CNG.swc')
# testSWC = os.path.join(temp1, 'TestFiles', 'HSN-fluoro01.CNGRandScale0.swc')
# resFile = os.path.join(temp1, 'Results', 'Tests', 'HSN-fluoro01.CNGRandScale0.swc')
# parFile = os.path.join(temp1, 'ParFiles', 'Reg-MaxS', 'HSN-fluoro01.CNGRandScale0.json')
# -------------------------------------------
# Example 5
# refSWC = os.path.join(temp1, 'TestFiles', 'LLC', 'Gad1-F-000062.CNG.swc')
# testSWC = os.path.join(temp1, 'TestFiles', 'LLC', 'Cha-F-400051.CNG.swc')
# resFile = os.path.join(temp1, 'Results', 'Tests', 'LLC', 'Cha-F-400051.CNG.swc')
# parFile = os.path.join(temp1, 'ParFiles', 'Reg-MaxS', 'LLC1.json')
# inPartsDir = os.path.join(temp1, 'TestFiles', 'LLC', 'Cha-F-400051.CNG')
# outPartsDir = os.path.join(temp1, 'Results', 'Tests', 'LLC', 'Cha-F-400051.CNG')
#
# -------------------------------------------
# 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 RegMaxSParNames}]
# **********************************************************************************************************************

# # 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 <...>/RegMaxS.py parFile
# -------------------------------------------
# dirPath = os.path.join(temp1, 'TestFiles')
# refSWC = os.path.join(dirPath, 'HSN-fluoro01.CNG.swc')
# testSWCFiles = [
#             'HSN-fluoro01.CNGRandTrans0.swc',
#             'HSN-fluoro01.CNGRandTrans1.swc',
#             'HSN-fluoro01.CNGRandTrans2.swc',
#             'HSN-fluoro01.CNGRandTrans3.swc',
#             'HSN-fluoro01.CNGRandTrans4.swc',
#             'HSN-fluoro01.CNGRandTrans5.swc',
#             'HSN-fluoro01.CNGRandTrans6.swc',
#             'HSN-fluoro01.CNGRandTrans7.swc',
#             'HSN-fluoro01.CNGRandTrans8.swc',
#             'HSN-fluoro01.CNGRandTrans9.swc',
#
#             # 'HSN-fluoro01.CNGNoiseStd1RandTrans.swc',
#             # 'HSN-fluoro01.CNGNoiseStd2RandTrans.swc',
#             # 'HSN-fluoro01.CNGNoiseStd3RandTrans.swc',
#             # 'HSN-fluoro01.CNGNoiseStd4RandTrans.swc',
#             # 'HSN-fluoro01.CNGNoiseStd5RandTrans.swc',
#             # 'HSN-fluoro01.CNGNoiseStd6RandTrans.swc',
#             # 'HSN-fluoro01.CNGNoiseStd7RandTrans.swc',
#             # 'HSN-fluoro01.CNGNoiseStd8RandTrans.swc',
#             # 'HSN-fluoro01.CNGNoiseStd9RandTrans.swc',
#             ]
# resPath = os.path.join(temp1, 'Results', 'Tests')
# parFile = os.path.join(temp1, 'ParFiles', 'Reg-MaxS', 'HSN-fluoro01.CNGRandTrans0-9.json')
#
# pars = []
# for sfr in testSWCFiles:
#     testSWC = os.path.join(dirPath, sfr)
#     resFile = os.path.join(resPath, sfr)
#     # 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 RegMaxSParNames})
# -------------------------------------------
# **********************************************************************************************************************

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