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

Add unit tests and run them in the CI action #25

Merged
merged 18 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions .github/workflows/run_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI tests

on: [push]

jobs:
build-linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: '3.10'
- name: Prepare mamba installation
env:
PYTHON_VERSION: '3.10'
run: |
# setup correct python version
sed -i -e "s/- python=.*/- python=$PYTHON_VERSION/g" environment.yml

- name: Mamba setup
uses: mamba-org/setup-micromamba@v1
with:
environment-file: environment.yml
cache-downloads: true

- name: Install dependencies
run: |
python --version
echo "Installing additional pip packages"

echo "pip install ."
pip install .

pip install pytest

- name: Test with pytest
run: |
pytest
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ dmypy.json

# Pyre type checker
.pyre/

# Ruff
output_ruff.txt
cpozogonzalez marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.8
hooks:
- id: ruff
name: ruff linting
args: ["--config=pyproject.toml"]
- id : ruff-format
name: ruff formatting
args: ["--config=pyproject.toml"]
4 changes: 2 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ dependencies:
- zstandard
- conda-forge::eventio>=1.9.1,<2.0.0a0
- pip:
- lstchain~=0.10.6
- lstchain==0.10.6
- pint-pulsar<=0.9.7
- more-itertools
- more-itertools==10.4.0
10 changes: 5 additions & 5 deletions ptiming_ana/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from . import phaseogram
from .version import __version__

