Skip to content

Commit

Permalink
Merge branch 'main' into image_deconvolution
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyoneda committed Jan 31, 2024
2 parents ffc19ef + 36459c7 commit 9cde63a
Show file tree
Hide file tree
Showing 33 changed files with 9,616 additions and 11,466 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: docs

on: [push, pull_request, workflow_dispatch]

permissions:
contents: write

jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
sudo apt install pandoc
pip install sphinx sphinx_rtd_theme nbsphinx mock
- name: Sphinx build
run: |
make -C docs html
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/docs' }}
with:
publish_branch: gh-pages
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: docs/_build/html
force_orphan: true

22 changes: 3 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
# cosipy
The COSI high-level data analysis tools

## Installation from source
The cosipy library is [COSI](https://cosi.ssl.berkeley.edu)'s high-level analysis software.

Meant for developers. Currently the only option.
The main repository is hosted in https://github.com/cositools/cosipy

1. Clone the repository into your local machine
For the cosipy installation and usage instructions please refer to the main [cosipy documentation](https://cositools.github.io/cosipy/).

```
git clone [email protected]:cositools/cosipy.git
```

2. Move to the cosipy folder

```
cd cosipy
```

3. Create a new conda environment. Optional but highly encouranged.

4. Install it:

```
pip install -e .
```
2 changes: 1 addition & 1 deletion cosipy/response/FullDetectorResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ def get_point_source_response(self,
Effective time spent by the source at each pixel location in spacecraft coordinates
coord : :py:class:`astropy.coordinates.SkyCoord`
Source coordinate
scatt_map : :py:cass:`SpacecraftAttitudeMap`
scatt_map : :py:class:`SpacecraftAttitudeMap`
Spacecraft attitude map
Returns
Expand Down
195 changes: 135 additions & 60 deletions cosipy/spacecraftfile/SpacecraftFile.py

Large diffs are not rendered by default.

105 changes: 48 additions & 57 deletions cosipy/threeml/COSILike.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,34 @@
logger = logging.getLogger(__name__)

class COSILike(PluginPrototype):

"""
COSI 3ML plugin.
Parameters
----------
name : str
Plugin name e.g. "cosi". Needs to have a distinct name with respect to other plugins in the same analysis
dr : str
Path to full detector response
data : histpy.Histogram
Binned data. Note: Eventually this should be a cosipy data class
bkg : histpy.Histogram
Binned background model. Note: Eventually this should be a cosipy data class
sc_orientation : cosipy.spacecraftfile.SpacecraftFile
Contains the information of the orientation: timestamps (astropy.Time) and attitudes (scoord.Attitude) that describe
the spacecraft for the duration of the data included in the analysis
nuisance_param : astromodels.core.parameter.Parameter, optional
Background parameter
coordsys : str, optional
Coordinate system ('galactic' or 'spacecraftframe') to perform fit in, which should match coordinate system of data
and background. This only needs to be specified if the binned data and background do not have a coordinate system
attached to them
precomputed_psr_file : str, optional
Full path to precomputed point source response in Galactic coordinates
"""
def __init__(self, name, dr, data, bkg, sc_orientation,
nuisance_param=None, coordsys=None, precomputed_psr_file=None, **kwargs):

"""
COSI 3ML plugin
Parameters
----------
name : str
Plugin name e.g. "cosi". Needs to have a distinct name with respect to other plugins in the same analysis
dr : Path
Path to full detector response
data: histpy.Histogram
Binned data. Note: Eventually this should be a cosipy data class
bkg: histpy.Histogram
Binned background model. Note: Eventually this should be a cosipy data class
sc_orientation: cosipy.spacecraftfile.SpacecraftFile
Contains the information of the orientation: timestamps (astropy.Time) and attitudes (scoord.Attitude) that describe
the spacecraft for the duration of the data included in the analysis
nuisance_param: astromodels.core.parameter.Parameter
Background parameter (optional)
coordsys: str
Coordinate system ('galactic' or 'spacecraftframe') to perform fit in, which should match coordinate system of data
and background. This only needs to be specified if the binned data and background do not have a coordinate system
attached to them
precomputed_psr_file: str
Full path to precomputed point source response in Galactic coordinates (optional)
"""

# create the hash for the nuisance parameters. We have none for now.
self._nuisance_parameters = collections.OrderedDict()

Expand Down Expand Up @@ -121,13 +119,13 @@ def __init__(self, name, dr, data, bkg, sc_orientation,
print("--> done")

def set_model(self, model):

"""
Set the model to be used in the joint minimization.
Parameters
----------
model: astromodels.core.model.Model; any model supported by astromodels
model : astromodels.core.model.Model
Any model supported by astromodels
"""

# Get point sources and extended sources from model:
Expand Down Expand Up @@ -194,7 +192,7 @@ def set_model(self, model):
print("... Calculating point source responses ...")

self._psr = {}
self._source_location = {} # Shoule the poition information be in the point source response? (HY)
self._source_location = {} # Should the poition information be in the point source response? (HY)

for name, source in point_sources.items():
coord = source.position.sky_coord
Expand Down Expand Up @@ -240,20 +238,19 @@ def set_model(self, model):
# Convolve with spectrum
# See also the Detector Response and Source Injector tutorials
spectrum = source.spectrum.main.shape

total_expectation = self._psr[name].get_expectation(spectrum).project(['Em', 'Phi', 'PsiChi'])
# should it be like self._psr[name].get_expectation(spectrum) (without 'project')? (HY)

total_expectation = self._psr[name].get_expectation(spectrum)

# Save expected counts for source:
self._expected_counts[name] = copy.deepcopy(total_expectation)

# Need to check if self._signal type is dense (i.e. 'Quantity') or sparse (i.e. 'COO').
if type(total_expectation.contents) == u.quantity.Quantity:
total_expectation = total_expectation.contents.value
total_expectation = total_expectation.project(['Em', 'Phi', 'PsiChi']).contents.value
elif type(total_expectation.contents) == COO:
total_expectation = total_expectation.contents.todense()
total_expectation = total_expectation.project(['Em', 'Phi', 'PsiChi']).contents.todense()
else:
raise RuntimeError("Expectation is an unknown object")

# Save expected counts for source:
self._expected_counts[name] = copy.deepcopy(total_expectation)

# Add source to signal and update source counter:
if self.src_counter == 0:
Expand All @@ -266,13 +263,13 @@ def set_model(self, model):
self._model = model

def get_log_like(self):

"""
Return the value of the log-likelihood.
Calculate the log-likelihood.
Returns
----------
log_like: float
log_like : float
Value of the log-likelihood
"""

# Recompute the expectation if any parameter in the model changed
Expand Down Expand Up @@ -309,25 +306,25 @@ def get_log_like(self):
return log_like

def inner_fit(self):

"""
This fits nuisance parameters.
Required for 3ML fit.
"""

return self.get_log_like()

def _get_dwell_time_map(self, coord):

"""
Get the dwell time map of the source in the spacecraft frame.
Get the dwell time map of the source in the inertial (spacecraft) frame.
Parameters
----------
coord: astropy.coordinates.SkyCoord; the coordinate of the target source.
coord : astropy.coordinates.SkyCoord
Coordinates of the target source
Returns
-------
dwell_time_map: mhealpy.containers.healpix_map.HealpixMap
dwell_time_map : mhealpy.containers.healpix_map.HealpixMap
Dwell time map
"""

self._sc_orientation.get_target_in_sc_frame(target_name = self._name, target_coord = coord)
Expand All @@ -336,32 +333,26 @@ def _get_dwell_time_map(self, coord):
return dwell_time_map

def _get_scatt_map(self):

"""
Get the spacecraft attitude map of the source in the inertial (spacecraft) frame.
Parameters
----------
nside: int; resolution of scatt map
coordsys: BaseFrameRepresentation or str; coordinate system of map
Returns
-------
scatt_map: cosipy.spacecraftfile.scatt_map.SpacecraftAttitudeMap
scatt_map : cosipy.spacecraftfile.scatt_map.SpacecraftAttitudeMap
"""

scatt_map = self._sc_orientation.get_scatt_map(nside = self._dr.nside * 2, coordsys = 'galactic')

return scatt_map

def set_inner_minimization(self, flag: bool):

"""
Turn on the minimization of the internal COSI parameters.
Turn on the minimization of the internal COSI (nuisance) parameters.
Parameters
----------
flag: bool; turns on and off the minimization of the internal parameters
flag : bool
Turns on and off the minimization of the internal parameters
"""

self._fit_nuisance_params: bool = bool(flag)
Expand Down
2 changes: 1 addition & 1 deletion cosipy/ts_map/fast_norm_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,4 @@ def solve(self, data, bkg, unit_excess):
#Assumed to be a numerical error
ts = 0

return (ts, norm, norm_err, failed)
return (ts, norm, norm_err, failed, iteration)
Loading

0 comments on commit 9cde63a

Please sign in to comment.