diff --git a/examples/example-option-2/mechanisms.json b/examples/example-option-2/config/mechanisms.json similarity index 100% rename from examples/example-option-2/mechanisms.json rename to examples/example-option-2/config/mechanisms.json diff --git a/examples/example-option-2/parameters.json b/examples/example-option-2/config/parameters.json similarity index 100% rename from examples/example-option-2/parameters.json rename to examples/example-option-2/config/parameters.json diff --git a/examples/simple_transfer.py b/examples/simple_transfer.py index b6fca99f..5f7ba474 100644 --- a/examples/simple_transfer.py +++ b/examples/simple_transfer.py @@ -1,31 +1,81 @@ -import shutil, os, sys +import shutil, os, sys, argparse sys.path.append('../tools') from transfer import SimpleTransfer as strans ''' -if mechnisms.json, - parameters.json and - best_models.json -all in base of source -and morphology is in - source/morphology -specifying source and destination is all that should be needed -''' +run this file with arguments or change the paths to source and destination +below (source and new_celltype_path, respectively) + + $ python simple_transfer.py -s -d + +In order to use hall_of_fame instead of best_models (default) +use the -h argument and send the path to the file + + $ python simple_transfer.py -hof path/hall_off_fame.json -# Please specify/update these paths, below -# --------------------------------------------------------------- -source_path = '../examples/example-option-2' # extra path added for illustration -new_celltype_path = '../data/neurons/striatum/test' -# --------------------------------------------------------------- +use the -rm/--delete argument to delete the destination folder before transfer (if exists) + + $ python simple_transfer.py -rm 1 +TODO: +-verify transfer +''' + +parser = argparse.ArgumentParser(description='Simple program for transfer (conversion) of BPO models into snudda') +parser.add_argument('-s','--sours', help='source directory') +parser.add_argument('-a','--all', help='transfer all models in directory (sub directories)', default=0) +parser.add_argument('-d','--destination', help='destination argument') +parser.add_argument('-hof','--hall_off_fame', help='list of best models (best_models/hall_off_fame)', default=None) +parser.add_argument('-rm','--delete', help='delete destination', default=0) +args = vars(parser.parse_args()) -# create new folder for storing the new cell (using the same name as the source) -source_name = os.path.split(source_path)[-1] -new_cell_path = '{}/{}'.format(new_celltype_path, source_name) # past the name of the source model to the celltype path -os.makedirs(new_cell_path, exist_ok=True) +def do_transfer(source_path, new_cell_path, hof): + print('\n--------------------') + print(f'tranfering source: \n\t{source_path} \nto destination \n\t{new_cell_path}\n') + strans.SimpleTransfer(source_path, new_cell_path, optimisation_result_file=hof) +# Please specify/update these paths, below (if not added as argument(s)) +# ------------------------------------------------------------------- +if args['sours']: + source_path = args['sours'].strip('/') +else: + # update here + source_path = '../examples/example-option-2' # extra path added for illustration + +if args['destination']: + new_celltype_path = args['destination'] +else: + # update here + new_celltype_path = '../data/neurons/striatum/test' +# ------------------------------------------------------------------- # do the transfer -strans.SimpleTransfer(source_path, new_cell_path) +if args['all']: + subdir = [ f.name for f in os.scandir(source_path) if f.is_dir() ] + print(subdir) + for d in subdir: + celltype = d.split('-')[1] # this is hardcoded and assumes that the type is in the first location of the filename + destination = os.path.join(new_celltype_path, celltype, d) + sub_source_path = os.path.join(source_path, d) + if int(args['delete']) and os.path.isdir(destination): + shutil.rmtree(destination) + os.makedirs(destination, exist_ok=True) + do_transfer(sub_source_path, destination, args['hall_off_fame']) +else: + if int(args['delete']): + shutil.rmtree(new_celltype_path) + + # create a new folder for storing the new cell (using the same name as the source) + if os.path.normpath(source_path).count(os.sep): # this check if there are more than one level in the path + source_name = os.path.split(source_path)[-1] + else: + source_name = source_path + + new_cell_path = '{}/{}'.format(new_celltype_path, source_name) # past the name of the source model to the celltype path + os.makedirs(new_cell_path, exist_ok=True) + + do_transfer(source_path, new_cell_path, args['hall_off_fame']) + + diff --git a/tools/transfer/SimpleTransfer.py b/tools/transfer/SimpleTransfer.py index 3d385811..db74f1fd 100644 --- a/tools/transfer/SimpleTransfer.py +++ b/tools/transfer/SimpleTransfer.py @@ -10,11 +10,12 @@ class SimpleTransfer: """ Class to Transfer all the files from Bluepyopt format to Snudda - By default assumes that the files listed below are all in the base of source - if not, one can specify each path directly by passing them as arguments + By default assumes that the files -mechanisms.json -parameters.json + are located in the config folder of the source, and that -best_model.json + are in the base of source if you are using hall_of_fame.json instead of best_models.json, please set the optimization_result_file directly @@ -38,22 +39,22 @@ def __init__(self, source, self.destination = destination if not optimisation_result_file: - self.optimisation_result_file = '{}/best_models.json'.format(source) + self.optimisation_result_file = f'{source}/best_models.json' else: self.optimisation_result_file = optimisation_result_file if not mechanisms_path_folder: - self.mechanisms_path_folder = source + self.mechanisms_path_folder = f'{source}/config' else: self.mechanisms_path_folder = mechanisms_path_folder if not parameters_path_folder: - self.parameters_path_folder = '{}/parameters.json'.format(source) + self.parameters_path_folder = f'{source}/config/parameters.json' else: self.parameters_path_folder = parameters_path_folder if not morphology_path_folder: - self.morphology_path_folder = '{}/morphology'.format(source) + self.morphology_path_folder = f'{source}/morphology' else: self.morphology_path_folder = morphology_path_folder @@ -61,6 +62,8 @@ def __init__(self, source, self.selected_models = selected_models self.transfer() + + print('\n---transfer complete---') def transfer(self): diff --git a/tools/transfer/mechanisms.py b/tools/transfer/mechanisms.py index 01342cf9..d023dd57 100644 --- a/tools/transfer/mechanisms.py +++ b/tools/transfer/mechanisms.py @@ -21,3 +21,5 @@ def transfer_mechanisms(source=None, destination=None, direct_path=None): if not os.path.exists(destination): os.mkdir(destination) shutil.copy(mechanisms_path, os.path.join(destination, "mechanisms.json")) + + print("mechanims file transfer complete") diff --git a/tools/transfer/morphology.py b/tools/transfer/morphology.py index b18b1e45..2179129b 100644 --- a/tools/transfer/morphology.py +++ b/tools/transfer/morphology.py @@ -27,6 +27,7 @@ def transfer_morphologies(source=None, destination=None, selected=False, direct_ if direct_path_morph: morphology_directory = os.path.join(direct_path_morph) + source = os.path.split(direct_path_morph)[0] else: morphology_directory = os.path.join(source, "morphology") @@ -64,9 +65,6 @@ def transfer_morphologies(source=None, destination=None, selected=False, direct_ with open(os.path.join(morphology_destination, "morphology_hash_filename.json"), "w") as f: json.dump(hash_name_dict, f, indent=4, sort_keys=True) - print(f"Morphology file transfer complete \n" - f" \n" - f"from : {source} \n" - f"to : {destination} \n") + print("Morphology file transfer complete") diff --git a/tools/transfer/parameters.py b/tools/transfer/parameters.py index 06e507ea..d5ad3672 100644 --- a/tools/transfer/parameters.py +++ b/tools/transfer/parameters.py @@ -188,3 +188,4 @@ def transfer_parameters(source=None, destination=None, direct_path_param=None, parameters_path=parameters_path, best_models_path=best_models_path, selected=selected) + print("Parameter file transfer complete")