Skip to content

Commit

Permalink
Merge pull request #63 from Serapieum-of-alex/array/plot-extent
Browse files Browse the repository at this point in the history
array/plot-extent to the Array plot
  • Loading branch information
MAfarrag authored Jan 11, 2024
2 parents f8e3141 + 33c587a commit 1c15631
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ dmypy.json
*.gif
*.mov
*.mp4
.run/*
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ History
* Add a colors module to handle issues related to
- Converting colors from one format to another
- Creating colormaps

0.4.1 (2024-1-11)
------------------
* add extent to the array plot.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ Installing `cleopatra` from the `conda-forge` channel can be achieved by:
conda install -c conda-forge cleopatra
```

It is possible to list all of the versions of `cleopatra` available on your platform with:
It is possible to list all the versions of `cleopatra` available on your platform with:

```
conda search cleopatra --channel conda-forge
```

## Install from Github
to install the last development to time you can install the library from github
## Install from GitHub
to install the last development to time, you can install the library from GitHub
```
pip install git+https://github.com/MAfarrag/cleopatra
pip install git+https://github.com/Serapieum-of-alex/cleopatra
```

## pip
to install the last release you can easly use pip
to install the last release, you can easily use pip
```
pip install cleopatra==0.4.0
pip install cleopatra==0.4.1
```

Quick start
Expand Down
87 changes: 49 additions & 38 deletions cleopatra/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
self,
array: np.ndarray,
exclude_value: List = np.nan,
extent: List = None,
rgb: List[int] = None,
surface_reflectance: int = 10000,
cutoff: List = None,
Expand All @@ -46,12 +47,13 @@ def __init__(
array: [numpy array]
array.
exclude_value : [numeric]
value used to fill cells out of the domain. Optional, Default is np.nan
needed only in case of plotting array
value used to fill cells out of the domain. Optional, Default is np.nan.
extent: [List]
[xmin, ymin, xmax, ymax]. Default is None.
rgb: [List]
Default is [3,2,1]
surface_reflectance: [int]
Default is 10000
Default is 10000.
cutoff: [List]
clip the range of pixel values for each band. (take only the pixel values from 0 to value of the cutoff and
scale them back to between 0 and 1. Default is None.
Expand All @@ -72,6 +74,11 @@ def __init__(

array[mask] = np.nan

# convert the extent from [xmin, ymin, xmax, ymax] to [xmin, xmax, ymin, ymax] as required by matplotlib.
if extent is not None:
extent = [extent[0], extent[2], extent[1], extent[3]]
self.extent = extent

if rgb is not None:
self.rgb = True
# prepare to plot rgb plot only if there are three arrays
Expand Down Expand Up @@ -100,7 +107,7 @@ def __init__(
)

self.arr = array
# get the tick spacing that have 10 ticks only
# get the tick spacing that has 10 ticks only
self.ticks_spacing = (self._vmax - self._vmin) / 10
shape = array.shape
if len(shape) == 3:
Expand Down Expand Up @@ -227,7 +234,7 @@ def get_im_cbar(self, ax, arr: np.ndarray, ticks: np.ndarray):
vmax = self.default_options["vmax"]

if color_scale == 1:
im = ax.matshow(arr, cmap=cmap, vmin=vmin, vmax=vmax)
im = ax.matshow(arr, cmap=cmap, vmin=vmin, vmax=vmax, extent=self.extent)
cbar_kw = dict(ticks=ticks)
elif color_scale == 2:
im = ax.matshow(
Expand All @@ -236,6 +243,7 @@ def get_im_cbar(self, ax, arr: np.ndarray, ticks: np.ndarray):
norm=colors.PowerNorm(
gamma=self.default_options["gamma"], vmin=vmin, vmax=vmax
),
extent=self.extent,
)
cbar_kw = dict(ticks=ticks)
elif color_scale == 3:
Expand All @@ -249,6 +257,7 @@ def get_im_cbar(self, ax, arr: np.ndarray, ticks: np.ndarray):
vmin=vmin,
vmax=vmax,
),
extent=self.extent,
)
formatter = LogFormatter(10, labelOnlyBase=False)
cbar_kw = dict(ticks=ticks, format=formatter)
Expand All @@ -260,7 +269,7 @@ def get_im_cbar(self, ax, arr: np.ndarray, ticks: np.ndarray):
bounds = self.default_options["bounds"]
cbar_kw = dict(ticks=self.default_options["bounds"])
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
im = ax.matshow(arr, cmap=cmap, norm=norm)
im = ax.matshow(arr, cmap=cmap, norm=norm, extent=self.extent)
else:
im = ax.matshow(
arr,
Expand All @@ -270,6 +279,7 @@ def get_im_cbar(self, ax, arr: np.ndarray, ticks: np.ndarray):
vmin=vmin,
vmax=vmax,
),
extent=self.extent,
)
cbar_kw = dict(ticks=ticks)

Expand Down Expand Up @@ -331,38 +341,38 @@ def plot(
Parameters
----------
points : [array]
3 column array with the first column as the value you want to display for the point, the second is the rows
3 column array with the first column as the value you want to display for the point, the second is the rows'
index of the point in the array, and the third column as the column index in the array.
- the second and third column tells the location of the point in the array.
point_color: [str]
color.
point_size: [Any]
size of the point.
pid_color: [str]
the color of the anotation of the point. Default is blue.
the color of the annotation of the point. Default is blue.
pid_size: [Any]
size of the point annotation.
**kwargs : [dict]
**kwargs: [dict]
keys:
figsize : [tuple], optional
figure size. The default is (8,8).
title : [str], optional
title: [str], optional
title of the plot. The default is 'Total Discharge'.
title_size : [integer], optional
title_size: [integer], optional
title size. The default is 15.
orientation : [string], optional
orintation of the colorbar horizontal/vertical. The default is 'vertical'.
rotation : [number], optional
rotation of the colorbar label. The default is -90.
orientation : [string], optional
orintation of the colorbar horizontal/vertical. The default is 'vertical'.
cbar_length : [float], optional
ratio to control the height of the colorbar. The default is 0.75.
ticks_spacing : [integer], optional
Spacing in the colorbar ticks. The default is 2.
cbar_label_size : integer, optional
orientation: [string], optional
orientation of the color bar horizontal/vertical. The default is 'vertical'.
rotation: [number], optional
rotation of the color bar label. The default is -90.
orientation: [string], optional
orientation of the color bar horizontal/vertical. The default is 'vertical'.
cbar_length: [float], optional
ratio to control the height of the color bar. The default is 0.75.
ticks_spacing: [integer], optional
Spacing in the color bar ticks. The default is 2.
cbar_label_size: integer, optional
size of the color bar label. The default is 12.
cbar_label : str, optional
cbar_label: str, optional
label of the color bar. The default is 'Discharge m3/s'.
color_scale : integer, optional
there are 5 options to change the scale of the colors. The default is 1.
Expand All @@ -372,26 +382,26 @@ def plot(
4- color_scale 4 is the PowerNorm scale
5- color_scale 5 is the BoundaryNorm scale
------------------------------------------------------------------
gamma : [float], optional
value needed for option 2 . The default is 1./2..
line_threshold : [float], optional
gamma: [float], optional
value needed for option 2. The default is 1./2.
line_threshold: [float], optional
value needed for option 3. The default is 0.0001.
line_scale : [float], optional
line_scale: [float], optional
value needed for option 3. The default is 0.001.
bounds: [List]
a list of number to be used as a discrete bounds for the color scale 4.Default is None,
midpoint : [float], optional
midpoint: [float], optional
value needed for option 5. The default is 0.
------------------------------------------------------------------
cmap : [str], optional
cmap: [str], optional
color style. The default is 'coolwarm_r'.
display_cell_value : [bool]
display_cell_value: [bool]
True if you want to display the values of the cells as a text
num_size : integer, optional
size of the numbers plotted intop of each cells. The default is 8.
background_color_threshold : [float/integer], optional
num_size: integer, optional
size of the numbers plotted on top of each cell. The default is 8.
background_color_threshold: [float/integer], optional
threshold value if the value of the cell is greater, the plotted
numbers will be black and if smaller the plotted number will be white
numbers will be black, and if smaller the plotted number will be white
if None given the maxvalue/2 will be considered. The default is None.
Returns
Expand Down Expand Up @@ -457,11 +467,12 @@ def plot(
ax.set_title(
self.default_options["title"], fontsize=self.default_options["title_size"]
)
ax.set_xticklabels([])
ax.set_yticklabels([])

ax.set_xticks([])
ax.set_yticks([])
if self.extent is None:
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xticks([])
ax.set_yticks([])

optional_display = {}
if self.default_options["display_cell_value"]:
Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

black >=23.11.0
darglint >=1.8.1
flake8 >=6.1.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name="cleopatra",
version="0.4.0",
version="0.4.1",
description="visualization package",
author="Mostafa Farrag",
author_email="moah.farag@gmail.come",
Expand Down
14 changes: 2 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,12 @@ def background_color_threshold():


@pytest.fixture(scope="module")
def IDsize() -> int:
return 20


@pytest.fixture(scope="module")
def IDcolor() -> str:
return "green"


@pytest.fixture(scope="module")
def Gaugesize() -> int:
def gauge_size() -> int:
return 100


@pytest.fixture(scope="module")
def Gaugecolor() -> str:
def gauge_color() -> str:
return "blue"


Expand Down
19 changes: 17 additions & 2 deletions tests/test_plot_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ def test_plot_numpy_array(
fig, ax = array.plot(title="Flow Accumulation")
assert isinstance(fig, Figure)

def test_plot_numpy_array_with_extent(
self,
arr: np.ndarray,
no_data_value: float,
):
extent = [
-75.60441003848668,
4.235054115032001,
-75.09878783366909,
4.704560448076901,
]
array = Array(arr, exclude_value=[no_data_value], extent=extent)
fig, ax = array.plot(title="Flow Accumulation")
assert isinstance(fig, Figure)

def test_plot_array_color_scale_1(
self,
arr: np.ndarray,
Expand Down Expand Up @@ -169,12 +184,12 @@ def test_plot_array_with_points(
id_size: int,
id_color: str,
point_size: int,
Gaugecolor: str,
gauge_color: str,
):
array = Array(arr, exclude_value=[no_data_value])
fig, ax = array.plot(
points=points,
point_color=Gaugecolor,
point_color=gauge_color,
point_size=point_size,
id_color=id_color,
id_size=id_size,
Expand Down

0 comments on commit 1c15631

Please sign in to comment.