Skip to content

Commit a807844

Browse files
committed
fixing flake8:
__init__: noqa: F401 on gTopRefinableObjRegistry import. Removing it will cause potential api breakage on downstream users. Also Exist at pyobjcryst.globals crystal.py: Add except type (AttributeError) at UpdateDisplay(self) to avoid E722. fourier.py: noqa E741 on ambigous variable name 'l'. (hkl) globaloptim.py: clarify refinableobj imports lsq.py: Expose LSQ from c++ extension powderpattern.py: Add except type (AttributeError) at UpdateDisplay(self) to avoid E722. Add except type (AttributeError, RuntimeError, ValueError) at plot() (Force immediate display) with warning messages "Plot refresh failed..." Add except type (AttributeError, RuntimeError) at _do_plot_hkl (Force immediate display). Nested utils.py: change lambda function to def
1 parent 0aa8b8b commit a807844

File tree

7 files changed

+47
-31
lines changed

7 files changed

+47
-31
lines changed

src/pyobjcryst/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
import pyobjcryst.spacegroup
7373
import pyobjcryst.unitcell
7474
import pyobjcryst.zscatterer
75-
from pyobjcryst._pyobjcryst import gTopRefinableObjRegistry
75+
76+
# could be api breaking if removed
77+
from pyobjcryst._pyobjcryst import gTopRefinableObjRegistry # noqa: F401
7678

7779
# Let's put this on the package level
7880
from pyobjcryst.general import ObjCrystException

src/pyobjcryst/crystal.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
]
3737

3838
import warnings
39-
from multiprocessing import current_process
40-
from types import MethodType
4139
from urllib.request import urlopen
4240

4341
import numpy as np
@@ -48,8 +46,6 @@
4846
)
4947
from pyobjcryst._pyobjcryst import Crystal as Crystal_orig
5048

