-
Notifications
You must be signed in to change notification settings - Fork 13
standalone template retriever #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| #!/opt/gensoft/exe/TemplateFlow_pyclient/0.8.0/venv/bin/python3.8 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have the standard apache header (which implicitly means you accept your contribution is covered by the license).
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my bad, I should have cleaned this sehbang. |
||
|
|
||
| import argparse | ||
| import os | ||
| import sys | ||
|
|
||
| #---- as templateflow.api.TF_LAYOUT.root is set at import time based on default or TEMPLATE_FLOW env var | ||
| # and it is not writable, we have to import template flow with environnement set if we want to be | ||
| # abble to set cache dir from command line option | ||
|
|
||
| import pprint | ||
|
|
||
| TOOL_VERSION = '0.1' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why having a different version for this module?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. first itération of the "getter" tool, I will comply with the module version instead |
||
|
|
||
| def parse_args(): | ||
| parser = argparse.ArgumentParser(description='TemplateFlow templates retriever.') | ||
|
|
||
| r_group = parser.add_argument_group('retrieve options') | ||
| r_group.add_argument('-r', '--resolution', | ||
| action='store', | ||
| default='unset', | ||
| help='Index to an specific spatial resolution of the template, multiple values coma separated may be given') | ||
|
|
||
| r_group.add_argument('-s', '--suffix', | ||
| action='store', | ||
| default='unset', | ||
| help='BIDS suffix') | ||
|
|
||
| r_group.add_argument('-a', '--atlas', | ||
| action='store', | ||
| default='unset', | ||
| help='Name of a particular atlas') | ||
|
|
||
| r_group.add_argument('-e', '--extension', | ||
| action='store', | ||
| default='unset', | ||
| help='file extension') | ||
|
|
||
| r_group.add_argument('-H', '--hemi', | ||
| action='store', | ||
| choices=['R', 'L'], | ||
| default='unset', | ||
| help='select Hemisphere') | ||
|
|
||
| r_group.add_argument('-S', '--space', | ||
| action='store', | ||
| default='unset', | ||
| help='Space template is mapped to') | ||
|
|
||
| r_group.add_argument('-d', '--density', | ||
| action='store', | ||
| default='unset', | ||
| help='Surface density') | ||
|
|
||
| r_group.add_argument('-D', '--desc', | ||
| action='store', | ||
| default='unset', | ||
| help='Description field') | ||
|
|
||
| r_group.add_argument('-c', '--cohort', | ||
| action='store', | ||
| default='unset', | ||
| help='Template cohort') | ||
|
|
||
| r_group.add_argument('-L', '--label', | ||
| action='store', | ||
| default='unset', | ||
| help='Template label') | ||
|
|
||
| i_group = parser.add_argument_group('info environement') | ||
| i_group.add_argument('-l', '--list', | ||
| action='store_true', | ||
| default=False, | ||
| help='list available local templates') | ||
|
|
||
| i_group.add_argument('-m', '--metadata', | ||
| action='store_true', | ||
| default=False, | ||
| help='Get full metadata for given template') | ||
|
|
||
| i_group.add_argument('-C', '--citations', | ||
| action='store_true', | ||
| default=False, | ||
| help='Get template citations') | ||
|
|
||
| i_group.add_argument('-B', '--bibtex-citations', | ||
| action='store_true', | ||
| default=False, | ||
| help='Get template citation in bibtex-citations') | ||
|
|
||
| m_group = parser.add_argument_group('misc options') | ||
| m_group.add_argument('-o', '--tf-cache', | ||
| action='store', | ||
| default='unset', | ||
| help='Directory to save templates in. default `${HOME}/.cache/templateflow`') | ||
|
Comment on lines
+92
to
+95
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this works around the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure to understand
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR should only give access to the templateflow API. There is no function to change If a user wants to change where templates are cached, they should set the environment variable themselves.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK got the point. I will adapt regarding this point. Eric |
||
|
|
||
| m_group.add_argument('-v', '--version', | ||
| action='store_true', | ||
| default=False, | ||
| help='Display version of this tools AND TemplateFlow Client version') | ||
|
|
||
| parser.add_argument('args', nargs='*',metavar=('template [template ...]') ) | ||
| opts = parser.parse_args(sys.argv[1:]) | ||
| args = opts.args | ||
| return opts, args | ||
|
|
||
|
|
||
| def version_info(): | ||
| import templateflow | ||
| prog = os.path.basename(sys.argv[0]) | ||
| print(f'{prog} v{TOOL_VERSION}') | ||
| print(f'Using TemplateFlow Client version {templateflow.__version__}') | ||
| sys.exit(0) | ||
|
|
||
| def templates_info(): | ||
| import templateflow.api as tf | ||
| templates=tf.templates() | ||
| ident_fmt = '\n'.join(templates) | ||
| print(f"{len(templates)} templates:\n-------------------\n{ident_fmt}") | ||
| sys.exit(0) | ||
|
|
||
| def metadata_info(args): | ||
| import templateflow.api as tf | ||
| pp = pprint.PrettyPrinter(indent=4) | ||
| for tmpl in args: | ||
| infos=tf.get_metadata(tmpl) | ||
| pp.pprint(infos) | ||
| sys.exit(0) | ||
|
|
||
| def citation_info(args, bibtex=True): | ||
| import templateflow.api as tf | ||
| # bibtex_format = True if opts.bibtex_citations else False | ||
| pp = pprint.PrettyPrinter(indent=4) | ||
| for tmpl in args: | ||
| print(tmpl) | ||
|
|
||
| infos=tf.get_citations(tmpl, bibtex=bibtex) | ||
| pp.pprint(infos) | ||
| sys.exit(0) | ||
|
|
||
| def template_retriever(args, options): | ||
| import templateflow | ||
| import templateflow.api as tf | ||
|
|
||
| for tmpl in args: | ||
| try: | ||
| tmpl = tf.get(tmpl, raise_empty=True, **options) | ||
| print('\n'.join(map(str, tmpl))) | ||
| except Exception as msg: | ||
| print(tmpl, msg) | ||
| sys.exit(0) | ||
|
|
||
|
|
||
| def main(): | ||
|
|
||
| opts, args = parse_args() | ||
|
|
||
| if opts.version: | ||
| version_info() | ||
|
|
||
| if opts.list: | ||
| templates_info() | ||
|
|
||
| if opts.metadata: | ||
| metadata_info(args) | ||
|
|
||
| if opts.citations or opts.bibtex_citations: | ||
| citation_info(args, opts.bibtex_citations) | ||
|
|
||
| if opts.tf_cache != 'unset': | ||
| #---- TemplatFlow cache dir was specified, export it | ||
| print("saving to ---------->", opts.tf_cache) | ||
| print("saving to ---------->", os.path.abspath(opts.tf_cache)) | ||
| os.environ['TEMPLATEFLOW_HOME'] = os.path.abspath(opts.tf_cache) | ||
|
Comment on lines
+170
to
+174
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be managed by templateflow
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems to me I was lazy and copied this spnippet from the docs or examples . |
||
|
|
||
| #---- allow multi resolution requirement | ||
| if ',' in opts.resolution: | ||
| opts.resolution = tuple(opts.resolution.split(',')) | ||
|
|
||
| #---- clean unset options and be sure to remove non relevant options | ||
| # to not introduce unknown arguments when calling tf.get | ||
| options={k:v for k,v in vars(opts).items() if v != 'unset' and v != False} | ||
| non_options = ['list', 'metadata', 'args', 'tf_cache', 'citations', 'bibtex_citation', 'version'] | ||
| for e in non_options: | ||
| options.pop(e, None) | ||
|
|
||
| #---- argument None is acquired as a string, not python None. convert it | ||
| for k,v in options.items(): | ||
| if v == 'None': options[k] = None | ||
|
|
||
| template_retriever(args, options) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hum I'm affraid it will collapse will templateflow name space....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have you tried? because it won't...