Skip to content

Commit

Permalink
lintint
Browse files Browse the repository at this point in the history
  • Loading branch information
scottprahl committed Apr 30, 2024
1 parent 6235c43 commit 3a71a0a
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 124 deletions.
1 change: 0 additions & 1 deletion iadpython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,3 @@
from .grid import *
from .rxt import *
from .port import *

1 change: 1 addition & 0 deletions iadpython/ad.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def stringify(form, x):
s += form % mx
return s


class Sample():
"""Container class for details of a sample.
Expand Down
7 changes: 4 additions & 3 deletions iadpython/iad.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import scipy.optimize
import iadpython as iad


def stringify(form, x):
if x is None:
s = 'None'
Expand Down Expand Up @@ -99,15 +100,15 @@ def __init__(self,
self.num_measurements = 0
self.grid = None
self.counter = 0
self.include_measurements=True
self.include_measurements = True

def __str__(self):
"""Return basic details as a string for printing."""
s = "---------------- Sample ---------------\n"
s += self.sample.__str__()
s += "\n--------------- Spheres ---------------\n"
if not np.isscalar(self.num_spheres):
s += "number of spheres range (%s)\n" % stringify("%d",self.num_spheres)
s += "number of spheres range (%s)\n" % stringify("%d", self.num_spheres)
elif self.num_spheres == 0:
s += "No spheres used.\n"
elif self.num_spheres == 1:
Expand Down Expand Up @@ -517,7 +518,7 @@ def abfun(x, *args):
exp.sample.b = x[1]
m_r, m_t = exp.measured_rt()
delta = np.abs(m_r - exp.m_r) + np.abs(m_t - exp.m_t)
print("%7.4f %7.4f %7.4f %7.4f %7.4f"%(exp.sample.a, exp.sample.b, m_r, m_t, delta))
print("%7.4f %7.4f %7.4f %7.4f %7.4f" % (exp.sample.a, exp.sample.b, m_r, m_t, delta))
return delta


Expand Down
22 changes: 20 additions & 2 deletions iadpython/iadcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
COUNTER = 0
ANY_ERROR = False


class SlidePosition(Enum):
"""All the combinations of glass and sample."""
NO_SLIDES = 0
Expand All @@ -29,12 +30,14 @@ class SlidePosition(Enum):
ONE_SLIDE_NEAR_SPHERE = 4
ONE_SLIDE_NOT_NEAR_SPHERE = 5


class LightCondition(Enum):
"""Options for interpreting illumination."""
MR_IS_ONLY_RD = 1
MT_IS_ONLY_TD = 2
NO_UNSCATTERED_LIGHT = 3


class InputError(Enum):
"""Possible input errors."""
NO_ERROR = 0
Expand All @@ -47,6 +50,7 @@ class InputError(Enum):
MU_TOO_SMALL = 7
TOO_MUCH_LIGHT = 8


def print_error_legend():
"""Print error explanation and quit."""
print("----------------- Sorry, but ... errors encountered ---------------")
Expand All @@ -62,6 +66,7 @@ def print_error_legend():
print(" + ==> Did not converge\n")
sys.exit(0)


def what_char(err):
"""Return appropriate character for analysis of current datapoint."""
if err == InputError.NO_ERROR:
Expand All @@ -84,6 +89,7 @@ def what_char(err):
return '!'
return '?'


def print_dot(start_time, err, points, final, verbosity):
"""Print a character for each datapoint during analysis."""
global COUNTER
Expand Down Expand Up @@ -122,6 +128,7 @@ def validator_01(value):
raise argparse.ArgumentTypeError(f"Commandline: {value} is not between 0 and 1")
return fvalue


def validator_11(value):
"""Is value between -1 and 1."""
try:
Expand All @@ -132,6 +139,7 @@ def validator_11(value):
raise argparse.ArgumentTypeError(f"Commandline: {value} is not between 0 and 1")
return fvalue


def validator_positive(value):
"""Is value non-negative."""
try:
Expand All @@ -142,6 +150,7 @@ def validator_positive(value):
raise argparse.ArgumentTypeError(f"{value} is not positive")
return fvalue


# Argument specifications
arg_specs = [
{"flags": ["-1"], "dest": "r_sphere", "metavar": ("SPHERE_D", "SAMPLE_D", "ENTRANCE_D", "DETECTOR_D", "WALL_R"),
Expand Down Expand Up @@ -184,6 +193,7 @@ def validator_positive(value):
{"flags": ["filename"], "nargs": "?", "type": str, "default": None, "help": "Input filename"}
]


def print_long_version():
"""Print the version information and quit."""
s = ''
Expand Down Expand Up @@ -212,6 +222,7 @@ def example_text():
s += " iad -r 0.3 R_total=0.\n"
return s


def add_sample_constraints(exp, args):
"""Command-line constraints on sample."""
if args.thickness is not None:
Expand Down Expand Up @@ -264,10 +275,11 @@ def add_sample_constraints(exp, args):
if args.q is not None:
if args.q % 4:
raise argparse.ArgumentTypeError('Commandline: Number of quadrature points must be a multiple of 4')
if exp.sample.nu_0 !=1 and args.q % 12:
if exp.sample.nu_0 != 1 and args.q % 12:
raise argparse.ArgumentTypeError('Commandline: Quadrature must be 12, 24, 36,... for oblique incidence')
exp.sample.quad_pts = args.q


def add_experiment_constraints(exp, args):
"""Command-line constraints on experiment."""
if args.S is not None:
Expand All @@ -291,6 +303,7 @@ def add_experiment_constraints(exp, args):
if args.u is not None:
exp.m_u = args.u


def add_analysis_constraints(exp, args):
"""Add command line constraints on analysis."""
# constraints on analysis
Expand Down Expand Up @@ -323,6 +336,7 @@ def add_analysis_constraints(exp, args):
# photons
pass


def forward_calculation(exp):
"""Do a forward calculation."""
# set albedo
Expand All @@ -348,7 +362,7 @@ def forward_calculation(exp):
elif exp.default_mus is None:
exp.sample.b = float('inf')
else:
exp.sample.b = exp.default_mus/exp.sample.a * exp.sample.d
exp.sample.b = exp.default_mus / exp.sample.a * exp.sample.d
else:
exp.sample.b = exp.default_b

Expand All @@ -371,6 +385,7 @@ def forward_calculation(exp):
print(" T unscattered = %.3f" % tu)
sys.exit(0)


def print_results_header(debug_lost_light=False):
"""Print the header for results to stdout."""
print("# \tMeasured \t M_R \tMeasured \t M_T \tEstimated\tEstimated\tEstimated", end='')
Expand All @@ -388,6 +403,7 @@ def print_results_header(debug_lost_light=False):
print("\t [---] \t [---] \t [---] \t [---] \t [---] \t [---] \t [---] ", end='')
print()


def invert_file(exp, args):
"""Process an entire .rxt file."""
# determine output file name
Expand Down Expand Up @@ -441,6 +457,7 @@ def invert_file(exp, args):
sys.stdout = original_stdout
sys.exit(0)


def main():
"""Main command-line interface."""
parser = argparse.ArgumentParser(description='iad command line program',
Expand Down Expand Up @@ -489,5 +506,6 @@ def main():
print(f"Error: {e}")
sys.exit(1)


if __name__ == "__main__":
main()
22 changes: 12 additions & 10 deletions iadpython/port.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import random
import numpy as np


def uniform_disk():
"""
Generate a point uniformly distributed on a unit disk.
Expand All @@ -58,16 +59,17 @@ def uniform_disk():
while s > 1:
x = 2 * random.random() - 1
y = 2 * random.random() - 1
s = x*x + y*y
s = x * x + y * y
return x, y, s