51-
from .refinableobj import wrap_boost_refinableobjregistry
52-
5349
try:
5450
import py3Dmol
5551
except ImportError:
@@ -78,7 +74,7 @@ def UpdateDisplay(self):
7874
try:
7975
if self._display_update_disabled:
8076
return
81-
except:
77+
except AttributeError:
8278
pass
8379
# test for _3d_widget is a bit ugly, but to correctly implement this we'd need an
8480
# __init__ function which overrides the 3 different Crystal constructors which
@@ -300,7 +296,7 @@ def _display_list(
300296
name = scatt.GetComponentName(j)
301297
name = name.replace("'", "_")
302298
symbol = s.mpScattPow.GetSymbol()
303-
occ = s.Occupancy
299+
# occ = s.Occupancy
304300
x, y, z = s.X, s.Y, s.Z
305301
if enantiomer:
306302
x = -x
@@ -413,8 +409,8 @@ def _display_list(
413409
}
414410
)
415411
else:
416-
# TODO add 'visible' value in dictionary to determine which atoms are shown,
417-
# then update the bond and bondOrder lists
412+
# TODO add 'visible' value in dictionary to determine which atoms
413+
# are shown, then update the bond and bondOrder lists
418414
for k in range(nsym):
419415
for dx in (-1, 0, 1):
420416
for dy in (-1, 0, 1):
@@ -443,14 +439,14 @@ def _display_list(
443439
j = a["j"]
444440
vb = []
445441
vo = []
446-
for l in range(len(a["bonds"])):
447-
int_ptr = a["bonds"][l]
442+
for i in range(len(a["bonds"])):
443+
int_ptr = a["bonds"][i]
448444
if atoms[int_ptr]["visible"]:
449445
vb.append(
450446
atoms[int_ptr]["idx"]
451447
)
452448
vo.append(
453-
a["bondOrder"][l]
449+
a["bondOrder"][i]
454450
)
455451
x, y, z = vxyz[k, j] + np.array(
456452
(dx, dy, dz)
@@ -479,7 +475,7 @@ def _display_list(
479475
# 3dmol.js does not like ' in names,
480476
# despite https://www.iucr.org/resources/cif/spec/version1.1/cifsyntax#bnf
481477
name = name.replace("'", "_")
482-
occ = s.Occupancy
478+
# occ = s.Occupancy
483479
x, y, z = s.X, s.Y, s.Z
484480
if enantiomer:
485481
x = -x
@@ -514,7 +510,7 @@ def _display_list(
514510
# 3dmol.js does not like ' in names,
515511
# despite https://www.iucr.org/resources/cif/spec/version1.1/cifsyntax#bnf
516512
name = name.replace("'", "_")
517-
occ = s.Occupancy
513+
# occ = s.Occupancy
518514

519515
for k in range(nsym):
520516
for dx in (-1, 0, 1):

src/pyobjcryst/fourier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def calc_fourier_map(
6767
# print(" Fourier map obs scale factor:", scale_fobs)
6868
vol = c.GetVolume()
6969
spg = c.GetSpaceGroup()
70-
h, k, l = data.GetH()[:nb], data.GetK()[:nb], data.GetL()[:nb]
70+
h, k, l = data.GetH()[:nb], data.GetK()[:nb], data.GetL()[:nb] # noqa E741
7171
# Map size to achieve resolution
7272
nx = int(np.ceil(c.a / resolution))
7373
ny = int(np.ceil(c.b / resolution))

src/pyobjcryst/globaloptim.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,21 @@
2929
from pyobjcryst._pyobjcryst import MonteCarlo as MonteCarlo_orig
3030
from pyobjcryst._pyobjcryst import OptimizationObjRegistry
3131

32-
from .refinableobj import *
32+
from .refinableobj import (
33+
refpartype_scattdata_background,
34+
refpartype_scattdata_corr,
35+
refpartype_scattdata_profile,
36+
refpartype_scattdata_radiation,
37+
refpartype_scattdata_scale,
38+
refpartype_unitcell,
39+
)
3340

3441

3542
class MonteCarlo(MonteCarlo_orig):
3643

3744
def Optimize(self, nb_step: int, final_cost=0, max_time=-1):
3845
self._fix_parameters_for_global_optim()
39-
if type(self) == MonteCarlo_orig:
46+
if type(self) is MonteCarlo_orig:
4047
self._Optimize(int(nb_step), True, final_cost, max_time)
4148
else:
4249
super().Optimize(int(nb_step), True, final_cost, max_time)
@@ -45,7 +52,7 @@ def MultiRunOptimize(
4552
self, nb_run: int, nb_step: int, final_cost=0, max_time=-1
4653
):
4754
self._fix_parameters_for_global_optim()
48-
if type(self) == MonteCarlo_orig:
55+
if type(self) is MonteCarlo_orig:
4956
self._MultiRunOptimize(
5057
int(nb_run), int(nb_step), True, final_cost, max_time
5158
)
@@ -56,7 +63,7 @@ def MultiRunOptimize(
5663

5764
def RunSimulatedAnnealing(self, nb_step: int, final_cost=0, max_time=-1):
5865
self._fix_parameters_for_global_optim()
59-
if type(self) == MonteCarlo_orig:
66+
if type(self) is MonteCarlo_orig:
6067
self._RunSimulatedAnnealing(
6168
int(nb_step), True, final_cost, max_time
6269
)
@@ -67,7 +74,7 @@ def RunSimulatedAnnealing(self, nb_step: int, final_cost=0, max_time=-1):
6774

6875
def RunParallelTempering(self, nb_step: int, final_cost=0, max_time=-1):
6976
self._fix_parameters_for_global_optim()
70-
if type(self) == MonteCarlo_orig:
77+
if type(self) is MonteCarlo_orig:
7178
self._RunParallelTempering(
7279
int(nb_step), True, final_cost, max_time
7380
)
@@ -116,7 +123,7 @@ def UpdateDisplay(self):
116123
try:
117124
if self._display_update_disabled:
118125
return
119-
except:
126+
except AttributeError:
120127
pass
121128
try:
122129
if self._widget is not None:

src/pyobjcryst/lsq.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717
In development !
1818
"""
1919

20+
__all__ = ["LSQ"]
21+
2022
from pyobjcryst._pyobjcryst import LSQ

src/pyobjcryst/powderpattern.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
Additional functions for plotting, basic QPA and profile fitting.
1818
"""
1919

20-
from multiprocessing import current_process
20+
import inspect
21+
import warnings
2122
from urllib.request import urlopen
2223

2324
import numpy as np
@@ -33,9 +34,6 @@
3334
"SpaceGroupExplorer",
3435
]
3536

36-
from types import MethodType
37-
38-
from pyobjcryst.general import ObjCrystException
3937
from pyobjcryst._pyobjcryst import LSQ
4038
from pyobjcryst._pyobjcryst import (
4139
CreatePowderPatternFromCIF as CreatePowderPatternFromCIF_orig,
@@ -48,6 +46,7 @@
4846
ReflectionProfileType,
4947
SpaceGroupExplorer,
5048
)
49+
from pyobjcryst.general import ObjCrystException
5150
from pyobjcryst.refinableobj import refpartype_scattdata_background
5251

5352

@@ -82,7 +81,7 @@ def UpdateDisplay(self):
8281
try:
8382
if self._display_update_disabled:
8483
return
85-
except:
84+
except AttributeError:
8685
pass
8786
if self._plot_fig is not None:
8887
if self._plot_fig is not None:
@@ -225,8 +224,17 @@ def plot(
225224
self._plot_fig.canvas.draw()
226225
if "ipympl" not in plt.get_backend():
227226
plt.pause(0.001)
228-
except:
229-
pass
227+
except (AttributeError, RuntimeError, ValueError) as e:
228+
cls_name = type(self).__name__
229+
func_name = inspect.currentframe().f_code.co_name
230+
backend = plt.get_backend()
231+
fig_id = getattr(self._plot_fig, "number", None)
232+
warnings.warn(
233+
f"[{cls_name}.{func_name}] "
234+
f"Plot refresh failed ({type(e).__name__}): {e}. "
235+
f"Matplotlib backend={backend}, figure id={fig_id}",
236+
stacklevel=2,
237+
)
230238
# plt.gca().callbacks.connect('xlim_changed', self._on_xlims_change)
231239
# plt.gca().callbacks.connect('ylim_changed', self._on_ylims_change)
232240
self._plot_fig.canvas.mpl_connect(
@@ -273,15 +281,15 @@ def _do_plot_hkl(self, nb_max=100, fontsize_hkl=None):
273281
try:
274282
# need the renderer to avoid text overlap
275283
renderer = plt.gcf().canvas.get_renderer()
276-
except:
284+
except (AttributeError, RuntimeError):
277285
# Force immediate display. Not supported on all backends (e.g. nbagg)
278286
ax.draw()
279287
self._plot_fig.canvas.draw()
280288
if "ipympl" not in plt.get_backend():
281289
plt.pause(0.001)
282290
try:
283291
renderer = self._plot_fig.canvas.get_renderer()
284-
except:
292+
except (AttributeError, RuntimeError):
285293
renderer = None
286294
else:
287295
renderer = None

src/pyobjcryst/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def putAtomsInMolecule(crystal, alist=None, name=None):
5454
# mapping fractional coords back into [0, 1)
5555
from math import floor
5656

57-
f = lambda v: v - floor(v)
57+
def f(v):
58+
return v - floor(v)
5859

5960
scat = []
6061
for idx in alist:

0 commit comments

Comments
 (0)