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

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 57a91fc was 57a91fc, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 5 years ago

cherrypicked changes from master to sascalc (except PR#190)

  • Property mode set to 100644
File size: 2.4 KB
Line 
1"""
2class Loader  to load any kind of file
3"""
4
5from __future__ import print_function
6
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
21
22    def set_filename(self, path=None):
23        """
24        Store path into a variable.If the user doesn't give
25        a path as a parameter a pop-up
26        window appears to select the file.
27
28        :param path: the path given by the user
29
30        """
31        self.filename = path
32
33    def get_filename(self):
34        """ return the file's path"""
35        return self.filename
36
37    def set_values(self):
38        """ Store the values loaded from file in local variables"""
39        if self.filename is not None:
40            input_f =  open(self.filename, 'r')
41            buff = input_f.read()
42            lines = buff.split('\n')
43            self.x = []
44            self.y = []
45            self.dx = []
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])
53
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:
59                    print("READ ERROR", line)
60            # Sanity check
61            if not len(self.x) == len(self.dx):
62                raise ValueError("x and dx have different length")
63            if not len(self.y) == len(self.dy):
64                raise ValueError("y and dy have different length")
65
66
67    def get_values(self):
68        """ Return x, y, dx, dy"""
69        return self.x, self.y, self.dx, self.dy
70
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()
80
81
82if __name__ == "__main__":
83    load = Load()
84    load.set_filename("testdata_line.txt")
85    print(load.get_filename())
86    load.set_values()
87    print(load.get_values())
88
Note: See TracBrowser for help on using the repository browser.