Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pydatview/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,15 @@ def exception2string(excp, iMax=40, prefix=' | ', prevStack=True):
# ---
# --------------------------------------------------------------------------------{
def isString(x):
b = x.dtype == object and isinstance(x.values[0], str)
return b
if len(x) == 0:
return False
if isinstance(x, list):
return isinstance(x[0], str)
if hasattr(x, 'values'): # handles pandas Series/DataFrame and xarray DataArray
val = x.values.flat[0]
else: # handles numpy array
val = x.flat[0]
return isinstance(val, str)

def isDate(x):
return np.issubdtype(x.dtype, np.datetime64)
Expand Down
18 changes: 12 additions & 6 deletions pydatview/plotdata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import numpy as np
import pandas
from pydatview.common import no_unit, unit, inverse_unit, splitunit, has_chinese_char
from pydatview.common import isString, isDate, getDt
from pydatview.common import unique, pretty_num, pretty_time, pretty_date
Expand All @@ -10,6 +11,7 @@
except AttributeError:
trapz = np.trapz

MAX_UNIQUE_STRING_TO_PLOT = 1000 # Potentially put this in user file

# --------------------------------------------------------------------------------}
# --- PlotDataList functions
Expand Down Expand Up @@ -70,6 +72,7 @@ def __init__(PD, x=None, y=None, sx='', sy=''):
PD.xyMeasInput1 = (None, None)
PD.xyMeasInput2 = (None, None)
PD.xyMeas = [(None,None)]*2 # 2 measures for now
PD.MAX_UNIQUE_STRING_TO_PLOT = MAX_UNIQUE_STRING_TO_PLOT

if x is not None and y is not None:
PD.fromXY(x,y,sx,sy)
Expand Down Expand Up @@ -118,12 +121,15 @@ def _post_init(PD, pipeline=None):


# --- Store stats
n=len(PD.y)
if n>1000:
if (PD.xIsString):
raise Exception('Error: x values contain more than 1000 string. This is not suitable for plotting.\n\nPlease select another column for table: {}\nProblematic column: {}\n'.format(PD.st,PD.sx))
if (PD.yIsString):
raise Exception('Error: y values contain more than 1000 string. This is not suitable for plotting.\n\nPlease select another column for table: {}\nProblematic column: {}\n'.format(PD.st,PD.sy))
n = len(PD.y)
if PD.xIsString:
nu = len(np.unique(PD.x))
if nu > PD.MAX_UNIQUE_STRING_TO_PLOT:
raise Exception(f'Error: x values contain more than {PD.MAX_UNIQUE_STRING_TO_PLOT} unique strings. This is not suitable for plotting.\n\nPlease select another column for table: {PD.st}\nProblematic column: {PD.sx}\n')
if PD.yIsString:
nu = len(np.unique(PD.y))
if nu > PD.MAX_UNIQUE_STRING_TO_PLOT:
raise Exception(f'Error: y values contain more than {PD.MAX_UNIQUE_STRING_TO_PLOT} unique strings. This is not suitable for plotting.\n\nPlease select another column for table: {PD.st}\nProblematic column: {PD.sy}\n')

PD.needChineseFont = has_chinese_char(PD.sy) or has_chinese_char(PD.sx)
# Stats of the raw data (computed once and for all, since it can be expensive for large dataset
Expand Down
18 changes: 16 additions & 2 deletions tests/test_plotdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import matplotlib.pyplot as plt


from pydatview.plotdata import PlotData
from pydatview.plotdata import PlotData, MAX_UNIQUE_STRING_TO_PLOT

class TestPlotData(unittest.TestCase):

Expand Down Expand Up @@ -115,7 +115,21 @@ def test_fatigue(self):
np.testing.assert_almost_equal(v, 9.4714702, 3)


def test_plotManyStrings(self):
# Test for an array of size less than MAX_UNIQUE_STRING_TO_PLOT
x = np.linspace(-2, 2, MAX_UNIQUE_STRING_TO_PLOT)
y = np.asarray([f"s_{i}" for i in range(1, len(x) + 1)])
PD = PlotData(x,y)
self.assertEqual(PD.xIsString, False)
self.assertEqual(PD.yIsString, True)
# Test for a larger array
x = np.linspace(-2, 2, MAX_UNIQUE_STRING_TO_PLOT+1)
y = np.asarray([f"s_{i}" for i in range(1, len(x) + 1)])
with self.assertRaises(Exception):
PD = PlotData(x,y)



if __name__ == '__main__':
unittest.main()
TestPlotData().test_plotManyStrings()
# unittest.main()
Loading