IMP logo
IMP Reference Guide  develop.86cca0715b,2025/05/19
The Integrative Modeling Platform
utility.py
1 """@namespace IMP.EMageFit.utility
2  Utility functions.
3 """
4 
5 import os
6 import sys
7 import IMP
8 import importlib.machinery
9 import importlib.util
10 
11 
12 def vararg_callback(option, opt_str, value, parser):
13  """
14  Snippet from Python website to process multiple values for
15  an option with OptionParser
16  """
17 
18  assert value is None
19  value = []
20 
21  def floatable(str):
22  try:
23  float(str)
24  return True
25  except ValueError:
26  return False
27 
28  for arg in parser.rargs:
29  # stop on --foo like options
30  if arg[:2] == "--" and len(arg) > 2:
31  break
32  # stop on -a, but not on -3 or -3.0
33  if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
34  break
35  value.append(arg)
36 
37  del parser.rargs[:len(value)]
38  setattr(parser.values, option.dest, value)
39 
40  return value
41 
42 
43 def get_experiment_params(fn_params):
44  """
45  Imports the configuration file
46  @param fn_params configuration file
47  @return Experiment Class with all the information from the config file
48  """
49  name, ext = os.path.splitext(fn_params)
50  foo = _import_from_path(name, fn_params)
51  exp = foo.Experiment()
52  # convert to absolute paths
53  exp.fn_pdbs = [IMP.get_relative_path(fn_params, fn) for fn in exp.fn_pdbs]
54  if hasattr(exp, "sampling_positions"):
55  exp.sampling_positions.read = IMP.get_relative_path(
56  fn_params, exp.sampling_positions.read)
57  if hasattr(exp, "benchmark"):
58  if hasattr(exp.benchmark, "fn_pdb_native"):
59  exp.benchmark.fn_pdb_native = IMP.get_relative_path(
60  fn_params, exp.benchmark.fn_pdb_native)
61  if hasattr(exp.benchmark, "fn_pdbs_native"):
62  fns = []
63  for fn in exp.benchmark.fn_pdbs_native:
64  fns.append(IMP.get_relative_path(fn_params, fn))
65  exp.benchmark.fn_pdbs_native = fns
66 
67  if hasattr(exp, "dock_transforms"):
68  for i in range(len(exp.dock_transforms)):
69  exp.dock_transforms[i][2] = IMP.get_relative_path(
70  fn_params, exp.dock_transforms[i][2])
71  if hasattr(exp, "em2d_restraints"):
72  for i in range(len(exp.em2d_restraints)):
73  exp.em2d_restraints[i][1] = IMP.get_relative_path(
74  fn_params, exp.em2d_restraints[i][1])
75  return exp
76 
77 
78 def _import_from_path(module_name, file_path):
79  """Import a Python source file directly as a module"""
80  loader = importlib.machinery.SourceFileLoader(module_name, file_path)
81  spec = importlib.util.spec_from_loader(module_name, loader)
82  module = importlib.util.module_from_spec(spec)
83  sys.modules[module_name] = module
84  spec.loader.exec_module(module)
85  return module
def vararg_callback
Snippet from Python website to process multiple values for an option with OptionParser.
Definition: utility.py:12
def get_experiment_params
Imports the configuration file.
Definition: utility.py:43
std::string get_relative_path(std::string base, std::string relative)
Return a path to a file relative to another file.