Skip to content

Commit

Permalink
example script updated to automatically handle multiple models (e.g. …
Browse files Browse the repository at this point in the history
…HBP-2022Q2/)
  • Loading branch information
robban80 committed Sep 13, 2024
1 parent 1ed45fc commit 7c3d302
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 29 deletions.
File renamed without changes.
File renamed without changes.
88 changes: 69 additions & 19 deletions examples/simple_transfer.py
Original file line number Diff line number Diff line change
@@ -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 <source> -d <destination>
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'])


15 changes: 9 additions & 6 deletions tools/transfer/SimpleTransfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,29 +39,31 @@ 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

self.selected = selected
self.selected_models = selected_models

self.transfer()

print('\n---transfer complete---')

def transfer(self):

Expand Down
2 changes: 2 additions & 0 deletions tools/transfer/mechanisms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
6 changes: 2 additions & 4 deletions tools/transfer/morphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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")


1 change: 1 addition & 0 deletions tools/transfer/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 7c3d302

Please sign in to comment.