Skip to content

Commit

Permalink
rapyuta v2.2.1 on PyPI
Browse files Browse the repository at this point in the history
  • Loading branch information
kxxdhdn committed Jul 13, 2022
1 parent 872a8b1 commit 347248b
Show file tree
Hide file tree
Showing 27 changed files with 14,314 additions and 22 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2021, Dangning HU
Copyright (c) 2022, Dangning HU
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
7 changes: 7 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ For the moment, there is only built-in documentation available, which can be cal
** Contact
[email protected]
* Version log
** v2.2.1 (20220616)
- ~plots.plotool~
+ Fixed labels for ~reset_handles~
+ Added ~transData2Axes~ series
+ Added extra loc (~locext~) for ~set_legend~
- Added ~maths.icorr2ij~ and ~maths.ij2icorr~
- Updated ~tests/use_cases/~
** v2.2 (20220220)
- ~plots.plotool~
+ Added tick label formatter 'mylog' (opitimized for <1 case)
Expand Down
2 changes: 1 addition & 1 deletion rapyuta/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2021, Dangning HU
Copyright (c) 2022, Dangning HU
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion rapyuta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
print(" * ¨^ .~ = ~^' _-")
print(" ~ °\n")
# print("\n Author: D. HU")
print(" Version 2.2 (20220220)")
print(" Version 2.2.1 (20220616)")
print("\n")


