-
Notifications
You must be signed in to change notification settings - Fork 50
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
Fitter improvement #198
Fitter improvement #198
Conversation
tests/test_fit_url.py
Outdated
import numpy as np | ||
import sys | ||
|
||
sys.path.append("../tidy3d_client_revamp") |
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.
we shouldn't have this ideally. In tests/init.py we have the importing of the tidy3d package.
https://github.com/flexcompute/tidy3d/blob/develop/tests/__init__.py#L3
so can you see if removing this line still works? also the name of the repo has changed.
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.
It is now removed, and it is still working.
tidy3d/plugins/dispersion/fit.py
Outdated
self.n_data = self.n_data[ind_final] | ||
self.k_data = self.k_data[ind_final] | ||
|
||
if np.all(ind_final) is False: |
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.
I think we can do if not np.all(...)
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.
revised
The beginning of wavelength range. Unit: micron | ||
wvl_max : float | ||
The end of wavelength range. Unit: micron | ||
""" |
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.
what does this return?
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.
removed
tidy3d/plugins/dispersion/fit.py
Outdated
log.error("No data within [wvl_min,wvl_max]") | ||
return | ||
|
||
self.__init__(self.wvl_um, self.n_data, self.k_data) |
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.
is this supposed to return? I'm confused
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.
I suppose it just updates wvl_um, n_data, etc., but doesn't return anything.
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.
aren't the values updated above? is the call to init needed?
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 are several others to be updated, such as frequency_range, eps_data, etc.
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.
removed
def validate_url_load(data_load: List): | ||
"""Validate if the loaded data from URL is valid | ||
The data list should be in this format: | ||
[["wl", "n"], |
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.
can we see whether this looks ok in the docs?
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.
in the from_url, now I format it as example
tidy3d/plugins/dispersion/fit.py
Outdated
url_file : str | ||
Url link to the csv file. | ||
e.g. "https://refractiveindex.info/data_csv.php?datafile=data/main/Ag/Johnson.yml" | ||
delimiter : str, optional |
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.
instead of str, optional
, we do str = ","
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.
resolved
tidy3d/plugins/dispersion/fit.py
Outdated
cls.validate_url_load(data_url) | ||
except ValidationError as e: # pylint:disable=unused-variable | ||
# log.error(e) | ||
return obj_failed |
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.
what is this case? maybe we should just raise a Tidy3D exception from e?
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.
now all they are all handled by Tidy3D exception
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.
see comments, also let's put some lines in the CHANGELOG.md explaining the new features.
b6e470b
to
c34ed59
Compare
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.
Let's do the final things to this and then wrap it up:
- Make these 6 functions at the beginning of the file (
_unpack_complex
, etc) into@staticmethod
inDispersionFitter
, change how they are called inside the code to addself.
where necessary. - Change
DispersionFitter
to apydantic.BaseModel
object. This will be good practice.
a. wvl_um, n_data, k_data will be defined usingpydantic.Field()
. Can remove init method and docstring entirely.
b._validate_data
can be written as apydantic.validator
(see other examples in code).
c.k_data
can also be handled using apydantic.validator
that ha the side effect of setting k_data if it's None.
d. Makeself.eps_data
,self.lossy
,self.freqs
,self.frequency_range
into@property
types. Then if things are changed (someone sets the wvl_range, or changes k_data), these things will be updated.
I think this will be a few changes but will ultimately be much cleaner and I think it will be ready after this. Also it will get documented fine.
nlopt_maxeval : PositiveInt, optional | ||
Maxeval in each optimization | ||
advanced_param : AdvancedFitterParam, optional | ||
Other advanced parameters. |
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.
let's format this like
:class:`AdvancedFitterParam`
so it links in the docs.
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.
All revised now.
tidy3d/plugins/dispersion/fit_web.py
Outdated
try: | ||
resp.raise_for_status() | ||
except Exception as e: | ||
log.error(e) |
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.
we can remove this, when you raise a WebError it will log as well
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.
It has been updated.
tidy3d/plugins/dispersion/fit_web.py
Outdated
log.error(e) | ||
raise WebError( # pylint:disable=raise-missing-from | ||
"Authorization to the server failed. Please try again." | ||
) |
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.
we can raise WebError(...) from e
to include the original exception.
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.
revised.
2c40af8
to
058cc88
Compare
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.
A few minor comments and we can merge PR
if __name__ == "__main__": | ||
test_dispersion_load_file() |
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.
let's delete this entire code block unless we need it.
tests/_test_fit_web.py
Outdated
num_poles = 2 | ||
num_tries = 50 | ||
tolerance_rms = 1e-3 | ||
# best_medium, best_rms = fitter.fit( |
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.
remove comments unless needed.
from .fit import DispersionFitter | ||
|
||
|
||
class FitterData(BaseModel): | ||
class AdvancedFitterParam(BaseModel): | ||
|
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.
hm, probably shouldn't be a space here? Does black not re-format this?
tidy3d/plugins/dispersion/fit_web.py
Outdated
"hard", | ||
title="Type of constraint for stability", | ||
description="Stability constraint that is hard but fast to compute, " | ||
"or soft but slightly slower to compute.", |
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.
when would user want "soft"? Could you explain just a little more? Seems like soft is less stable and slower to compute? but more likely to return a fit?
tidy3d/plugins/dispersion/fit_web.py
Outdated
nlopt_maxeval: PositiveInt = Field( | ||
5000, | ||
title="Number of inner iterations", | ||
description="Number of iterations in each inner optimization", |
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.
period at end of description
tidy3d/plugins/dispersion/fit_web.py
Outdated
Other advanced parameters. | ||
|
||
Returns | ||
------- | ||
Tuple[``PoleResidue``, float] |
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.
:class:`PoleResiude`
tidy3d/plugins/dispersion/fit_web.py
Outdated
_url = self._set_url("default") | ||
# set up bound_f, bound_amp | ||
if advanced_param.bound_f is None: | ||
advanced_param.bound_f = self.frequency_range[1] * 10 |
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.
instead of hardcoding this 10
, let's make it either a constant BOUND_MAX_FACTOR = 10
defined somewhere or an argument to this function if it makes more sense.
tidy3d/plugins/dispersion/fit_web.py
Outdated
if advanced_param.bound_f is None: | ||
advanced_param.bound_f = self.frequency_range[1] * 10 | ||
if advanced_param.bound_amp is None: | ||
advanced_param.bound_amp = self.frequency_range[1] * 10 |
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.
see comment above
Change DispersionFitter to a pydantic.BaseModel object. Make eps_data, lossy, freqs, frequency_range into @Property types More error handling in StableDispersiveFitter Allow to load dispersive data directly by providing URL to txt or csv file Allow to filter wavelength range for fitting Add tunable eps_inf as a variable for optimization
058cc88
to
74c5eb6
Compare
All the above comments have been addressed in this commit |
More error handling in StableDispersiveFitter
Allow to load dispersive data directly by providing URL to txt or csv file
Add tunable eps_inf as a variable for optimization
Allow to set frequency range for fitting