Skip to content

Commit

Permalink
Merge pull request #13 from cslotboom/customFuncs
Browse files Browse the repository at this point in the history
Custom funcs
  • Loading branch information
cslotboom authored Jan 25, 2021
2 parents 8440778 + 42f51ae commit 6939a24
Show file tree
Hide file tree
Showing 48 changed files with 219 additions and 20 deletions.
4 changes: 4 additions & 0 deletions doc/readthedocs/hysteresis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
from .climate import SeasonalCurve
from .envelope import getBackboneCurve, getAvgBackbone, fitEEEP

# from .env import HYSTERESIS_ENVIRONMENT
# environment = HYSTERESIS_ENVIRONMENT()

from .env import environment
environment.restart()
15 changes: 9 additions & 6 deletions doc/readthedocs/hysteresis/baseClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .defaultDataFuncs import defaultAreaFunction, defaultSlopeFunction
from .defaultPlotFuncs import initializeFig, defaultPlotFunction, defaultShowCycles
import hysteresis.env as env
import matplotlib.pyplot as plt


Expand Down Expand Up @@ -68,9 +69,7 @@ class CurveBase:
"""

def __init__(self, XYData, fArea = defaultAreaFunction,
fslope = defaultSlopeFunction,
fplot = defaultPlotFunction, xunit = '', yunit = ''):
def __init__(self, XYData, xunit = '', yunit = ''):
"""
Parameters
Expand All @@ -94,9 +93,13 @@ def __init__(self, XYData, fArea = defaultAreaFunction,
"""
self.xy = XYData
self.Npoints = len(XYData[:,0])
self.AreaFunction = fArea
self.slopefunction = fslope
self.plotfunction = fplot
# self.AreaFunction = fArea
# self.slopefunction = fslope
# self.plotfunction = fplot

self.AreaFunction = env.environment.fArea
self.slopefunction = env.environment.fslope
self.plotfunction = env.environment.fplot

self.colorDict = {0:'C0', 1:'C1', 2:'C3'}

Expand Down
8 changes: 6 additions & 2 deletions doc/readthedocs/hysteresis/compare.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


import hysteresis.env as env
from .defaultDataFuncs import defaultSampleFunction, defaultCombineDiff
from .resample import resample

Expand Down Expand Up @@ -33,7 +33,7 @@ def compareCycle(Curve1, Curve2, Nsample = 10,
The net difference between each curve.
"""

sampleFunction = env.environment.fSample


if Curve1.Npoints != Curve1.Npoints:
Expand Down Expand Up @@ -74,6 +74,10 @@ def compareHys(Hys1, Hys2, combineDiff = defaultCombineDiff):
"""

combineDiff = env.environment.fCombineDiff



if Hys1.NCycles != Hys2.NCycles:
raise Exception("Hysteresis don't have a similar number of Cycles.")
# Check both hystesis have the same number of reversals
Expand Down
5 changes: 3 additions & 2 deletions doc/readthedocs/hysteresis/defaultPlotFuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ def defaultPlotFunction(self, x, y, plotCycles, plotPeaks, labelCycles = []):

# fig, ax = initializeFig(xlim, ylim)

line1 = plt.plot(x, y)
line = plt.plot(x, y)

defaultShowCycles(self, x, y, plotCycles, plotPeaks, labelCycles)


return line



Expand Down
38 changes: 38 additions & 0 deletions doc/readthedocs/hysteresis/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

from .defaultDataFuncs import (defaultAreaFunction, defaultSlopeFunction,
defaultSampleFunction, defaultCombineDiff)

from .defaultPlotFuncs import initializeFig, defaultPlotFunction, defaultShowCycles



# =============================================================================
# Curve objects
# =============================================================================



class HYSTERESIS_ENVIRONMENT:

def __init__(self):
"""
Contains the standard functions needed by the hysteresis analysis.
"""

self.fslope = defaultSlopeFunction
self.fArea = defaultAreaFunction

self.finit = initializeFig
self.fplot = defaultPlotFunction
self.fcycles = defaultShowCycles

self.fSample = defaultSampleFunction
self.fCombineDiff = defaultCombineDiff


def restart(self):
self.__init__()


environment = HYSTERESIS_ENVIRONMENT()
# environment.restart()
5 changes: 5 additions & 0 deletions examples/01_Demo.py → examples/01_Demo/01_Demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
An example of very basic usage of the hysteresis package.
"""


import numpy as np
import hysteresis as hys

Expand Down
3 changes: 3 additions & 0 deletions examples/01_Demo/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
An example of very basic usage of the hysteresis package.


File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions examples/02_Damper/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1 align = "Left">Damper 'Clean' Data Example.</h1>

An example of analyzing data from a siesmic damper.
The data from this study is very clean, and little manual effort is needed to accurately find the propreties of the dampers.


File renamed without changes.
6 changes: 6 additions & 0 deletions examples/03_Wall/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1 align = "Left">Wall 'Messy' Data Example.</h1>

An more complex example analyzing data from a shear wall.
The data from this study is very messy, and manual effort is needed to accurately find the propreties of the dampers.


File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions examples/04_Envelope/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1 align = "Left">Curve fitting example.</h1>

An example of finding the backbone curve for data from a siesmic damper.
OpenSees is used to generate input data for the damper.



6 changes: 6 additions & 0 deletions examples/05_CustomFunctions/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1 align = "Left">Custom Functions.</h1>

An example of how to overwrite the default hysteresis functions with custom functions.
In this example the slope function is changed so that it always returns one.


53 changes: 53 additions & 0 deletions examples/05_CustomFunctions/customFunctions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
"""
Many of the Hysteresis default functions can be overwritten by assigning
a new function to the environment object, located at
hys.env.environment
In this example, this feature is showcased by implementing a custom slope
function.
"""
import hysteresis as hys
import numpy as np

# =============================================================================
# Input Data
# =============================================================================
# We create a set of input xy data using numpy, create a hysteresis, then
# plot a figure of our object.

x = np.linspace(0,3,301)
y = x**3 + x**2 + 2
xy = np.column_stack((x,y))
myHys = hys.Hysteresis(xy)

fig, ax = myHys.initFig()
myHys.plot()
myHys.plotSlope()
ax.set_title('Default slope Function')

# =============================================================================
# Define a custom function
# =============================================================================


# define a custom slope function - it makes the slope always 1.
def fslope(xy):
slope = np.ones_like(xy[:,0])
return slope

# Overwrite the slope function in the environment.
hys.env.environment.fslope = fslope

# Plot to show it's worked
myHys = hys.Hysteresis(xy)
fig, ax = myHys.initFig()
myHys.plot()
myHys.plotSlope()


# If we want the behaviour to return to normal, restart the environment.
hys.env.environment.restart()

13 changes: 13 additions & 0 deletions examples/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1 align = "Left">Examples.</h1>


In the following is a list of examples showing how the hysteresis package can be used.
The examples include:

* 01 Demo: a basic demonstration of creating a hysteresis object.
* 02 Damper: an example working with 'clean' input data from a siesmic damper.
* 03 Wall: an example working with 'messy' data from a shear wall damper.
* 04 Envelope: an example fitting a backbone to a cyclic hysteresis.
* 05 CustomFunctions: an example of how to overwrite the default hysteresis functions.


File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
4 changes: 4 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
from .climate import SeasonalCurve
from .envelope import getBackboneCurve, getAvgBackbone, fitEEEP

# from .env import HYSTERESIS_ENVIRONMENT
# environment = HYSTERESIS_ENVIRONMENT()

from .env import environment
environment.restart()
15 changes: 9 additions & 6 deletions src/baseClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .defaultDataFuncs import defaultAreaFunction, defaultSlopeFunction
from .defaultPlotFuncs import initializeFig, defaultPlotFunction, defaultShowCycles
import hysteresis.env as env
import matplotlib.pyplot as plt


Expand Down Expand Up @@ -68,9 +69,7 @@ class CurveBase:
"""

def __init__(self, XYData, fArea = defaultAreaFunction,
fslope = defaultSlopeFunction,
fplot = defaultPlotFunction, xunit = '', yunit = ''):
def __init__(self, XYData, xunit = '', yunit = ''):
"""
Parameters
Expand All @@ -94,9 +93,13 @@ def __init__(self, XYData, fArea = defaultAreaFunction,
"""
self.xy = XYData
self.Npoints = len(XYData[:,0])
self.AreaFunction = fArea
self.slopefunction = fslope
self.plotfunction = fplot
# self.AreaFunction = fArea
# self.slopefunction = fslope
# self.plotfunction = fplot

self.AreaFunction = env.environment.fArea
self.slopefunction = env.environment.fslope
self.plotfunction = env.environment.fplot

self.colorDict = {0:'C0', 1:'C1', 2:'C3'}

Expand Down
8 changes: 6 additions & 2 deletions src/compare.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


import hysteresis.env as env
from .defaultDataFuncs import defaultSampleFunction, defaultCombineDiff
from .resample import resample

Expand Down Expand Up @@ -33,7 +33,7 @@ def compareCycle(Curve1, Curve2, Nsample = 10,
The net difference between each curve.
"""

sampleFunction = env.environment.fSample


if Curve1.Npoints != Curve1.Npoints:
Expand Down Expand Up @@ -74,6 +74,10 @@ def compareHys(Hys1, Hys2, combineDiff = defaultCombineDiff):
"""

combineDiff = env.environment.fCombineDiff



if Hys1.NCycles != Hys2.NCycles:
raise Exception("Hysteresis don't have a similar number of Cycles.")
# Check both hystesis have the same number of reversals
Expand Down
5 changes: 3 additions & 2 deletions src/defaultPlotFuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ def defaultPlotFunction(self, x, y, plotCycles, plotPeaks, labelCycles = []):

# fig, ax = initializeFig(xlim, ylim)

line1 = plt.plot(x, y)
line = plt.plot(x, y)

defaultShowCycles(self, x, y, plotCycles, plotPeaks, labelCycles)


return line



Expand Down
38 changes: 38 additions & 0 deletions src/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

from .defaultDataFuncs import (defaultAreaFunction, defaultSlopeFunction,
defaultSampleFunction, defaultCombineDiff)

from .defaultPlotFuncs import initializeFig, defaultPlotFunction, defaultShowCycles



# =============================================================================
# Curve objects
# =============================================================================



class HYSTERESIS_ENVIRONMENT:

def __init__(self):
"""
Contains the standard functions needed by the hysteresis analysis.
"""

self.fslope = defaultSlopeFunction
self.fArea = defaultAreaFunction

self.finit = initializeFig
self.fplot = defaultPlotFunction
self.fcycles = defaultShowCycles

self.fSample = defaultSampleFunction
self.fCombineDiff = defaultCombineDiff


def restart(self):
self.__init__()


environment = HYSTERESIS_ENVIRONMENT()
# environment.restart()

0 comments on commit 6939a24

Please sign in to comment.