__all__=[
"cphase",
"phaseogram",
"__version__",
"spectral",
__all__ = [
"cphase",
"phaseogram",
"__version__",
"spectral",
]
2 changes: 1 addition & 1 deletion ptiming_ana/_dev_version/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Try to use setuptools_scm to get the current version; this is only used
# Try to use setuptools_scm to get the current version; this is only used
# in development installations from the git repository.
# see lstchain/version.py for details
try:
Expand Down
5 changes: 3 additions & 2 deletions ptiming_ana/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Tuple, Union

cpozogonzalez marked this conversation as resolved.
Show resolved Hide resolved
VERSION_TUPLE = Tuple[Union[int, str], ...]
else:
VERSION_TUPLE = object
Expand All @@ -12,5 +13,5 @@
__version_tuple__: VERSION_TUPLE
version_tuple: VERSION_TUPLE

__version__ = version = '0.3.dev127+g6d5e09f.d20240620'
__version_tuple__ = version_tuple = (0, 3, 'dev127', 'g6d5e09f.d20240620')
__version__ = version = "0.6.dev4+g14a7d6b.d20240830"
__version_tuple__ = version_tuple = (0, 6, "dev4", "g14a7d6b.d20240830")
21 changes: 11 additions & 10 deletions ptiming_ana/cphase/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from .utils import add_mjd,dl2time_totim,merge_dl2_pulsar, model_fromephem
from .pulsarphase_cal import DL2_calphase,DL3_calphase,fermi_calphase
from .utils import add_mjd, dl2time_totim, merge_dl2_pulsar, model_fromephem
from .pulsarphase_cal import DL2_calphase, DL3_calphase, fermi_calphase


__all__=[
'dl2time_totim',
'model_fromephem',
'add_mjd',
'DL3_calphase',
'DL2_calphase',
'merge_dl2_pulsar',
]
__all__ = [
"dl2time_totim",
"model_fromephem",
"add_mjd",
"DL3_calphase",
"DL2_calphase",
"merge_dl2_pulsar",
"fermi_calphase",
]
164 changes: 111 additions & 53 deletions ptiming_ana/cphase/add_DL2_phase_table.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#####################
###Author: Alvaro Mas Aguilar (alvmas)
#mail: [email protected]
#Using modules from PINT-pulsar and lstchain to calculate phases and add them to the input files in a new table called phase_info
# mail: [email protected]
# Using modules from PINT-pulsar and lstchain to calculate phases and add them to the input files in a new table called phase_info
###################3


'''
"""
Script that adds the information of the phases and corrected times for the pulsar analysis to a DL2 file. It includes this information in a new table called 'phase_info' and optionally a new table with the ource position and theta2 values (called 'source_position')

Parameters:
----------------------
--dir: string
--dir: string
Directory where to find the standard DL3 files

--in-file: string
Expand All @@ -19,110 +19,168 @@
--ephem:string
Path to the ephemeris file (.par or .gro)

--run-number: int
--run-number: int
Run number to process (only if --dir is given)

--include-theta: boolean
True if want to add the source position and theta2 values on the DL2 files (in a table called 'source_position')

--interpolation: boolean
--interpolation: boolean
Set to True if want to use the interpolation method (faster but loses some precision)

--number-interpolation: int
Number of events between two interpolation points.


Usage:
------------------------
1. An example of usage for a given file is:
python add_DL2_phase_table.py
--in-file ./DL2_directory/dl2_LST-1.Run0000.fits.gz
python add_DL2_phase_table.py
--in-file ./DL2_directory/dl2_LST-1.Run0000.fits.gz
--ephem model_test.par

2. Another way of calling (if we have a directory and the run number) is:
python add_DL2_phase_table.py
python add_DL2_phase_table.py
--dir ./DL2_directory/
--run-number 0000
--ephem model_test.par

3. If we want to use the interpolation method:
python add_DL2_phase_table.py
--in_file ./DL2_directory/dl3_LST-1.Run0000.fits.gz
python add_DL2_phase_table.py
--in_file ./DL2_directory/dl3_LST-1.Run0000.fits.gz
--ephem model_test.par
--interpolation

4. If we want to include the source position and the theta2 values:
python add_DL2_phase_table.py
--in_file ./DL2_directory/dl3_LST-1.Run0000.fits.gz
python add_DL2_phase_table.py
--in_file ./DL2_directory/dl3_LST-1.Run0000.fits.gz
--ephem model_test.par
--interpolation
--include-theta

'''
"""

import sys
import pandas as pd
import argparse
import numpy as np
import os
import warnings
from ptiming_ana.cphase.pulsarphase_cal import DL2_calphase
from ptiming_ana.cphase.utils import add_source_info_dl2


def main():
parser = argparse.ArgumentParser()
parser.add_argument('--dir', '-d', action='store',type=str,dest='directory',default=None,help = 'Directory where to find the standard DL3 files (one by one)')
parser.add_argument('--in-file', '-f', action='store',type=str,dest='in_file',default=None,help = 'DL3 file to process (if want to analyze only one file)')
parser.add_argument('--ephem','-ephem',action='store',type=str,dest='ephem',default=None, help = 'Path to the ephemeris file (.par or .gro)')
parser.add_argument('--pickle','-pickle',action='store',type=bool,dest='pickle',default=False, help ='True if pickle, False otherwise')
parser.add_argument('--run-number','-r',action='store',type=str,dest='run',default=False, help = 'Run number to process (only if --dir is given)' )
parser.add_argument('--include-theta','-t',action='store_true',dest='include_theta', help ='True if want to add the source position and theta2 values on the DL2 files (in a table called source_position)')
parser.add_argument('--interpolation','-interp',action='store_true',dest='interpolation',help='Set to True if want to use the interpolation method (faster but loses some precision)')
parser.add_argument('--number-interpolation','-ninterp',action='store',type=int,dest='ninterp',default=1000, help ='Number of events between two interpolation points' )

parser.add_argument(
"--dir",
"-d",
action="store",
type=str,
dest="directory",
default=None,
help="Directory where to find the standard DL3 files (one by one)",
)
parser.add_argument(
"--in-file",
"-f",
action="store",
type=str,
dest="in_file",
default=None,
help="DL3 file to process (if want to analyze only one file)",
)
parser.add_argument(
"--ephem",
"-ephem",
action="store",
type=str,
dest="ephem",
default=None,
help="Path to the ephemeris file (.par or .gro)",
)
parser.add_argument(
"--pickle",
"-pickle",
action="store",
type=bool,
dest="pickle",
default=False,
help="True if pickle, False otherwise",
)
parser.add_argument(
"--run-number",
"-r",
action="store",
type=str,
dest="run",
default=False,
help="Run number to process (only if --dir is given)",
)
parser.add_argument(
"--include-theta",
"-t",
action="store_true",
dest="include_theta",
help="True if want to add the source position and theta2 values on the DL2 files (in a table called source_position)",
)
parser.add_argument(
"--interpolation",
"-interp",
action="store_true",
dest="interpolation",
help="Set to True if want to use the interpolation method (faster but loses some precision)",
)
parser.add_argument(
"--number-interpolation",
"-ninterp",
action="store",
type=int,
dest="ninterp",
default=1000,
help="Number of events between two interpolation points",
)

args = parser.parse_args()

ephem=args.ephem
pickle=args.pickle
in_file=args.in_file
run=args.run
include_theta=args.include_theta
interpolation=args.interpolation
ninterp=args.ninterp
dl2_params_lstcam_key='dl2/event/telescope/parameters/LST_LSTCam'
ephem = args.ephem
pickle = args.pickle
in_file = args.in_file
run = args.run
include_theta = args.include_theta
interpolation = args.interpolation
ninterp = args.ninterp

pd.set_option("display.precision", 10)
if ephem is None:
raise ValueError('No ephemeris provided')
raise ValueError("No ephemeris provided")

if args.directory is not None:
if in_file is not None:
raise ValueError('Both directory and file were given, can only use one of them')

filelist=[]
raise ValueError(
"Both directory and file were given, can only use one of them"
)

filelist = []
for x in os.listdir(args.directory):
rel_dir = os.path.relpath(args.directory)
rel_file = os.path.join(rel_dir, x)
if run in rel_file:
filelist.append(rel_file)

filelist.sort()
for i in range(0,len(filelist)):
#Calculate the phases
DL2_calphase(filelist[i],ephem,'lst',interpolation,ninterp,pickle)
for i in range(0, len(filelist)):
# Calculate the phases
DL2_calphase(filelist[i], ephem, "lst", interpolation, ninterp, pickle)
if include_theta:
add_source_info_dl2(filelist[i],'Crab')
add_source_info_dl2(filelist[i], "Crab")

else:
if in_file is not None:
#Calculate the phases
DL2_calphase(in_file,ephem,'lst',interpolation,ninterp,pickle)
# Calculate the phases
DL2_calphase(in_file, ephem, "lst", interpolation, ninterp, pickle)
if include_theta:
add_source_info_dl2(in_file,'Crab')
add_source_info_dl2(in_file, "Crab")
else:
raise ValueError('No input file or directory given')
raise ValueError("No input file or directory given")



if __name__ == "__main__":
main()


Loading
Loading