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

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249
Last change on this file since cb9640f was 57e48ca, checked in by Paul Kienzle <pkienzle@…>, 6 years ago

fix rcond requires float error in pr

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