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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since f26b7c1 was a1b8fee, checked in by andyfaff, 8 years ago

MAINT: from future import print_function

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