Expand Down
77 changes: 76 additions & 1 deletion rapyuta/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
Maths
f_lin, f_lin0, f_lin1, gaussian, gaussian2D,
rms, nanrms, std, nanstd, nanavg, bsplinterpol
rms, nanrms, std, nanstd, nanavg,
icorr2ij, ij2icorr,
bsplinterpol
"""

import math
import numpy as np
import scipy.interpolate as interpolate
import warnings

## Local
from utilities import InputError


def f_lin(x, A, B):
'''
Expand Down Expand Up @@ -158,6 +165,74 @@ def nanavg(a, axis=None, weights=None, MaskedValue=np.nan):

return avg

def icorr2ij(Npar, Ncorr=None, upper=True):
'''
Get the pair of parameter indices and their correlation coefficient index
------ INPUT ------
Npar Number of parameters
Ncorr Number of correlation coefficients (Default: None)
upper Upper or lower triangles (Default: True)
------ OUTPUT ------
ij Indices with a dimension of (Ncorr,2)
'''
if Ncorr is None:
# Ncorr = math.comb(Npar,2) # Python v3.8+
Ncorr = int( Npar*(Npar-1)/2 )
ij = np.empty((Ncorr,2), dtype=int)
icorr = 0
for i in range(Npar):
for j in range(Npar):
if upper:
if j>i:
ij[icorr,:] = [i+1,j+1]
icorr += 1
else:
if i>j:
ij[icorr,:] = [j+1,i+1]
icorr += 1

return ij

def ij2icorr(i, j, Npar, verbose=False):
'''
Get the correlation coefficient index given the pair of parameter indices
------ INPUT ------
i Index of the first parameter (>= 1)
j Index of the second parameter (>= 1)
Npar Number of parameters
verbose Default: False
------ OUTPUT ------
icorr Indices of correlation coefficient
'''
## (i-1)*N-(i+1)C2+j
try:
icorr = 0
for ii in range(Npar):
for jj in range(Npar):
if j>i:
if verbose:
print('Upper triangle detected.')
if jj>ii:
icorr += 1
if ii==i-1 and jj==j-1:
raise StopIteration
elif i>j:
if verbose:
print('Lower triangle detected.')
if ii>jj:
icorr += 1
if ii==i-1 and jj==j-1:
raise StopIteration
else:
warnings.warn('<maths.ij2icorr> i equals to j! icorr = 0')
raise StopIteration

except StopIteration:

return icorr

def bsplinterpol(x, y, x0):
'''
Monte-Carlo propagated error calculator
Expand Down
131 changes: 126 additions & 5 deletions rapyuta/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
plotool:
set_clib, set_fig, set_ax,
reset_handles, append_handles, get_handles, set_legend,
plot, eplot, save, show, close
plot, eplot, save, show, close,
transData2Axes, transData2Figure, transAxes2Data, transAxes2Figure,
pplot(plotool):
add_plot, add_legend
Expand Down Expand Up @@ -518,6 +519,7 @@ def reset_handles(self):
elif self.trans=='Axes' or self.trans=='Data':
self.ax.add_artist(self.legend)
self.handles = []
self.labels = []
self.horder = 0

return self.handles
Expand Down Expand Up @@ -553,7 +555,7 @@ def get_handles(self):

def set_legend(self, subpos=None,
left=None, right=None, bottom=None, top=None, figtight=False,
handles=None, **kwargs):
handles=None, loc=None, locext=0, **kwargs):
'''
- bbox_to_anchor rules: (1,1) correspond to upper right of the axis
Expand All @@ -579,10 +581,21 @@ def set_legend(self, subpos=None,
shrinkx,shrinky current Axes size (Default: 1)
figtight ignore Axes settings (Default: False)
handles Default: None - self.handles
loc relative position of legend box (Default: None)
'extra upper left' - extend axis limits to add legend
'extra center left'
'extra lower left'
'extra upper right'
'extra center right'
'extra lower right'
'extra upper center'
'extra lower center'
locext percentage of the axis limit length extension (Default: +0%)
- positive: left/right
- negtive: upper/lower
self.fig/ax.legend(**kwargs):
title title of legend box
loc relative position of legend box
bbox_to_anchor reference point of legend box
bbox_transform reference frame of coordinates
self.fig.transFigure
Expand All @@ -593,11 +606,63 @@ def set_legend(self, subpos=None,
if handles is None:
handles = self.handles

## Extra loc
xmin, xmax = self.ax.get_xlim()
ymin, ymax = self.ax.get_ylim()
if loc=='extra upper left':
loc = 'upper left'
## if locext==0: trivial
if locext>0:
self.ax.set_xlim( left=xmin-locext*(xmax-xmin) )
elif locext<0:
self.ax.set_ylim( top=ymax-locext*(ymax-ymin) )
elif loc=='extra upper center':
loc = 'upper center'
## if locext>=0: trivial
if locext<0:
self.ax.set_ylim( top=ymax-locext*(ymax-ymin) )
elif loc=='extra upper right':
loc = 'upper right'
## if locext==0: trivial
if locext>0:
self.ax.set_xlim( right=xmax+locext*(xmax-xmin) )
elif locext<0:
self.ax.set_ylim( top=ymax-locext*(ymax-ymin) )
elif loc=='extra lower left':
loc = 'lower left'
## if locext==0: trivial
if locext>0:
self.ax.set_xlim( left=xmin-locext*(xmax-xmin) )
elif locext<0:
self.ax.set_ylim( bottom=ymin+locext*(ymax-ymin) )
elif loc=='extra lower center':
loc = 'lower center'
## if locext>=0: trivial
if locext<0:
self.ax.set_ylim( bottom=ymin+locext*(ymax-ymin) )
elif loc=='extra lower right':
loc = 'lower right'
## if locext==0: trivial
if locext>0:
self.ax.set_xlim( right=xmax+locext*(xmax-xmin) )
elif locext<0:
self.ax.set_ylim( bottom=ymin+locext*(ymax-ymin) )
elif loc=='extra center left':
loc = 'center left'
## if locext<=0: trivial
if locext>0:
self.ax.set_xlim( left=xmin-locext*(xmax-xmin) )
elif loc=='extra center right':
loc = 'center right'
## if locext<=0: trivial
if locext>0:
self.ax.set_xlim( left=xmax+locext*(xmax-xmin) )

if self.trans=='Figure':
self.fig.subplots_adjust(left=left, right=right,
bottom=bottom, top=top)

self.legend = self.fig.legend(handles=handles, **kwargs)
self.legend = self.fig.legend(handles=handles, loc=loc, **kwargs)
elif self.trans=='Axes' or self.trans=='Data':
if self.nrows!=1 or self.ncols!=1:
if subpos is not None:
Expand All @@ -624,7 +689,7 @@ def set_legend(self, subpos=None,
y1 = bbox.y0 + top*(bbox.y1-bbox.y0)
self.ax.set_position([x0, y0, x1-x0, y1-y0])

self.legend = self.ax.legend(handles=handles, **kwargs)
self.legend = self.ax.legend(handles=handles, loc=loc, **kwargs)
else:
raise InputError('<plotool.set_legend>',
'Non-recognized transformation!')
Expand Down Expand Up @@ -921,6 +986,62 @@ def close(self):

plt.close(self.figid)

def transData2Axes(self, ptData=(0,0)):
'''
Transform the coordinates of a point from transData to transAxes
------ INPUT ------
ptData point in transData (Default: (0,0))
------ OUTPUT ------
ptAxes point in transAxes
'''
ptAxes = self.ax.transData.transform(ptData)
ptAxes = self.ax.transAxes.inverted().transform(ptAxes)

return ptAxes

def transData2Figure(self, ptData=(0,0)):
'''
Transform the coordinates of a point from transData to transFigure
------ INPUT ------
ptData point in transData (Default: (0,0))
------ OUTPUT ------
ptFig point in transFigure
'''
ptFig = self.ax.transData.transform(ptData)
ptFig = self.fig.transFigure.inverted().transform(ptFig)

return ptFig

def transAxes2Data(self, ptAxes=(0,0)):
'''
Transform the coordinates of a point from transAxes to transData
------ INPUT ------
ptAxes point in transAxes (Default: (0,0))
------ OUTPUT ------
ptData point in transData
'''
ptData = self.ax.transAxes.transform(ptAxes)
ptData = self.ax.transData.inverted().transform(ptData)

return ptData

def transAxes2Figure(self, ptAxes=(0,0)):
'''
Transform the coordinates of a point from transAxes to transFigure
------ INPUT ------
ptAxes point in transAxes (Default: (0,0))
------ OUTPUT ------
ptFig point in transFigure
'''
ptFig = self.ax.transAxes.transform(ptAxes)
ptFig = self.fig.transFigure.inverted().transform(ptFig)

return ptFig

class pplot(plotool):
'''
Uni-frame plot (1 row * 1 col)
Expand Down
9 changes: 8 additions & 1 deletion rapyuta/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Error
InputError(Error)
merge_aliases, is_different_value
"""

Expand Down Expand Up @@ -47,7 +48,13 @@ def merge_aliases(default, **kwargs):
return d.popitem()[1]
else:
return default


## The guru way to stop a for loop
## https://stackoverflow.com/questions/6346492/how-to-stop-a-for-loop
# def is_different_value(iterable, i, j):
# value = iterable[i][j]
# return any(any((cell != value for cell in col)) for col in iterable)

## Tests
if __name__ == '__main__':

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name = 'rapyuta',
version = '2.2',
version = '2.2.1',
author = 'D. HU',
author_email = '[email protected]',
description = 'libraRy of Astronomical PYthon UTilities for Astrophysics nerds',
Expand Down
Loading

0 comments on commit 347248b

Please sign in to comment.