Repository: cosimoNigro/agnpy

Issue:
d_L parameter in blob definition spoiling older code and examples
Hi @cosimoNigro 
I realized that you added a possibility to set d_L independently  in PR #138 
https://github.com/cosimoNigro/agnpy/blob/b72c94fa5c205284e0e3ee7a37072f3ccfc97c7b/agnpy/emission_regions/blob.py#L56-L58
This is a bit of a nuisance for the old codes that specified blob parameters using position arguments.
What is worse it also makes some inconsistencies, namely the doc string with the list of arguments is not valid:
https://github.com/cosimoNigro/agnpy/blob/b72c94fa5c205284e0e3ee7a37072f3ccfc97c7b/agnpy/emission_regions/blob.py#L30-L33

and it also make the code from the examples not working:
https://agnpy.readthedocs.io/en/latest/synchrotron.html
because delta_D is interpretted as d_L

I see two possibilities:
1) move the extra parameter at the end of the parameter list (I would recommend to do it for all the parameters initialized to None)
or
2) go carefully through the docs and update all the blob definitions to R_b=R_b, z=z, ... etc. convention
I think 1) is much simpler. 

In addition to this the doc string should be also updated.



Comments:

Patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
new file mode 100644
index 0000000..76a69e5
--- /dev/null
+++ b/.pre-commit-config.yaml
@@ -0,0 +1,18 @@
+repos:
+  # https://pycqa.github.io/isort/docs/configuration/black_compatibility.html#integration-with-pre-commit
+  - repo: https://github.com/pycqa/isort
+    rev: 5.12.0
+    hooks:
+      - id: isort
+        args: ["--profile", "black", "--filter-files"]
+  - repo: https://github.com/psf/black
+    rev: 22.6.0
+    hooks:
+      - id: black
+  # https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html?highlight=other%20tools#flake8
+  - repo: https://github.com/PyCQA/flake8
+    rev: 4.0.1
+    hooks:
+      - id: flake8
+        exclude: experiments
+        args: ["--count", "--max-line-length=100", "--extend-ignore=E203,E712,W503"]
diff --git a/agnpy/emission_regions/blob.py b/agnpy/emission_regions/blob.py
index ab3aa0f..c6aec43 100644
--- a/agnpy/emission_regions/blob.py
+++ b/agnpy/emission_regions/blob.py
@@ -48,13 +48,15 @@ class Blob:
         size of the array of electrons Lorentz factors
     gamma_p_size : int
         size of the array of protons Lorentz factors
+    cosmology : :class:`~astropy.cosmology.Cosmology`
+        (optional) cosmology used to convert the redshift in a distance,
+        see https://docs.astropy.org/en/stable/cosmology/index.html
     """
 
     def __init__(
         self,
         R_b=1e16 * u.cm,
         z=0.069,
-        d_L=None,
         delta_D=10,
         Gamma=10,
         B=1 * u.G,
@@ -63,11 +65,12 @@ class Blob:
         xi=1.0,
         gamma_e_size=200,
         gamma_p_size=200,
+        cosmology=None
     ):
         self.R_b = R_b.to("cm")
         self.z = z
         # if the luminosity distance is not specified, it will be computed from z
-        self.d_L = Distance(z=self.z).cgs if d_L is None else d_L
+        self.d_L = Distance(z=self.z, cosmology=cosmology).cgs
         self.delta_D = delta_D
         self.Gamma = Gamma
         self.B = B
diff --git a/environment.yml b/environment.yml
index ae24b1d..7a54a17 100644
--- a/environment.yml
+++ b/environment.yml
@@ -13,7 +13,7 @@ dependencies:
   - pyyaml # needed to read astropy ecsv file
   - matplotlib
   - sherpa
+  - pre-commit
   - pip:
     - agnpy
     - gammapy
-


Test patch:
diff --git a/agnpy/tests/test_emission_regions.py b/agnpy/tests/test_emission_regions.py
index c9e6d64..bc7d43e 100644
--- a/agnpy/tests/test_emission_regions.py
+++ b/agnpy/tests/test_emission_regions.py
@@ -2,6 +2,7 @@
 import numpy as np
 import astropy.units as u
 from astropy.constants import m_e, m_p
+from astropy.cosmology import Planck18
 import pytest
 from agnpy.spectra import PowerLaw, BrokenPowerLaw
 from agnpy.emission_regions import Blob
@@ -39,9 +40,9 @@ class TestBlob:
         # - automatically set d_L
         blob = Blob(z=2.1)
         assert u.isclose(blob.d_L, 5.21497473e28 * u.cm, atol=0 * u.cm, rtol=1e-3)
-        # - set a different d_L, not computed from z
-        d_L = 1.5e27 * u.cm
-        blob = Blob(z=0.1, d_L=d_L)
+        # - change cosmology
+        blob = Blob(z=0.1, cosmology=Planck18)
+        d_L = 1.4682341e+27 * u.cm
         assert u.isclose(blob.d_L, d_L, atol=0 * u.cm, rtol=1e-5)
 
     def test_particles_spectra(self):