Skip to content

Commit

Permalink
Merge pull request #2 from AlvaroEzq/add_pre-commit
Browse files Browse the repository at this point in the history
Add pre commit and tests
  • Loading branch information
AlvaroEzq authored Jan 25, 2024
2 parents e0f006b + 15c2c63 commit 1ba2527
Show file tree
Hide file tree
Showing 14 changed files with 396 additions and 163 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "Build and Test"

on:
workflow_dispatch:
pull_request:
push:
branches: [ "main" ]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build-test:
strategy:
fail-fast: false
matrix:
platform: [ ubuntu-latest, windows-latest, macos-latest ]
python-version: [ "3.8", "3.9", "3.10", "3.11" ]

runs-on: ${{ matrix.platform }}
timeout-minutes: 20

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install pre-commit
run: pip install pre-commit

- name: Run pre-commit
run: pre-commit run --all-files

#- name: Install the package with pip
# run: python -m pip install .[test]
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# IDE
.idea

# Python compilation files
__pycache__/
*.py[cod]
*$py.class

# Jupyter Notebook
.ipynb_checkpoints

*.pickle
.DS_Store

# Plots
/plots/*
!/plots/*.pdf
!/plots/*.png
/plots/testing*
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: check-toml
- id: requirements-txt-fixer
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black

exclude: |
(?x)^(
data/.*|
plots/.*|
Javat/plots/.*|
databases/.*|
buildDataBase.py
)$
187 changes: 119 additions & 68 deletions AxionPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
from XPlotter import BasePlot, ExPltItem
import DataBaseClass as db


def extract_kwargs(arguments_str):
"""Extracts kwargs from a string of arguments.
"""Extracts kwargs from a string of arguments.
Example: extract_kwargs("a=1, b=2, c=3") returns {'a':1, 'b':2, 'c':3}
extract_kwargs("a=1, b="red", c=3,") returns {'a':1, 'b':'red', 'c':3}"""
kwargs = {}
for arg in arguments_str.split(','):
if arg.strip() != '':
key, value = arg.split('=')
for arg in arguments_str.split(","):
if arg.strip() != "":
key, value = arg.split("=")
kwargs[key.strip()] = eval(value.strip())
return kwargs


# ==============================================================================#
# renormalize data, to plot C_ag instead of g_ag
#
Expand All @@ -24,23 +26,45 @@ def RenormItem(item):
# print(item.data[i,0],item.data[i,1])
# C_ag = g_ag / m_a * 5.172e9

class AxionGagPlot:

ListOfPlotTypes = {'large_panorama', 'panorama', 'LSWexps', 'haloscopes', 'haloscopes_zoom',
'haloscopes_radeszoom', 'helioscopes'}

def __init__(self,
database : db.DataBaseGag, labels : db.DataBaseLabels, plottype="",
projections=False, showplot=True, saveplotname=None,
figx=None, figy=None, ymin=None, ymax=None, xmin=None, xmax=None,
ticksopt_x=None, ticksopt_y=None,
labelx='$m_a$ (eV)', labely=r'$|g_{a\gamma}|$ (GeV$^{-1}$)'
):
class AxionGagPlot:
ListOfPlotTypes = {
"large_panorama",
"panorama",
"LSWexps",
"haloscopes",
"haloscopes_zoom",
"haloscopes_radeszoom",
"helioscopes",
}

def __init__(
self,
database: db.DataBaseGag,
labels: db.DataBaseLabels,
plottype="",
projections=False,
showplot=True,
saveplotname=None,
figx=None,
figy=None,
ymin=None,
ymax=None,
xmin=None,
xmax=None,
ticksopt_x=None,
ticksopt_y=None,
labelx="$m_a$ (eV)",
labely=r"$|g_{a\gamma}|$ (GeV$^{-1}$)",
):
if plottype not in self.ListOfPlotTypes:
print('Warning: ' + plottype + ' not a known plot type. Using default values and wildType column in database')
print(
"Warning: "
+ plottype
+ " not a known plot type. Using default values and wildType column in database"
)

#default values for the plot (with no specified type)
# default values for the plot (with no specified type)
if (plottype in ["large_panorama"]) or (plottype not in self.ListOfPlotTypes):
if figx is None:
figx = 6.5
Expand All @@ -55,11 +79,11 @@ def __init__(self,
if xmax is None:
xmax = 1e9
if ticksopt_x is None:
ticksopt_x = 'dense'
ticksopt_x = "dense"
if ticksopt_y is None:
ticksopt_y = 'normal'
ticksopt_y = "normal"

#default values for the plot (with specified type)
# default values for the plot (with specified type)
if plottype == "panorama":
if figx is None:
figx = 6.5
Expand All @@ -74,9 +98,9 @@ def __init__(self,
if xmax is None:
xmax = 10
if ticksopt_x is None:
ticksopt_x = 'normal'
ticksopt_x = "normal"
if ticksopt_y is None:
ticksopt_y = 'normal'
ticksopt_y = "normal"

if plottype == "helioscopes":
if figx is None:
Expand All @@ -92,9 +116,9 @@ def __init__(self,
if xmax is None:
xmax = 1
if ticksopt_x is None:
ticksopt_x = 'normal'
ticksopt_x = "normal"
if ticksopt_y is None:
ticksopt_y = 'normal'
ticksopt_y = "normal"

if plottype == "LSWexps":
if figx is None:
Expand All @@ -110,9 +134,9 @@ def __init__(self,
if xmax is None:
xmax = 1e-2
if ticksopt_x is None:
ticksopt_x = 'normal'
ticksopt_x = "normal"
if ticksopt_y is None:
ticksopt_y = 'normal'
ticksopt_y = "normal"

if plottype in ["haloscopes", "haloscopes_zoom", "haloscopes_radeszoom"]:
if figx is None:
Expand All @@ -138,52 +162,61 @@ def __init__(self,
if plottype in ["haloscopes_radeszoom"]:
xmax = 4.5e-5
if ticksopt_x is None:
ticksopt_x = 'normal'
ticksopt_x = "normal"
if ticksopt_y is None:
ticksopt_y = 'normal'
labely = r'$|C_{a\gamma}|\tilde{\rho}_a^{1/2}$'
ticksopt_y = "normal"
labely = r"$|C_{a\gamma}|\tilde{\rho}_a^{1/2}$"

#plot the background
self.axplot = BasePlot(xlab=labelx, ylab=labely,
figsizex=figx, figsizey=figy,
y_min=ymin, y_max=ymax,
x_min=xmin, x_max=xmax,
ticksopt_x=ticksopt_x, ticksopt_y=ticksopt_y)
# plot the background
self.axplot = BasePlot(
xlab=labelx,
ylab=labely,
figsizex=figx,
figsizey=figy,
y_min=ymin,
y_max=ymax,
x_min=xmin,
x_max=xmax,
ticksopt_x=ticksopt_x,
ticksopt_y=ticksopt_y,
)

self.axionDB = database
self.labelsDB = labels
#print(self.axionDB.get_rows())
# print(self.axionDB.get_rows())

#Plotting Data & Labels
# Plotting Data & Labels
self.PlotData(plottype, projections)
self.PlotLabels(projections)

if showplot:
self.axplot.ShowPlot()

if type(saveplotname)==str:
if type(saveplotname) == str:
if len(saveplotname) > 0:
print('saving...')
print("saving...")
self.axplot.SavePlot(saveplotname)
print('done')
print("done")

def PlotData(self, plottype, projections = False):
print("projections = ", projections)
def PlotData(self, plottype, projections=False):
print("projections = ", projections)
if plottype not in self.ListOfPlotTypes:
plottype = "wildType"
if "haloscopes" in plottype:
plottype = "haloscopes" # all haloscopes have the same column assigned ("haloscopes") in the database
plottype = "haloscopes" # all haloscopes have the same column assigned ("haloscopes") in the database

data = self.axionDB.get_rows(f"{plottype}==1")
if projections:
data = data + self.axionDB.get_rows(f"projection==1 AND {plottype}==0") # get projections not already included
data = data + self.axionDB.get_rows(
f"projection==1 AND {plottype}==0"
) # get projections not already included
for row in data:
pltItem = ExPltItem(row[0], row[1], row[2], **extract_kwargs(row[3]))
if "haloscopes" in plottype:
RenormItem(pltItem) # Its done in Haloscopes exclusively
RenormItem(pltItem) # Its done in Haloscopes exclusively
pltItem.DrawItem(self.axplot)

def PlotLabels(self, projections = False):
def PlotLabels(self, projections=False):
labels = self.labelsDB.get_rows(f"onoff==1 AND projection==0")
if projections:
labels = labels + self.labelsDB.get_rows(f"onoff==1 AND projection==1")
Expand All @@ -193,53 +226,71 @@ def PlotLabels(self, projections = False):


class AxionGaePlot:
ListOfPlotTypes = {} # No plottypes for Gae yet

ListOfPlotTypes = {} # No plottypes for Gae yet

def __init__(self,
database : db.DataBaseGae, labels : db.DataBaseLabels, plottype="",
projections=False, showplot=True, saveplotname=None,
figx=6, figy=5, xmin=1.0e-4, xmax=1., ymin=1.0e-13, ymax=1.0e-10, labelfontsize=13,
labelx='$m_a$ (eV)', labely=r'$|g_{ae}g_{a\gamma}|^{1/2}$ (GeV$^{-1/2}$)'
):

self.axplot = BasePlot(xlab=labelx, ylab=labely,
figsizex=figx, figsizey=figy,
y_min=ymin, y_max=ymax,
x_min=xmin, x_max=xmax,
labelfontsize=labelfontsize)
def __init__(
self,
database: db.DataBaseGae,
labels: db.DataBaseLabels,
plottype="",
projections=False,
showplot=True,
saveplotname=None,
figx=6,
figy=5,
xmin=1.0e-4,
xmax=1.0,
ymin=1.0e-13,
ymax=1.0e-10,
labelfontsize=13,
labelx="$m_a$ (eV)",
labely=r"$|g_{ae}g_{a\gamma}|^{1/2}$ (GeV$^{-1/2}$)",
):
self.axplot = BasePlot(
xlab=labelx,
ylab=labely,
figsizex=figx,
figsizey=figy,
y_min=ymin,
y_max=ymax,
x_min=xmin,
x_max=xmax,
labelfontsize=labelfontsize,
)

self.axionDB = database
self.labelsDB = labels
#print(self.axionDB.get_rows())
# print(self.axionDB.get_rows())

self.PlotData(plottype, projections)
self.PlotLabels(projections)
if showplot:
self.axplot.ShowPlot()

if type(saveplotname)==str:
if type(saveplotname) == str:
if len(saveplotname) > 0:
print('saving...')
print("saving...")
self.axplot.SavePlot(saveplotname)
print('done')
print("done")

def PlotData(self, plottype, projections=False):
print("projections = ", projections)
print("projections = ", projections)
if plottype not in self.ListOfPlotTypes:
plottype = "wildType"

data = self.axionDB.get_rows(f"{plottype}==1")
if projections:
data = data + self.axionDB.get_rows(f"projection==1 AND {plottype}==0") # get projections not already included
data = data + self.axionDB.get_rows(
f"projection==1 AND {plottype}==0"
) # get projections not already included
for row in data:
pltItem = ExPltItem(row[0], row[1], row[2], **extract_kwargs(row[3]))
pltItem.DrawItem(self.axplot)

def PlotLabels(self, projections = False):
def PlotLabels(self, projections=False):
labels = self.labelsDB.get_rows(f"onoff==1 AND projection==0")
if projections:
labels = labels + self.labelsDB.get_rows(f"onoff==1 AND projection==1")

for row in labels:
plt.text(row[1], row[2], row[0], **extract_kwargs(row[3]))
Loading

0 comments on commit 1ba2527

Please sign in to comment.