Skip to content

Commit

Permalink
PEP 8 check
Browse files Browse the repository at this point in the history
  • Loading branch information
terrencetec committed Sep 1, 2023
1 parent 4b49157 commit 5f16f23
Show file tree
Hide file tree
Showing 31 changed files with 289 additions and 347 deletions.
4 changes: 2 additions & 2 deletions kontrol/__init__.py
@@ -1,14 +1,14 @@
from .complementary_filter.complementary_filter import ComplementaryFilter
from .transfer_function import *
from .sensact.matrix import Matrix, SensingMatrix
from .sensact.matrix import Matrix, SensingMatrix
from .sensact.optical_lever import (
OpticalLeverSensingMatrix, HorizontalOpticalLeverSensingMatrix,
VerticalOpticalLeverSensingMatrix)
from .core import spectral, foton
from . import dmd


## for ad hoc/optional packages
# for ad hoc/optional packages
import importlib
ezca_spec = importlib.util.find_spec("ezca")
ezca_exist = ezca_spec is not None
Expand Down
9 changes: 4 additions & 5 deletions kontrol/complementary_filter/complementary_filter.py
@@ -1,6 +1,5 @@
"""Complementary filter class for sythesis
"""
import control
import numpy as np


Expand Down Expand Up @@ -53,7 +52,7 @@ class ComplementaryFilter():
:math:`\mathcal{H}_\infty`-norm
of the super sensor noise. The noise must be specified
before using this method.
Notes
-----
This is a utility class for complementary filter synthesis,
Expand Down Expand Up @@ -120,7 +119,7 @@ def __init__(self, noise1=None, noise2=None, weight1=None, weight2=None,

def h2synthesis(self, clean_filter=True, **kwargs):
"""Synthesize complementary filters using H2 synthesis.
Returns
-------
filter1 : TransferFunction
Expand All @@ -138,7 +137,7 @@ def h2synthesis(self, clean_filter=True, **kwargs):

def hinfsynthesis(self, clean_filter=True, **kwargs):
"""Synthesize complementary filters using H-inifinity synthesis.
Returns
-------
filter1 : TransferFunction
Expand Down Expand Up @@ -242,7 +241,7 @@ def noise_super(self, f=None, noise1=None, noise2=None,
The complementary filter for filtering noise1
Use self.filter2 if not specified.
Defaults None
Returns
-------
array
Expand Down
20 changes: 6 additions & 14 deletions kontrol/complementary_filter/predefined.py
Expand Up @@ -41,15 +41,6 @@ def sekiguchi(coefs):

blend_freq = coefs[0]
_coefs = [blend_freq]*4
# hpf_numerator = [
# 1,
# 7*blend_freq,
# 21*blend_freq**2,
# 35*blend_freq**3,
# 0, 0, 0, 0,
# ]
# hpf = control.tf(hpf_numerator, [1]) * control.tf([1], [1, blend_freq])**7
# lpf = 1 - hpf
return modified_sekiguchi(_coefs)


Expand Down Expand Up @@ -94,7 +85,7 @@ def modified_sekiguchi(coefs):

def generalized_sekiguchi(fb, order_low_pass, order_high_pass):
"""Generalized Sekiguchi Filter
Parameters
----------
fb : float
Expand All @@ -103,7 +94,7 @@ def generalized_sekiguchi(fb, order_low_pass, order_high_pass):
Order of roll-off of the low-pass filter
order_high_pass : int
Order of roll-off of the high-pass filter
Returns
-------
lpf : kontrol.TransferFunction
Expand All @@ -114,7 +105,7 @@ def generalized_sekiguchi(fb, order_low_pass, order_high_pass):
nl = order_low_pass
nh = order_high_pass
den = (scipy.special.comb(nl+nh-1, np.arange(0, nl+nh))
*(2*np.pi*fb)**np.arange(0, nl+nh))
* (2*np.pi*fb)**np.arange(0, nl+nh))
lpf_num = den[nl:]
lpf = kontrol.TransferFunction(lpf_num, den)
return lpf, 1-lpf
Expand Down Expand Up @@ -150,8 +141,9 @@ def lucia(coefs):
p1, p2, z1, w1, q1, w2, q2 = coefs
hpf = control.tf([1], [1/p1, 1])**5 * control.tf([1], [1/p2, 1])**3
hpf *= control.tf([1/z1, 1], [1])
hpf *= control.tf([1, w1/q1, w1**2], [w1**2]) * control.tf([1, w2/q2, w2**2], [w2**2])
hpf *= control.tf([1, 0],[1])**3
hpf *= control.tf(
[1, w1/q1, w1**2], [w1**2]) * control.tf([1, w2/q2, w2**2], [w2**2])
hpf *= control.tf([1, 0], [1])**3
hpf *= hpf.den[0][0][0]/hpf.num[0][0][0]
lpf = 1 - hpf
return (lpf, hpf)
4 changes: 2 additions & 2 deletions kontrol/complementary_filter/synthesis.py
Expand Up @@ -7,7 +7,7 @@

def generalized_plant(noise1, noise2, weight1, weight2):
"""Return the generalized plant of a 2 complementary filter system
Parameters
----------
noise1 : TransferFunction
Expand Down Expand Up @@ -45,7 +45,7 @@ def generalized_plant(noise1, noise2, weight1, weight2):
# [tf([1],[1]), tf([0],[1])]]
p = kontrol.core.controlutils.tfmatrix2tf(p)
return p


def h2complementary(noise1, noise2, weight1=None, weight2=None):
"""H2 optimal complementary filter synthesis
Expand Down
5 changes: 1 addition & 4 deletions kontrol/core/algorithm.py
Expand Up @@ -31,7 +31,7 @@ def bisection_method(
x1 : float
The first endpoint.
x2 : float
The second endpoint.
The second endpoint.
val : float
The target value.
ftol : float, optional,
Expand Down Expand Up @@ -87,6 +87,3 @@ def bisection_method(
x2 = xm
kontrol.logger.logger.error("Maximum number of iteration reached")
return None



34 changes: 14 additions & 20 deletions kontrol/core/controlutils.py
Expand Up @@ -81,7 +81,7 @@ def zpk(zeros, poles, gain, unit='f', negate=True):
for i in range(len(poles)):
poles[i] = -poles[i]

zpk_tf = control.tf([gain],[1])
zpk_tf = control.tf([gain], [1])
for z in zeros:
zpk_tf *= control.tf([1/z, 1], [1])
for p in poles:
Expand Down Expand Up @@ -283,7 +283,7 @@ def generic_tf(zeros=None, poles=None,
poles_wn = []
if poles_q is None:
poles_q = []

zeros = np.array(zeros)
poles = np.array(poles)
zeros_wn = np.array(zeros_wn)
Expand Down Expand Up @@ -392,13 +392,7 @@ def tf_order_split(tf, max_order=20):
zeros = tf.zeros()
poles = tf.poles()
gain = tf.minreal().num[0][0][0]
# for i in range(len(zeros)):
# if zeros[i]
nzero = len(zeros)
npole = len(poles)
niter_zero = int(np.ceil(nzero/max_order))
niter_pole = int(np.ceil(npole/max_order))


global order_running
global zero_running
global tf_zero_list
Expand All @@ -409,7 +403,7 @@ def tf_order_split(tf, max_order=20):
tf_zero_list = []
pole_running = []
tf_pole_list = []

def put_tf_zeros_into_list():
"""Use zeros in zero_running to create an all zero transfer function
and put store them into tf_zero_list, and reset order_running to 0.
Expand Down Expand Up @@ -497,11 +491,11 @@ def put_tf_poles_into_list():
order_running += 2
zero_running += [zeros[i]]
else:
pass ## Ignore negative part of the complex pair.
## Add the reminders
pass # Ignore negative part of the complex pair.

# Add the reminders
put_tf_zeros_into_list()

for i in range(len(poles)):
if poles[i].imag == 0:
if order_running+1 > max_order:
Expand All @@ -514,8 +508,8 @@ def put_tf_poles_into_list():
order_running += 2
pole_running += [poles[i]]
else:
pass ## Ignore negative part of the complex pair.
pass # Ignore negative part of the complex pair.

put_tf_poles_into_list()

n_tf = max(len(tf_pole_list), len(tf_zero_list))
Expand Down Expand Up @@ -613,7 +607,7 @@ def clean_tf2(tf, tol_order=5, small_number=1e-25):
tf_cleaned *= (s+abs(zero))/abs(zero)
elif zero == 0:
tf_cleaned *= s

for i, pole in enumerate(poles):
if mask_poles[i]: # Remove outliers
continue
Expand All @@ -639,7 +633,7 @@ def clean_tf2(tf, tol_order=5, small_number=1e-25):

def clean_tf3(tf, tol_order=5, small_number=1e-25):
"""Remove first coefficient if it is much smaller than the second
Parameters
----------
tf : TransferFunction
Expand All @@ -651,7 +645,7 @@ def clean_tf3(tf, tol_order=5, small_number=1e-25):
numerator and denominator.
small_number : float, optional
A small number to be added to the log10() in case 0 is encountered.
Returns
-------
tf_cleaned : TransferFunction
Expand All @@ -660,7 +654,7 @@ def clean_tf3(tf, tol_order=5, small_number=1e-25):
num = tf.num[0][0].copy()
den = tf.den[0][0].copy()
if (abs(np.log10(num[0]) - np.log10(num[1])) > tol_order
and abs(np.log10(den[0]) - np.log10(den[1])) > tol_order):
and abs(np.log10(den[0]) - np.log10(den[1])) > tol_order):
#
tf_cleaned = control.tf(tf.num[0][0][1:], tf.den[0][0][1:])
else:
Expand Down
37 changes: 19 additions & 18 deletions kontrol/core/foton.py
Expand Up @@ -48,9 +48,9 @@ def tf2foton(
raise ValueError("expression {} not available."
"Please select expression from [\"zpk\", \"rpoly\"."
"".format(expression))
## Divide tf into tfs with less than 20 order.
## Do tf conversion.
## Stack the string
# Divide tf into tfs with less than 20 order.
# Do tf conversion.
# Stack the string
foton_expression = ""
tf_list = kontrol.core.controlutils.tf_order_split(tf, max_order=20)
if len(tf_list) > 1:
Expand Down Expand Up @@ -123,23 +123,23 @@ def tf2zpk(tf, root_location="s", significant_figures=6,
str_zeros = "" # String of list of zeros (placeholder)
str_poles = "" # String of list of poles (placeholder)

## get zeros and poles.
# get zeros and poles.
if root_location in ["f", "n"]:
zeros /= 2*np.pi
poles /= 2*np.pi
if root_location == "n":
zeros = -zeros.conjugate()
poles = -poles.conjugate()

## get zeros and poles list, and sort.
# get zeros and poles list, and sort.
z_wn = np.sqrt(tf.zeros().real**2 + tf.zeros().imag**2)
p_wn = np.sqrt(tf.poles().real**2 + tf.poles().imag**2)
z_sort_arg = z_wn.argsort()
p_sort_arg = p_wn.argsort()
z_wn.sort()
p_wn.sort()

## get gain
# get gain
gain = tf.minreal().num[0][0][0]
if root_location in ["n"]:
for wn in p_wn:
Expand All @@ -153,7 +153,7 @@ def tf2zpk(tf, root_location="s", significant_figures=6,
else:
gain *= 2*np.pi

## Convert to zpk expressing string
# Convert to zpk expressing string
for zero in zeros[z_sort_arg]:
if abs(zero.imag)/abs(zero.real+epsilon) < itol:
str_zeros += "{:.{}g}".format(zero.real, significant_figures)
Expand Down Expand Up @@ -201,8 +201,8 @@ def tf2rpoly(tf, significant_figures=6):

num = tf.minreal().num[0][0]
den = tf.minreal().den[0][0]
str_num = "" ## String of numerator coefficients
str_den = "" ## String of numerator coefficients
str_num = "" # String of numerator coefficients
str_den = "" # String of numerator coefficients
gain = num[0]
num /= num[0]

Expand Down Expand Up @@ -260,12 +260,12 @@ def _order_gt(tf, order):

def foton2tf(foton_string):
"""Convert a Foton string to TransferFunction
Parameters
----------
foton_string : str
The Foton string, e.g. zpk([0], [1; 1], 1, "n").
Returns
-------
tf : TransferFunction
Expand All @@ -283,7 +283,7 @@ def foton2tf(foton_string):

def zpk2tf(foton_string):
"""Convert a Foton ZPK string to TransferFunction
Paramters
---------
foton_string : str
Expand Down Expand Up @@ -311,7 +311,7 @@ def process_zp_string(zp_string):
if zp_string == [""]:
return np.array(zp)
for string in zp_string:
#split real and imaginary
# split real and imaginary
split = string.split("i*")
if len(split) == 2:
real_string, imag_string = split
Expand All @@ -322,7 +322,7 @@ def process_zp_string(zp_string):
sign = "+"
real_string = real_string.rstrip("+")
real_string = real_string.rstrip("-")

zp_complex = f"{real_string}{sign}{imag_string}j"
zp.append(complex(zp_complex))
zp = np.array(zp)
Expand All @@ -346,7 +346,7 @@ def process_zp_string(zp_string):
zeros = -zeros
poles = -poles

tf = control.tf([1],[1])
tf = control.tf([1], [1])
s = control.tf("s")

for zero in zeros:
Expand Down Expand Up @@ -379,7 +379,7 @@ def process_zp_string(zp_string):
tf *= gain
elif root_location in "sf":
tf *= gain / (tf.num[0][0][0]/tf.den[0][0][0])

return kontrol.TransferFunction(tf)


Expand All @@ -396,14 +396,15 @@ def rpoly2tf(foton_string):
foton_string = foton_string.replace("(", "")
foton_string = foton_string.replace(")", "")
num_string, den_string, gain_string = foton_string.split(",")

def string2floatarray(string):
"""Convert a string of float array to array"""
string = string.replace("[", "")
string = string.replace("]", "")
string = string.split(";")
float_array = []
if string == [""]:
return np.array(float_array)
return np.array(float_array)
for value in string:
float_array.append(float(value))
return np.array(float_array)
Expand All @@ -417,7 +418,7 @@ def string2floatarray(string):

def notch(frequency, q, depth, significant_figures=6):
"""Returns the foton expression of a notch filter.
Parameters
----------
frequency : float
Expand Down

0 comments on commit 5f16f23

Please sign in to comment.