Skip to content

Commit

Permalink
linting (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
philiplinden authored Oct 25, 2023
1 parent efc4d42 commit accfa1c
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 60 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Lint

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

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruff
# Update output format to enable automatic inline annotations.
- name: Run Ruff
run: ruff check --output-format=github .
27 changes: 0 additions & 27 deletions .github/workflows/pylint.yml

This file was deleted.

6 changes: 4 additions & 2 deletions repro/data_io.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import os
import numpy as np
"""data_io
Tools for reading data files included in this repository.
"""
21 changes: 13 additions & 8 deletions repro/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
"""main
This module executes the simulation and runs the plotting
functions. It emulates src/Hydrated_Regolith_Spectra_Generator_and_Retrieval.m
"""
from matplotlib import pyplot as plt
import numpy as np
from pprint import pprint as print
from pprint import pprint

from simulator import Hapke, get_sample_grid
from .simulator import Hapke, get_sample_grid


if __name__ == '__main__':
if __name__ == "__main__":
WLS = get_sample_grid()
Refl = np.array([np.random.randn()*x for x in WLS])
Refl = np.array([np.random.randn() * x for x in WLS])
R = Hapke(Refl, WLS)
print(WLS)
print(Refl)
print(R)
pprint(WLS)
pprint(Refl)
pprint(R)
plt.figure()
plt.plot(WLS,Refl,WLS,R)
plt.plot(WLS, Refl, WLS, R)
plt.show()
69 changes: 49 additions & 20 deletions repro/simulator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
"""simulator
This module contains the classes and functions that emulate the helper
functions:
src/Hapke_Inverse_Function_Passive.m
src/Hapke_Lidar_R_Function.m
src/Hapke_Lidar_SSA_Function.m
"""
from functools import partial
from typing import Any, Callable
import numpy as np
Expand Down Expand Up @@ -28,34 +36,34 @@ def get_sample_grid(


def ordinary_least_squares(x: Any, y: Callable, yx: Any):
'''Ordinary Least Squares Function.
"""Ordinary Least Squares Function.
y (Callable): The estimator function.
x (Any): The argument to y.
yx (Any): The observation. Must be the same type as the result of y.
'''
"""
return sum((y(x) - yx) ** 2)


def angular_width(filling_factor=0.41):
'''Angular width parameter, see Equation 3.
"""Angular width parameter, see Equation 3.
Args:
filling_factor (float, optional): Filling factor. Defaults to 0.41 for the
lunar regolith (Bowell et al., 1989).
'''
"""
return (-3 / 8) * np.log(1 - filling_factor)


def backscattering(h, g):
'''Backscattering function, see Equation 2.
"""Backscattering function, see Equation 2.
This describes the opposition effect.
Args:
h (float): angular width parameter.
g (float): phase angle in radians.
'''
"""
return 1 / (1 + (1 / h) * np.tan(g / 2))


Expand All @@ -64,7 +72,7 @@ def R(SSA, P, mu, mu0, B):
R = (ω/4) (μ₀ / (μ + μ₀)) {(1 + B)P + H(ω)H₀(ω) - 1}
This equation is HUGE so I broke it down into smaller terms for code readability.
This equation is HUGE so I broke it down into smaller terms for code readability.
let:
R = r1 * r2 * (r3 + r4 - 1)
Expand All @@ -88,30 +96,30 @@ def R(SSA, P, mu, mu0, B):
"""

def Hfunc(SSA, mu, mu0):
'''Ambartsumian-Chandrasekhar H functions.
"""Ambartsumian-Chandrasekhar H functions.
Computed using the approximation from equation 8.57 from Hapke (2012).
Args:
SSA (float): single-scattering albedo, aka ω
mu (float): cosine of emission angle
mu0 (float): coside of incident angle
'''
"""
gamma = np.sqrt(1 - SSA)
r0 = (1 - gamma) / (1 + gamma)

h1 = np.log((1 + mu0) / mu0)
h2 = (r0 + ((1 - 2 * r0) * mu0) / 2) * h1

H = (1 - SSA * mu0 * h2) ** -1

h3 = np.log((1 + mu) / mu)
h4 = (1 - 2 * r0 * mu)
h4 = 1 - 2 * r0 * mu
h5 = r0 + h3 * (h4 / 2)

H0 = (1 - SSA * mu * h5) ** -1
return H, H0
return H, H0

H, H0 = Hfunc(SSA, mu, mu0)

r1 = SSA / 4
Expand All @@ -121,10 +129,18 @@ def Hfunc(SSA, mu, mu0):
return r1 * r2 * (r3 + r4 - 1)


def Hapke(Refl, WLS, P=0.15, emission_angle=0, incident_angle=30, phase_angle=30, filling_factor=0.41):
def Hapke(
Refl,
WLS,
P=0.15,
emission_angle=0,
incident_angle=30,
phase_angle=30,
filling_factor=0.41,
):
"""Convert reflectance spectrum to single-scattering albedo (SSA)
Uses scipy.optimize.fmin (equivalent to MATLAB fminsearch) to minimize
Uses scipy.optimize.fmin (equivalent to MATLAB fminsearch) to minimize
ordinary least squares distance between SSA obtained from the supplied
reflectance, R, and the SSA from the estimated reflectance at each
sample point in WLS.
Expand Down Expand Up @@ -155,7 +171,20 @@ def Hapke(Refl, WLS, P=0.15, emission_angle=0, incident_angle=30, phase_angle=30
w = []
for m, x in zip(Refl, WLS):
w0 = 0.5 # initial guess, ω₀
y = partial(R, P=P, mu=mu, mu0=mu0, B=B) # turn R() into the form y=f(x)
OLS = partial(ordinary_least_squares, y=y, yx=m) # turn least squares into the form y=f(x)
w.append(fminsearch(OLS, w0, args=x, disp=False, maxiter=10_000, maxfun=10_000, ftol=1e-7, xtol=1e-7))
y = partial(R, P=P, mu=mu, mu0=mu0, B=B) # turn R() into the form y=f(x)
OLS = partial(
ordinary_least_squares, y=y, yx=m
) # turn least squares into the form y=f(x)
w.append(
fminsearch(
OLS,
w0,
args=x,
disp=False,
maxiter=10_000,
maxfun=10_000,
ftol=1e-7,
xtol=1e-7,
)
)
return np.concatenate(w)
12 changes: 9 additions & 3 deletions repro/spectra.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""spectra
This module defines classes that represent spectra for each regolith type.
"""

from dataclasses import dataclass
from typing import Tuple

Expand All @@ -12,9 +17,10 @@ class Endmember:


class HydratedMorbGlass(Endmember):
''' Hydrated mid-ocean-ridge basalt (MORB) glass '''
sample_label = ''
spectrum_label = 'MORB D38A'
"""Hydrated mid-ocean-ridge basalt (MORB) glass"""

sample_label = ""
spectrum_label = "MORB D38A"
density = 2.8
grain_size = 69e-6

Expand Down

0 comments on commit accfa1c

Please sign in to comment.