Skip to content

Commit

Permalink
style: Fix unconventional-import-alias (ICN001) (#1317)
Browse files Browse the repository at this point in the history
- `matplotlib` should be imported as `mpl`
- `numpy` should be imported as `np`
- `xml.etree.ElementTree` should be imported as `ET`

Ruff rule: https://docs.astral.sh/ruff/rules/unconventional-import-alias/
  • Loading branch information
echoix authored Feb 16, 2025
1 parent f49d73f commit bf40491
Show file tree
Hide file tree
Showing 29 changed files with 88 additions and 89 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ ignore = [
"G002", # logging-percent-format
"G003", # logging-string-concat
"I001", # unsorted-imports
"ICN001", # unconventional-import-alias
"INP001", # implicit-namespace-package
"ISC003", # explicit-string-concatenation
"LOG015", # root-logger-call
Expand Down
6 changes: 3 additions & 3 deletions src/gui/wxpython/wx.metadata/mdlib/cswutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import os
import sys
import webbrowser
import xml.etree.ElementTree as etree
import xml.etree.ElementTree as ET
from gettext import gettext, ngettext
from xml.dom.minidom import parseString

Expand All @@ -24,11 +24,11 @@ def get_connections_from_file(filename):

error = 0
try:
doc = etree.parse(filename).getroot()
doc = ET.parse(filename).getroot()
if doc.tag != "qgsCSWConnections":
error = 1
msg = "Invalid CSW connections XML."
except etree.ParseError as err:
except ET.ParseError as err:
error = 1
msg = "Cannot parse XML file: %s" % err
except OSError as err:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,9 @@ def parse_instances(
May be extended in the future (commented parameters)
"""
import matplotlib # required by windows
import matplotlib as mpl # required by windows

matplotlib.use("wxAGG") # required by windows
mpl.use("wxAGG") # required by windows
import matplotlib.pyplot as plt

dpi = 80
Expand Down
4 changes: 2 additions & 2 deletions src/imagery/i.sentinel/i.sentinel.import/i.sentinel.import.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,15 +811,15 @@ def _parse_mtd_file(mtd_file):
import numpy as np

try:
from xml.etree import ElementTree
from xml.etree import ElementTree as ET
from datetime import datetime
except ImportError as e:
gs.fatal(_("Unable to parse metadata file. {}").format(e))

meta = {}
meta["timestamp"] = None
with io.open(mtd_file, encoding="utf-8") as fd:
root = ElementTree.fromstring(fd.read())
root = ET.fromstring(fd.read())
nsPrefix = root.tag[: root.tag.index("}") + 1]
nsDict = {"n1": nsPrefix[1:-1]}
node = root.find("n1:General_Info", nsDict)
Expand Down
4 changes: 2 additions & 2 deletions src/imagery/i.sentinel/i.sentinel.mask/i.sentinel.mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
import json
import sys

import xml.etree.ElementTree as et
import xml.etree.ElementTree as ET

from pathlib import Path

Expand Down Expand Up @@ -243,7 +243,7 @@ def get_sun_position(module_options, bands):
)
)
try:
xml_tree = et.parse(module_options["mtd_file"])
xml_tree = ET.parse(module_options["mtd_file"])
root = xml_tree.getroot()
zenith_azimuth = []
for elem in root[1]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
# %end

import grass.script as gscript
import xml.etree.ElementTree as et
import xml.etree.ElementTree as ET
from datetime import datetime
import os
import math
Expand Down Expand Up @@ -258,7 +258,7 @@ def main():
)

# Create xml "tree" for reading parameters from metadata
tree = et.parse(mtd_file)
tree = ET.parse(mtd_file)
root = tree.getroot()

# Start reading the xml file
Expand Down
4 changes: 2 additions & 2 deletions src/imagery/i.variance/i.variance.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def FindMaxima(numbers):


def main():
import matplotlib # required by windows
import matplotlib as mpl # required by windows

matplotlib.use("wxAGG") # required by windows
mpl.use("wxAGG") # required by windows
import matplotlib.pyplot as plt

input = options["input"]
Expand Down
4 changes: 2 additions & 2 deletions src/raster/r.agent/libagent/playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from libagent import error

import numpy
import numpy as np


class Playground(object):
Expand Down Expand Up @@ -126,7 +126,7 @@ def createlayer(self, layername, filename=False, force=False):
"""
r = self.region["rows"]
c = self.region["cols"]
layer = numpy.zeros(r * c).reshape((r, c))
layer = np.zeros(r * c).reshape((r, c))

if filename:
# TODO import from file
Expand Down
4 changes: 2 additions & 2 deletions src/raster/r.area.createweight/r.area.createweight.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@
import math as ma

# Import pyplot library
import matplotlib
import matplotlib as mpl
import matplotlib.pyplot as plt

# Import literal_eval
from ast import literal_eval
from copy import deepcopy

# Use a non-interactive backend: prevent the figure from popping up
matplotlib.use("Agg")
mpl.use("Agg")


# For list of files to cleanup
Expand Down
8 changes: 4 additions & 4 deletions src/raster/r.boxplot/r.boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,15 @@

def lazy_import_py_modules(backend):
"""Lazy import Py modules"""
global matplotlib
global mpl
global plt

# lazy import matplotlib
try:
import matplotlib
import matplotlib as mpl

if backend is None:
matplotlib.use("WXAgg")
mpl.use("WXAgg")
from matplotlib import pyplot as plt
except ModuleNotFoundError:
gs.fatal(_("Matplotlib is not installed. Please, install it."))
Expand Down Expand Up @@ -434,7 +434,7 @@ def get_valid_color(color):
"""
if ":" in color:
color = [int(x) / 255 for x in color.split(":")]
if not matplotlib.colors.is_color_like(color):
if not mpl.colors.is_color_like(color):
gs.fatal(_("{} is not a valid color.").format(color))
return color

Expand Down
4 changes: 2 additions & 2 deletions src/raster/r.colors.cubehelix/r.colors.cubehelix.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ def main(options, flags):
# as_cmap ignores n_colors in 0.7.0
# but we want n_colors to be exact when we are exporting
# the color table or doing discrete one
import matplotlib # required by windows
import matplotlib as mpl # required by windows

matplotlib.use("wxAGG") # required by windows
mpl.use("wxAGG") # required by windows
import matplotlib.colors as clr

cmap = clr.LinearSegmentedColormap.from_list("from_list", cmap, N=n_colors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ def main():
)
)

import matplotlib
import matplotlib as mpl

matplotlib.use("wxAGG") # required by windows
mpl.use("wxAGG") # required by windows
import matplotlib.pyplot as plt

# Input variables
Expand Down
12 changes: 6 additions & 6 deletions src/raster/r.edm.eval/r.edm.eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ def cleanup():

def lazy_import_matplotlib():
"""Lazy import matplotlib modules"""
global matplotlib
global mpl
global plt
try:
import matplotlib
import matplotlib as mpl

matplotlib.use("WXAgg")
mpl.use("WXAgg")
from matplotlib import pyplot as plt
except ModuleNotFoundError:
gs.fatal(_("Matplotlib is not installed. Please, install it."))
Expand Down Expand Up @@ -365,7 +365,7 @@ def random_color():
:return list with rgb elements
"""
hex_color = "#{:06x}".format(random.randint(0, 0xFFFFFF))
return matplotlib.colors.hex2color(hex_color)
return mpl.colors.hex2color(hex_color)


def get_valid_color(color):
Expand All @@ -379,9 +379,9 @@ def get_valid_color(color):
color = [int(x) for x in color.split(":")]
if max(color) > 1:
color[:] = [x / 255 for x in color]
if not matplotlib.colors.is_color_like(color):
if not mpl.colors.is_color_like(color):
gs.fatal(_("{} is not a valid color.").format(color))
color = matplotlib.colors.to_rgba(color)
color = mpl.colors.to_rgba(color)
return color


Expand Down
8 changes: 4 additions & 4 deletions src/raster/r.fidimo/r.fidimo.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def cleanup():

def main():
# lazy import required numpy and scipy modules
import numpy
import numpy as np
from scipy import stats
from scipy import optimize

Expand Down Expand Up @@ -1343,10 +1343,10 @@ def cdf(x):

RealisedDensity = garray.array()
if options["seed2"]:
numpy.random.seed(int(options["seed2"]))
RealisedDensity[...] = numpy.random.multinomial(
np.random.seed(int(options["seed2"]))
RealisedDensity[...] = np.random.multinomial(
n_fish,
(CorrectedDensity / numpy.sum(CorrectedDensity)).flat,
(CorrectedDensity / np.sum(CorrectedDensity)).flat,
size=1,
).reshape(CorrectedDensity.shape)

Expand Down
4 changes: 2 additions & 2 deletions src/raster/r.futures/r.futures.demand/r.futures.demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ def main():
demand = {}
i = 0
if plot:
import matplotlib
import matplotlib as mpl

matplotlib.use("Agg")
mpl.use("Agg")
import matplotlib.pyplot as plt

n_plots = int(np.ceil(np.sqrt(len(subregionIds))))
Expand Down
4 changes: 2 additions & 2 deletions src/raster/r.hypso/r.hypso.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ def findint(kl, f):


def plotImage(x, y, image, type, xlabel, ylabel, title):
import matplotlib # required by windows
import matplotlib as mpl # required by windows

matplotlib.use("wxAGG") # required by windows
mpl.use("wxAGG") # required by windows
import matplotlib.pyplot as plt

plt.plot(x, y, type)
Expand Down
14 changes: 7 additions & 7 deletions src/raster/r.popgrowth/r.popgrowth.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@


# import required numpy/scipy modules
import numpy
import numpy as np


def cleanup():
Expand Down Expand Up @@ -203,22 +203,22 @@ def main():
def exponential_mod(n0, r, t):
n = n0
for t in range(t):
n = 1.0 * n * numpy.exp(r)
n = 1.0 * n * np.exp(r)
if flags["i"]:
# n = vprob_round(n) #function not mature yet (takes partly long time, problems with NaNs)
n = numpy.round(n)
n = np.round(n)
return n

# Ricker Model
def ricker_mod(n0, r, k, t):
n = n0
for t in range(t):
numpy.seterr(invalid="ignore")
n = 1.0 * n * numpy.exp(r * (1 - (n / k)))
numpy.seterr(invalid="warn")
np.seterr(invalid="ignore")
n = 1.0 * n * np.exp(r * (1 - (n / k)))
np.seterr(invalid="warn")
if flags["i"]:
# n = vprob_round(n) #function not mature yet (takes partly long time, problems with NaNs)
n = numpy.round(n)
n = np.round(n)
return n

################# Exponential Model #################
Expand Down
10 changes: 5 additions & 5 deletions src/raster/r.series.boxplot/r.series.boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ def main(options, flags):

# lazy import matplotlib
try:
import matplotlib
import matplotlib as mpl

matplotlib.use("WXAgg")
mpl.use("WXAgg")
from matplotlib import pyplot as plt
except ModuleNotFoundError:
gs.fatal(_("matplotlib is not installed"))
Expand Down Expand Up @@ -295,8 +295,8 @@ def main(options, flags):
bxcolor = [float(_x) / 255 for _x in bxcolor]
else:
bxcolor = set_bxcolor.replace(" ", "")
if matplotlib.colors.is_color_like(bxcolor):
bxcolor = matplotlib.colors.to_rgba(bxcolor)
if mpl.colors.is_color_like(bxcolor):
bxcolor = mpl.colors.to_rgba(bxcolor)
else:
gs.fatal(
_(
Expand Down Expand Up @@ -326,7 +326,7 @@ def main(options, flags):
flier_color = [int(_x) / 255 for _x in options["flier_color"].split(":")]
else:
flier_color = options["flier_color"]
if not matplotlib.colors.is_color_like(flier_color):
if not mpl.colors.is_color_like(flier_color):
gs.fatal(_("{} is not a valid color").format(options["flier_color"]))
if bool(options["text_labels"]):
list_text_labels = options["text_labels"].split(",")
Expand Down
4 changes: 2 additions & 2 deletions src/raster/r.width.funct/r.width.funct.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ def main():


def plotImage(x, y, image, type, xlabel, ylabel, title):
import matplotlib # required by windows
import matplotlib as mpl # required by windows

matplotlib.use("wxAGG") # required by windows
mpl.use("wxAGG") # required by windows
import matplotlib.pyplot as plt

plt.plot(x, y, type)
Expand Down
8 changes: 4 additions & 4 deletions src/temporal/t.rast.boxplot/t.rast.boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@

def lazy_import_py_modules():
"""Lazy import Py modules"""
global matplotlib
global mpl
global plt

# lazy import matplotlib
try:
import matplotlib
import matplotlib as mpl

matplotlib.use("WXAgg")
mpl.use("WXAgg")
from matplotlib import pyplot as plt
except ModuleNotFoundError:
gs.fatal(_("Matplotlib is not installed. Please, install it."))
Expand Down Expand Up @@ -354,7 +354,7 @@ def get_valid_color(color):
"""
if ":" in color:
color = [int(x) / 255 for x in color.split(":")]
if not matplotlib.colors.is_color_like(color):
if not mpl.colors.is_color_like(color):
gs.fatal(_("{} is not a valid color.").format(color))
return color

Expand Down
Loading

0 comments on commit bf40491

Please sign in to comment.