class Port():
"""
A container class for a port in an integrating sphere, which is a structure
used to analyze light properties. The class calculates and stores various
geometrical properties of the port, such as its diameter, position, and the
relative area of the spherical cap it forms on the sphere's surface.
Attributes:
sphere (object): Reference to the sphere the port belongs to.
d (float): Diameter of the port in millimeters (mm).
Expand All @@ -78,10 +80,10 @@ class Port():
a (float): Relative area of the port's spherical cap to the sphere's surface.
chord2 (float): Square of the distance from the port center to the port edge.
sagitta (float): The sagitta (height) of the spherical cap formed by the port.
Examples:
Importing the module and creating a sphere with a port:
```python
import iadpython as iad
s = iad.Sphere(200, 20)
Expand Down Expand Up @@ -126,7 +128,7 @@ def d(self):
def d(self, value):
"""
Sets the diameter of the port and recalculates geometrical properties.
Args:
value (float): The new diameter of the port.
Expand All @@ -148,7 +150,7 @@ def __str__(self):
"""Return basic details as a string for printing."""
s = ""
s += " diameter = %7.2f mm\n" % self.d
s += " radius = %7.2f mm\n" % (self.d/2)
s += " radius = %7.2f mm\n" % (self.d / 2)
s += " chord = %7.2f mm\n" % np.sqrt(self.chord2)
s += " sagitta = %7.2f mm\n" % self.sagitta
s += " center = (%6.1f, %6.1f, %6.1f) mm\n" % (self.x, self.y, self.z)
Expand Down Expand Up @@ -206,19 +208,19 @@ def set_center(self, x, y, z):
def uniform(self):
"""
Generate a point uniformly distributed on a spherical cap.
This function generates points uniformly distributed over a spherical cap,
defined by a specified sagitta (height of the cap from its base to the top)
and sphere radius. The spherical cap can be positioned either at the top or
bottom of the sphere. The method utilizes principles from uniform distribution
on a disk and transforms these points to the spherical cap geometry.
WARNING: The cap assumed to be at the top of a sphere. Nothing is done to rotate
the points so they align with the center of the cap!
The algorithm to generate a random point on a spherical cap is adapted from
http://marc-b-reynolds.github.io/distribution/2016/11/28/Uniform.html
Args:
sagitta: The height of the spherical cap from its base to the top.
sphere_radius: The radius of the sphere with the cap.
Expand Down

0 comments on commit 3a71a0a

Please sign in to comment.