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

Changes in some perturbations from the math to numpy library, and added a ClassInterchange perturbation #5

Merged
merged 4 commits into from
Sep 1, 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
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ jobs:
- { os: windows-latest, py: "3.8" }
- { os: windows-latest, py: "3.9" }
- { os: windows-latest, py: "3.10" }
- { os: macOS-latest, py: "3.7" }
- { os: macOS-latest, py: "3.8" }
- { os: macOS-latest, py: "3.9" }
- { os: macOS-12, py: "3.7" }
- { os: macOS-12, py: "3.8" }
- { os: macOS-12, py: "3.9" }
- { os: ubuntu-latest, py: "3.7" }
- { os: ubuntu-latest, py: "3.8" }
- { os: ubuntu-latest, py: "3.9" }
Expand Down
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/pudu_jon.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 25 additions & 6 deletions pudu/perturbation.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Log:
def __init__(self, base=10):
self.base = base
def apply(self, x, row, col, window, bias):
x[0, row:row+window[0], col:col+window[1], 0] = math.log(x[0, row:row+window[0], col:col+window[1], 0] + bias, self.base)
x[0, row:row+window[0], col:col+window[1], 0] = np.log(x[0, row:row+window[0], col:col+window[1], 0] + bias) / np.log(self.base)
return x, None


Expand Down Expand Up @@ -151,7 +151,7 @@ def __init__(self, freq=30, amp=1):
self.freq = freq
self.amp = amp
def apply(self, x, row, col, window, bias):
x[0, row:row+window[0], col:col+window[1], 0] = (x[0, row:row+window[0], col:col+window[1], 0] + bias) * math.sin(self.freq * x[0, row:row+window[0], col:col+window[1], 0]) + self.amp
x[0, row:row+window[0], col:col+window[1], 0] = (x[0, row:row+window[0], col:col+window[1], 0] + bias) * np.sin(self.freq * x[0, row:row+window[0], col:col+window[1], 0]) + self.amp
return x, None


Expand All @@ -172,7 +172,7 @@ def __init__(self, mean=1, stdv=1):
self.mean = mean
self.stdv = stdv
def apply(self, x, row, col, window, bias):
x[0, row:row+window[0], col:col+window[1], 0] = (x[0, row:row+window[0], col:col+window[1], 0] + bias) * math.exp(-((x[0, row:row+window[0], col:col+window[1], 0] - self.mean) ** 2) / (2 * self.stdv ** 2))
x[0, row:row+window[0], col:col+window[1], 0] = (x[0, row:row+window[0], col:col+window[1], 0] + bias) * np.exp(-((x[0, row:row+window[0], col:col+window[1], 0] - self.mean) ** 2) / (2 * self.stdv ** 2))
return x, None


Expand All @@ -189,7 +189,7 @@ class Tanh:
def __init__(self, sf=1):
self.sf = sf # scale factor
def apply(self, x, row, col, window, bias):
x[0, row:row+window[0], col:col+window[1], 0] = math.tanh(self.sf * (x[0, row:row+window[0], col:col+window[1], 0] + bias))
x[0, row:row+window[0], col:col+window[1], 0] = np.tanh(self.sf * (x[0, row:row+window[0], col:col+window[1], 0] + bias))
return x, None


Expand All @@ -206,7 +206,7 @@ class Sigmoid:
def __init__(self, sf=1):
self.sf = sf
def apply(self, x, row, col, window, bias):
x[0, row:row+window[0], col:col+window[1], 0] = 1 / (1 + math.exp(-self.sf * (x[0, row:row+window[0], col:col+window[1], 0] + bias)))
x[0, row:row+window[0], col:col+window[1], 0] = 1 / (1 + np.exp(-self.sf * (x[0, row:row+window[0], col:col+window[1], 0] + bias)))
return x, None


Expand Down Expand Up @@ -253,7 +253,7 @@ class Softplus:
def __init__(self, sf=1):
self.sf = sf
def apply(self, x, row, col, window, bias):
x[0, row:row+window[0], col:col+window[1], 0] = math.log(1 + math.exp(self.sf * (x[0, row:row+window[0], col:col+window[1], 0] + bias)))
x[0, row:row+window[0], col:col+window[1], 0] = np.log(1 + np.exp(self.sf * (x[0, row:row+window[0], col:col+window[1], 0] + bias)))
return x, None


Expand Down Expand Up @@ -419,3 +419,22 @@ def apply(self, x, row, col, window, bias):
if x[0, row, col, 0] >= self.th:
x[0, row:row+window[0], col:col+window[1], 0] = self.c
return x, None


class ClassInterchange:
"""
Sets the values to a combination between the spectrum and another spectrum
(generally another class spectrum)

:type change: float
:param change: The percentage of change (0=no perturbation, 1=spectrum2)

:rtype: 4d array
:return: Custom perturbated array
"""
def __init__(self, spectrum2, change=0):
self.spectrum2 = spectrum2
self.change = change
def apply(self, x, row, col, window, bias):
x[0, row:row+window[0], col:col+window[1], 0] = x[0, row:row+window[0], col:col+window[1], 0]*(1-self.change)+self.spectrum2[0, row:row+window[0], col:col+window[1], 0]*(self.change)
return x, None
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
numpy
numpy<2.0
pandas
matplotlib
spectrapepper
Expand Down