Skip to content
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

[WIP] ENH : first pass a getting import hooks to work #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
'vttools.vtmods.import_lists'
],
include_dirs=[np.get_include()],
package_data = {'vttools.vtmods.import_lists': ['*.yaml']}
package_data = {'vttools.vtmods.import_lists': ['*.yaml'],
'vttools': ['*.yaml'],
}
)


Expand Down
76 changes: 72 additions & 4 deletions vt_config/NSLS-II/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,67 @@
import importlib
import collections
import os
from vttools import wrap_lib
import vistrails
from vttools.vtmods.import_lists import load_config
import_dict = load_config()

from vttools import wrap_lib
from vttools.wrap_lib import AutowrapError
logger = logging.getLogger(__name__)

# get modules to import
import_dict = load_config()
import imp


class VTImporter(object):
"""

"""
def __init__(self, path):
self._path = path

def find_module(self, fullname, path=None):
import os
name = fullname.rpartition('.')[-1]
if path is None:
path = self._path
for dn in path:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dn stands for what?

filename = os.path.join(dn, name+'.yaml')
if os.path.exists(filename):
return StructYAMLLoader(filename)
return None


class StructYAMLLoader(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume that you're going to add docs to this class?

def __init__(self, filename):
self._filename = filename

def load_module(self, fullname):
mod = sys.modules.setdefault(fullname,
imp.new_module(fullname))
mod.__file__ = self._filename
mod.__loader__ = self
with open(self._filename, 'r') as modules:
import_dict = yaml.load(modules)

# import the hand-built VisTrails modules
module_list = import_dict['import_modules']
for module_path, mod_lst in six.iteritems(module_list):
for module_name in mod_lst:
mod.__dict__[module_name.strip('.')] = importlib.import_module(
module_name, module_path)

for func_dict in import_dict['autowrap_func']:
try:
mod.__dict__[func_dict['func_name']] = (
wrap_lib.wrap_function(**func_dict))
except AttributeError:
print("failed to find {}".format(func_dict['func_name']))

return mod


def install_importer(path=sys.path):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this one too

sys.meta_path.append(VTImporter(path))


def get_modules():
Expand Down Expand Up @@ -96,5 +150,19 @@ def get_modules():
return all_mods


def get_modules2():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And docs for this guy also

Module = vistrails.core.modules.vistrails_module.Module
modules = []
spec_list = ['vttools.vt_wraps']
for spec in spec_list:
mod = importlib.import_module(spec)
for name, obj in six.iteritems(mod.__dict__):
if (isinstance(obj, type) and issubclass(obj, Module)):
modules.append(obj)
elif hasattr(obj, 'vistrails_modules'):
modules.extend(obj.vistrails_modules())
return modules

# # init the modules list
_modules = get_modules()
install_importer()
_modules = get_modules2()
85 changes: 85 additions & 0 deletions vttools/vt_wraps.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
# there are two possible keys:
# - 'import' are for python modules that have a
# _vistrails_modules() function within them
# - 'autowrap_func' are for python functions
# that should be wrapped into vistrails
# - 'autowrap_class' are for python classes
# that should be wrapped into vistrails

# list of modules to import
import_modules:
vttools.vtmods:
- .io
- .vis
- .utils
- .broker

# list of functions to autowrap
autowrap_func:
- func_name: grid3d
module_path: nsls2.core
namespace: core
- func_name: process_to_q
module_path: nsls2.recip
namespace: recip
add_input_dict: true
- func_name: hkl_to_q
module_path: nsls2.recip
namespace: recip
- func_name: twotheta_to_q
module_path: nsls2.core
namespace: recip
- func_name: q_to_twotheta
module_path: nsls2.core
namespace: recip
- func_name: bin_1D
module_path: nsls2.core
namespace: core
- func_name : bin_edges_to_centers
module_path: nsls2.core
namespace: core
- func_name : pixel_to_radius
module_path: nsls2.core
namespace: core
- func_name : pixel_to_phi
module_path: nsls2.core
namespace: core
- func_name: read_binary
module_path: nsls2.io.binary
namespace: io
- func_name: convolve
module_path: numpy
namespace: numpy
- func_name: gauss_fit
module_path: nsls2.fitting.model.physics_model
namespace: fitting
- func_name: lorentzian_fit
module_path: nsls2.fitting.model.physics_model
namespace: fitting
- func_name: lorentzian2_fit
module_path: nsls2.fitting.model.physics_model
namespace: fitting
- func_name: refine_center
module_path: nsls2.calibration
namespace: calibration
- func_name: estimate_d_blind
module_path: nsls2.calibration
namespace: calibration
- func_name: find_ring_center_acorr_iD
module_path: nsls2.image
namespace: image


#- {func_name: emission_line_search, module_path: nsls2.constants, namespace: core, add_input_dict: true}
#- {func_name: snip_method, module_path: nsls2.fitting.model.background,namespace: core, add_input_dict: true}
#- {func_name: gauss_peak, module_path: nsls2.fitting.model.physics_peak, namespace:core, add_input_dict: true}
#- {func_name: gauss_step, module_path: nsls2.fitting.model.physics_peak}
#- {func_name: gauss_tail, module_path: nsls2.fitting.model.physics_peak}
#- {func_name: elastic_peak, module_path: nsls2.fitting.model.physics_peak}
#- {func_name: compton_peak, module_path: nsls2.fitting.model.physics_peak}
#- {func_name: fit_quad_to_peak, module_path: nsls2.spectroscopy}
#- {func_name: align_and_scale, module_path: nsls2.spectroscopy}
#- {func_name: find_largest_peak, module_path: nsls2.spectroscopy}
#- {func_name: integrate_ROI_spectrum, module_path: nsls2.spectroscopy}
#- {func_name: integrate_ROI, module_path: nsls2.spectroscopy}
1 change: 1 addition & 0 deletions vttools/wrap_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def docstring_func(pyobj):
'ndarray': 'basic:Variant',
'array': 'basic:Variant',
'array_like': 'basic:Variant',
'array-like': 'basic:Variant',
'np.ndarray': 'basic:Variant',
'list': 'basic:List',
'int': 'basic:Integer',
Expand Down