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

Refact component type #726

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion docs/examples/PINT_walkthrough.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.2'
jupytext_version: 1.4.1
jupytext_version: 1.4.0
kernelspec:
display_name: Python 3
language: python
Expand Down
16 changes: 8 additions & 8 deletions docs/examples/build_model_from_scratch.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.2'
jupytext_version: 1.4.1
jupytext_version: 1.4.0
kernelspec:
display_name: Python 3
language: python
Expand Down Expand Up @@ -84,7 +84,7 @@ tm = TimingModel("NGC6400E", component_instances)

To view all the components in `TimingModel` instance, we can use the property `.components`, which returns a dictionary (name as the key, component instance as the value).

Internally, the components are stored in a list(ordered list, you will see why this is important below) according to their types. All the delay type of components (subclasses of `DelayComponent` class) are stored in the `DelayComponent_list`, and the phase type of components (subclasses of `PhaseComponent` class) in the `PhaseComponent_list`.
Internally, the components are stored in the `.model_sectors`, a dictionary with the component type name as the key and `ModelSector` subclasses as the value. `ModelSector` class also have the methods to collect results from each component. For instance, `DelaySector` has `delay()` that compute the total delay from all the `DelayComponent` objects and `PhaseSector` has `phase()` for total phase. But these methods can be accessed from the `TimingModel` class.

```python
# print the components in the timing model
Expand All @@ -100,10 +100,10 @@ for (cp_name, cp_instance) in tm.components.items():
* `TimingModel.params()` : List all the parameters in the timing model from all components.
* `TimingModel.setup()` : Setup the components (e.g., register the derivatives).
* `TimingModel.validate()` : Validate the components see if the parameters are setup properly.
* `TimingModel.delay()` : Compute the total delay.
* `TimingModel.phase()` : Compute the total phase.
* `TimingModel.delay_funcs()` : List all the delay functions from all the delay components.
* `TimingModel.phase_funcs()` : List all the phase functions from all the phase components.
* `TimingModel.delay()` : Compute the total delay, if DelayComponent exists.
* `TimingModel.phase()` : Compute the total phase, if PhaseComponent exists.
* `TimingModel.delay_funcs()` : List all the delay functions from all the delay components, if DelayComponent exists.
* `TimingModel.phase_funcs()` : List all the phase functions from all the phase components, if DelayComponent exists.
* `TimingModel.get_component_type()` : Get all the components from one category
* `TimingModel.map_component()` : Get the component location. It returns the component's instance, order in the list, host list and its type.
* `TimingModel.get_params_mapping()` : Report which component each parameter comes from.
Expand Down Expand Up @@ -201,10 +201,10 @@ print("All components in timing model:")
display(tm.components)

print("\n")
print("Delay components in the DelayComponent_list (order matters!):")
print("Delay components in the DelayComponent sector (order matters!):")

# print the delay component order, dispersion should be after the astrometry
display(tm.DelayComponent_list)
display(tm.model_sectors['DelayComponent'].component_list)
```

The DM value can be set as we set the parameters above.
Expand Down
18 changes: 11 additions & 7 deletions docs/examples/understanding_timing_models.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.2'
jupytext_version: 1.4.1
jupytext_version: 1.4.0
kernelspec:
display_name: Python 3
language: python
Expand Down Expand Up @@ -56,12 +56,12 @@ The TimingModel class stores lists of the delay model components and phase compo

```python
# When this list gets printed, it shows the parameters that are associated with each component as well.
m.DelayComponent_list
m.model_sectors['DelayComponent'].component_list
```

```python
# Now let's look at the phase components. These include the absolute phase, the spindown model, and phase jumps
m.PhaseComponent_list
m.model_sectors['PhaseComponent'].component_list
```

We can add a component to an existing model
Expand All @@ -82,7 +82,7 @@ m.add_component(a, validate=False)
```

```python
m.DelayComponent_list # The new instance is added to delay component list
m.model_sectors['DelayComponent'].component_list # The new instance is added to delay component list
```

There are two ways to remove a component from a model. This simplest is to use the `remove_component` method to remove it by name.
Expand Down Expand Up @@ -112,7 +112,7 @@ from_list.remove(component)
```

```python
m.DelayComponent_list # AstrometryEcliptic has been removed from delay list.
m.model_sectors['DelayComponent'].component_list # AstrometryEcliptic has been removed from delay list.
```

To switch the order of a component, just change the order of the component list
Expand All @@ -121,7 +121,7 @@ To switch the order of a component, just change the order of the component list

```python
# Let's look at the order of the components in the delay list first
_ = [print(dc.__class__) for dc in m.DelayComponent_list]
_ = [print(dc.__class__) for dc in m.model_sectors['DelayComponent'].component_list]
```

```python
Expand All @@ -133,7 +133,7 @@ from_list[order], from_list[new_order] = from_list[new_order], from_list[order]

```python
# Print the classes to see the order switch
_ = [print(dc.__class__) for dc in m.DelayComponent_list]
_ = [print(dc.__class__) for dc in m.model_sectors['DelayComponent'].component_list]
```

Delays are always computed in the order of the DelayComponent_list
Expand Down Expand Up @@ -181,3 +181,7 @@ for comp, tp in Component.component_types.items():

special
```

```python
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this for?


```
14 changes: 11 additions & 3 deletions src/pint/fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ def fit_toas(self, maxiter=1, threshold=False, full_cov=False):
accuracy where they both can be applied.
"""
chi2 = 0
has_noise_model = "NoiseComponent" in self.model.component_types
for i in range(max(maxiter, 1)):
fitp = self.get_fitparams()
fitpv = self.get_fitparams_num()
Expand All @@ -758,8 +759,12 @@ def fit_toas(self, maxiter=1, threshold=False, full_cov=False):

# get any noise design matrices and weight vectors
if not full_cov:
Mn = self.model.noise_model_designmatrix(self.toas)
phi = self.model.noise_model_basis_weight(self.toas)
if has_noise_model:
Mn = self.model.noise_model_designmatrix(self.toas)
phi = self.model.noise_model_basis_weight(self.toas)
else:
Mn = None
phi = None
phiinv = np.zeros(M.shape[1])
if Mn is not None and phi is not None:
phiinv = np.concatenate((phiinv, 1 / phi))
Expand Down Expand Up @@ -844,7 +849,10 @@ def fit_toas(self, maxiter=1, threshold=False, full_cov=False):

# Compute the noise realizations if possible
if not full_cov:
noise_dims = self.model.noise_model_dimensions(self.toas)
if has_noise_model:
noise_dims = self.model.noise_model_dimensions(self.toas)
else:
noise_dims = {}
noise_resids = {}
for comp in noise_dims.keys():
p0 = noise_dims[comp][0] + ntmpar
Expand Down
1 change: 1 addition & 0 deletions src/pint/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"StandardTimingModel",
[AstrometryEquatorial(), Spindown(), DispersionDM(), SolarSystemShapiro()],
)

# BTTimingModel = generate_timing_model("BTTimingModel",
# (Astrometry, Spindown, Dispersion, SolarSystemShapiro, BT))
# DDTimingModel = generate_timing_model("DDTimingModel",
Expand Down
Loading