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

new method: new_template_with_spec_file under template.py #181

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 52 additions & 0 deletions BPG/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import math
import copy
import warnings
import importlib
import yaml

# bag imports
import bag.io
Expand Down Expand Up @@ -990,3 +992,53 @@ def new_template_with(self, angle=0.0, **kwargs):
**kwargs
)



def new_template_with_spec_file(self,
path_to_yaml,
**kwargs):
"""
Create a new template from the spec file

This method will load the spec file, reads the layout class and parameters, then create a new template with those.
It can also update the parameter values if appropriately given as kwargs.
The procedure is useful in the context of reusing the large spec file, enabling the device design choices to be kept within a few yaml files and thus hierarchically managed.
Written as a simple wrapper method for new_template

Parameters
----------
path_to_yaml : string
(absolute or relative from run directory) path to yaml spec file
kwargs : dict
a dictionary of new parameter values

Returns
-------
master : PhotonicTemplateBase
Newly created master from the given spec file
"""
# yaml-written spec file load
with open( path_to_yaml ) as yaml_file:
yaml_file_load = yaml.load(yaml_file)

# auto type-in the template class
photonic_module = importlib.import_module(yaml_file_load['layout_package'])
photonic_class = yaml_file_load['layout_class']
temp_cls = getattr(photonic_module, photonic_class)

# Create a new parameter dictionary based on the provided changes
new_params = copy.deepcopy(yaml_file_load['layout_params'])
for key, val in kwargs.items():
if key in new_params:
new_params[key] = val

# TODO: Handling exceptions
# 1. If module, class not found, show what file is being broken
# 2. when kwargs is not empty but only have wrong keys, raise Error
# Not necessary, most are automatically handled by new_template method

return TemplateBase.new_template(self,
params=new_params,
temp_cls=temp_cls,
**kwargs
)