def setUnit(self, new_y_unit): if self.checkUnit(new_y_unit): old_unit = self.getUnits()[1] self.__units[1] = new_y_unit coeff = self.findCoeff('V', new_y_unit) self.__y = self.getF(coeff * self.getX()) print("Unit of signal changed form {} to {}. Y-axis value are now {} time bigger that the original signal " "(unit with coeff 1).".format(old_unit, new_y_unit, coeff)) msg = "We use the function, not the y value to change the curve !" print(warningText(msg)) else: msg = "Incorrect unit value, unit of y-axis is currently {}.".format(self.getUnits()[1]) print(warningText(msg))
def __init__(self, name, units, x, y=None, f=None): # Name self.__name = str(name) # Units if len(units) == 2: self.__units = units else: msg = "Units must be a list of size 2 (for x and y axis). Please define units !" print(warningText(msg)) self.__units = None # x-axis self.__x = np.array(x) # Axis Names self.__axisNames = self.findAxisNames() # y axis and relation if f is not None and y is None: self.__f = f self.__y = f(x) pass # Equation and y : compare and ask witch are the good elif f is not None and y is not None: self.__f = f self.__y = y self.autoCompareSignal() # Not equation and y : create y elif f is None and y is not None: self.__y = y self.__f = None msg = "No function has been defined." print(warningText(msg)) # Not equation and not y : say and None else: msg = "No possibility to interpret the signal. Neither the ordinates nor a function has been defined. " \ "Please define at least one of the two parameters" print(warningText(msg)) self.__y = None self.__f = None
def findAxisNames(self): axis_names = ['Unknown', 'Unknown'] if 's' in self.getUnits()[0]: axis_names[0] = 'Temps' try: axis_names[1] = self.__units_d[self.getUnits()[1][-1]] except KeyError: msg = "y-axis name can not be define, please define them manually." print(warningText(msg)) return axis_names
def setName(self, new_name): """ Change the name of the object and check that the length of the new name is behind 144 charters. :param new_name: New name for the title of the plot :type new_name: str :return: Nothing, it just change the instance variable __name """ if len(new_name) <= 144: old_name = self.getName() self.__name = str(new_name) print('Name {} is change by {}.'.format(old_name, self.getName())) else: msg = "The requested name has a length of more than 144 characters, please change the name." print(warningText(msg))
def plot_signals(x, signals, signals_names, x_label="", y_label="", title="", saving=False): """ Plot the different signals of the signals list as function of x :param x: List of dimension n containing the abscissas at which the signals are evaluated :type x: numpy.ndarray :param signals: List m by n of m signals evaluate in n points (the points of x list) :type signals: list of numpy.ndarray :param signals_names: List of dimension m containing all the names of the signals :type signals_names: list of str :param x_label: Name of horizontal axis :type x_label: str :param y_label: Name of the vertical axis :type y_label: str :param title: Title of the plot figure :type title: str :param saving: To know of the plot will be save or not :type saving: bool :return: Show a plot of all the signals into a unique graph """ # Size of parameters n = len(x) m = len(signals) # Errors if len(signals_names) != m: raise ValueError("'signals' and 'signals_name' must have the same size") # Plot and errors for i in range(m): if len(signals[i]) != n: msg = "WARNING : All signal of 'signals' must by have a length equals to {} (length of x vector). Error " \ "occurred for signal {}".format(n, i) print(warningText(msg)) continue plt.plot(x, signals[i], label=signals_names[i]) plt.xlabel(x_label) plt.ylabel(y_label) plt.legend() if title is not False: plt.title(title) plt.show() if saving: frame = str(input('Frame : ')) trans = bool(input('Transparent image ? (bool) : ')) datas = str(input('Data\'s ? (False or str) : ')) plt.savefig(frame, transparent=trans, metadata={'Description': datas})
def setPreferredUnit(self, unit): """ Changes the unit used to represent the graphs. What is most important is the prefix and therefore the order of magnitude. Be careful not to change the unit itself, otherwise the names of the axes will also be changed. It also check that the unit is correct and usable (see the definition of the class). :param unit: New unit to use :type unit: str """ if self.checkUnit(unit): old_unit = self.getPreferredUnit() self.__preferredUnit = unit self.__axisUnits = self.findAxisUnits() print('Preferred unit {} is change by {}.'.format( old_unit, self.getPreferredUnit())) else: msg = "This unit is not usable. Please change the unit. Current unit: {}".format( self.getPreferredUnit()) print(warningText(msg))
def compareSignals(self, *args, title=False, saving=False): """ :param args: :type args: Signal :return: """ signals_names = [self.getName()] signals = [self.getY()] for arg in args: if len(arg.getY()) == len(self.getX()): signals_names.append(arg.getName()) signals.append(arg.getY()) else: msg = "Signal {} have not a length of {}, comparaison can not be done. This signal is ignored". \ format(arg.getName(), len(self.getX())) print(warningText(msg)) continue plot_signals(self.getX(), signals, signals_names, x_label=self.getAxisLabels()[0], y_label=self.getAxisLabels()[1], title=title, saving=saving)
def findAxisNames(self): """ Find the name of each axis. The name of x axis is the name that is given bt the CSV file (first word at first line => see CSV structure that is use in the class description). The name of y axis is deduced from the unit of signals. :return: The name of x and y axis :rtype: list """ # x-axis with open(self.getPath(), 'r') as file: x_axis_name = file.readline().strip().split(';')[0] # y-axis try: y_axis_name = self.__units[self.getPreferredUnit()[-1]] except: y_axis_name = 'Error !' msg = "y-axis name can not be define, please define them manually." print(warningText(msg)) return [x_axis_name, y_axis_name]
def autoCompareSignal(self): msg = "An equation and a value of y have been given. Here is the comparison of the 2." plot_signals(self.getX(), [self.getY(), self.getF(self.getX())], ['Y list', 'Function'], x_label=self.getAxisLabels()[0], y_label=self.getAxisLabels()[1]) print(warningText(msg))