source: sasview/src/sas/sascalc/pr/fit/Loader.py @ 009abcc

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249unittest-saveload
Last change on this file since 009abcc was 574adc7, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

convert sascalc to python 2/3 syntax

  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[a1b8fee]1from __future__ import print_function
2
[959eb01]3# class Loader  to load any king of file
4#import wx
5#import string
6import numpy as np
7
8class Load:
9    """
10    This class is loading values from given file or value giving by the user
11    """
12    def __init__(self, x=None, y=None, dx=None, dy=None):
13        raise NotImplementedError("a code search shows that this code is not active, and you are not seeing this message")
14        # variable to store loaded values
15        self.x = x
16        self.y = y
17        self.dx = dx
18        self.dy = dy
19        self.filename = None
[574adc7]20
[959eb01]21    def set_filename(self, path=None):
22        """
[574adc7]23        Store path into a variable.If the user doesn't give
[959eb01]24        a path as a parameter a pop-up
25        window appears to select the file.
[574adc7]26
[959eb01]27        :param path: the path given by the user
[574adc7]28
[959eb01]29        """
30        self.filename = path
[574adc7]31
[959eb01]32    def get_filename(self):
33        """ return the file's path"""
34        return self.filename
[574adc7]35
[959eb01]36    def set_values(self):
37        """ Store the values loaded from file in local variables"""
[ac07a3a]38        if self.filename is not None:
[959eb01]39            input_f =  open(self.filename, 'r')
40            buff = input_f.read()
41            lines = buff.split('\n')
42            self.x = []
43            self.y = []
[574adc7]44            self.dx = []
[959eb01]45            self.dy = []
46            for line in lines:
47                try:
48                    toks = line.split()
49                    x = float(toks[0])
50                    y = float(toks[1])
51                    dy = float(toks[2])
[574adc7]52
[959eb01]53                    self.x.append(x)
54                    self.y.append(y)
55                    self.dy.append(dy)
56                    self.dx = np.zeros(len(self.x))
57                except:
[9c3d784]58                    print("READ ERROR", line)
[959eb01]59            # Sanity check
60            if not len(self.x) == len(self.dx):
[574adc7]61                raise ValueError("x and dx have different length")
[959eb01]62            if not len(self.y) == len(self.dy):
[574adc7]63                raise ValueError("y and dy have different length")
64
65
[959eb01]66    def get_values(self):
67        """ Return x, y, dx, dy"""
68        return self.x, self.y, self.dx, self.dy
[574adc7]69
[959eb01]70    def load_data(self, data):
71        """ Return plottable"""
72        #load data
73        data.x = self.x
74        data.y = self.y
75        data.dx = self.dx
76        data.dy = self.dy
77        #Load its View class
78        #plottable.reset_view()
[574adc7]79
80
81if __name__ == "__main__":
[959eb01]82    load = Load()
83    load.set_filename("testdata_line.txt")
[574adc7]84    print(load.get_filename())
[959eb01]85    load.set_values()
[9c3d784]86    print(load.get_values())
[574adc7]87
Note: See TracBrowser for help on using the repository browser.