Changeset 1808
- Timestamp:
- 24/01/12 16:01:23 (4 months ago)
- File:
-
- 1 edited
-
trunk/bindings/python/nxs/tree.py (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/bindings/python/nxs/tree.py
r1806 r1808 133 133 * component('nxclass') return group entries of a particular class 134 134 * dir() print the list of entries in the group 135 * tree printthe list of entries and subentries in the group135 * tree return the list of entries and subentries in the group 136 136 * plot() plot signal and axes for the group, if available 137 137 … … 515 515 # find gids for targets 516 516 for target in gid.iterkeys(): 517 #sprint "target",target518 517 self.openpath(target) 519 518 # Can't tell from the name if we are linking to a group or … … 530 529 # ignore self-links 531 530 parent = "/".join(path.split("/")[:-1]) 532 #print "link %s -> %s"%(parent,target)533 531 self.openpath(parent) 534 532 self.makelink(gid[target]) … … 711 709 712 710 tree: 713 Print the object's tree.711 Return the object's tree as a string. 714 712 715 713 It invokes the 'dir' method with both 'attrs' and 'recursive' 716 set to True. Note that this methodis defined as a property attribute and714 set to True. Note that this is defined as a property attribute and 717 715 does not require parentheses. 718 716 … … 784 782 785 783 def walk(self): 786 print "yielding",self.nxname,self.nxclass787 784 if False: yield 788 785 … … 801 798 def tree(self): 802 799 """ 803 Print the directory tree.800 Return the directory tree as a string. 804 801 805 802 The tree contains all child objects of this object and their children. … … 807 804 to True. 808 805 """ 809 return self._str_tree(attrs=True,recursive=True)810 811 @property812 def tree_with_attrs(self, attrs=True):813 """Return directory tree string"""814 806 return self._str_tree(attrs=True,recursive=True) 815 807 … … 913 905 return self._changed 914 906 915 def set_unchanged(self ):907 def set_unchanged(self, recursive=False): 916 908 """ 917 909 Set an object's change status to unchanged. 918 910 """ 919 self._changed = False 911 if recursive: 912 for node in self.walk(): 913 node._changed = False 914 else: 915 self._changed = False 920 916 921 917 def _getclass(self): … … 1185 1181 1186 1182 tree: 1187 Printthe NXfield's tree.1183 Return the NXfield's tree. 1188 1184 1189 1185 It invokes the 'dir' method with both 'attrs' and 'recursive' 1190 set to True. Note that this methodis defined as a property attribute and1186 set to True. Note that this is defined as a property attribute and 1191 1187 does not require parentheses. 1192 1188 … … 1214 1210 """ 1215 1211 1216 def __init__(self, value=None, name='field', dtype=None, shape=(), attrs={},group=None,1217 **attr):1212 def __init__(self, value=None, name='field', dtype=None, shape=(), group=None, 1213 attrs={}, **attr): 1218 1214 if isinstance(value, list) or isinstance(value, tuple): 1219 1215 value = np.array(value) … … 2117 2113 self._class = opts["nxclass"] 2118 2114 del opts["nxclass"] 2115 if "group" in opts.keys(): 2116 self._group = opts["group"] 2117 del opts["group"] 2119 2118 for k,v in opts.items(): 2120 2119 setattr(self, k, v) … … 2255 2254 """ 2256 2255 if key in self.entries: 2257 infile = self[key]._infile 2258 attrs = self[key].attrs 2259 if isinstance(self[key], NXlink): 2256 infile = self._entries[key]._infile 2257 if isinstance(self._entries[key], NXlink): 2260 2258 if self._entries[key].nxlink: 2261 2259 setattr(self._entries[key].nxlink.nxgroup, key, value) 2262 2260 return 2261 attrs = self._entries[key].attrs 2263 2262 else: 2264 2263 infile = None … … 2341 2340 else: 2342 2341 raise NeXusError, "Link target must be an NXobject" 2342 2343 def read(self): 2344 """ 2345 Read the NXgroup and all its children from the NeXus file. 2346 """ 2347 if self.nxfile: 2348 with self as path: 2349 n, nxname, nxclass = path.getgroupinfo() 2350 if nxclass != self.nxclass: 2351 raise NeXusError("The NeXus group class does not match the file") 2352 self._setattrs(path.getattrs()) 2353 entries = path.entries() 2354 for name,nxclass in entries: 2355 path = self.nxpath + '/' + name 2356 if nxclass == 'SDS': 2357 attrs = self.nxfile.getattrs() 2358 if 'target' in attrs and attrs['target'] != path: 2359 self._entries[name] = NXlinkfield(target=attrs['target']) 2360 else: 2361 self._entries[name] = NXfield(name=name) 2362 else: 2363 attrs = self.nxfile.getattrs() 2364 if 'target' in attrs and attrs['target'] != path: 2365 self._entries[name] = NXlinkgroup(name=name, 2366 target=attrs['target']) 2367 else: 2368 self._entries[name] = NXgroup(nxclass=nxclass) 2369 self._entries[name]._group = self 2370 #Make sure non-linked variables are processed first. 2371 for entry in self._entries.values(): 2372 for node in entry.walk(): 2373 if not isinstance(node, NXlink): node.read() 2374 for entry in self._entries.values(): 2375 for node in entry.walk(): 2376 if isinstance(node, NXlink): node.read() 2377 self._infile = self._saved = self._changed = True 2378 else: 2379 raise IOError("Data is not attached to a file") 2343 2380 2344 2381 def write(self): … … 2442 2479 return self[obj.nxname] 2443 2480 return None 2481 2482 def _set_signal(self, signal): 2483 """ 2484 Setter for the signal attribute. 2485 2486 The argument should be a valid NXfield within the group. 2487 """ 2488 self[signal.nxname].signal = NXattr(1) 2444 2489 2445 2490 def _axes(self): … … 2456 2501 return [axes[key] for key in sorted(axes.keys())] 2457 2502 2503 def _set_axes(self, axes): 2504 """ 2505 Setter for the signal attribute. 2506 2507 The argument should be a list of valid NXfields within the group. 2508 """ 2509 if not isinstance(axes, list): 2510 axes = [axes] 2511 self.nxsignal.axes = NXattr(":".join([axis.nxname for axis in axes])) 2512 2458 2513 def _errors(self): 2459 2514 """ … … 2488 2543 return self._entries 2489 2544 2490 nxsignal = property(_signal, "Signal NXfield within group")2491 nxaxes = property(_axes, "List of axes within group")2545 nxsignal = property(_signal, _set_signal, "Signal NXfield within group") 2546 nxaxes = property(_axes, _set_axes, "List of axes within group") 2492 2547 nxerrors = property(_errors, "Errors NXfield within group") 2493 2548 nxtitle = property(_title, "Title for group plot") … … 2570 2625 def __init__(self, target=None, name='link', group=None): 2571 2626 self._group = group 2627 self._class = "NXlink" 2572 2628 if isinstance(target, NXobject): 2573 2629 self._name = target.nxname … … 2629 2685 attrs = property(_getattrs,doc="NeXus attributes for object") 2630 2686 2687 def read(self): 2688 """ 2689 Read the linked NXobject. 2690 """ 2691 self.nxlink.read() 2692 self._infile = self._saved = self._changed = True 2693 2631 2694 2632 2695 class NXlinkfield(NXlink, NXfield): … … 2649 2712 path.makelink(target) 2650 2713 self._infile = self._saved = True 2651 2652 2714 2653 2715 def get(self, offset, size): … … 2785 2847 of axes, for multidimensional data. These arguments can either be NXfield 2786 2848 objects or Numpy arrays, which are converted to NXfield objects with default 2787 names. 2788 2849 names. Alternatively, the signal and axes NXfields can be defined using the 2850 'nxsignal' and 'nxaxes' properties. See the examples below. 2851 2789 2852 Various arithmetic operations (addition, subtraction, multiplication, 2790 2853 and division) have been defined for combining NXdata groups with other … … 2801 2864 Methods 2802 2865 ------- 2803 plot(self, over=False, log=False, **opts)2866 plot(self, fmt, over=False, log=False, logy=False, logx=False, **opts) 2804 2867 Plot the NXdata group using the defined signal and axes. Valid 2805 2868 Matplotlib parameters, specifying markers, colors, etc, can be 2806 specified using the 'opts' dictionary. 2869 specified using format argument or through keyword arguments. 2870 2871 logplot(self, fmt, over=False, logy=False, logx=False, **opts) 2872 Plot the NXdata group using the defined signal and axes with 2873 the intensity plotted on a logarithmic scale. In one-dimensional 2874 plots, this is the y-axis. In two-dimensional plots, it is the 2875 color scale. 2876 2877 oplot(self, fmt, **opts) 2878 Plot the NXdata group using the defined signal and axes over 2879 the current plot. 2807 2880 2808 2881 moment(self, order=1) … … 2813 2886 Examples 2814 2887 -------- 2888 There are three methods of creating valid NXdata groups with the 2889 signal and axes NXfields defined according to the NeXus standard. 2890 2891 1) Create the NXdata group with Numpy arrays that will be assigned 2892 default names. 2893 2815 2894 >>> x = np.linspace(0, 2*np.pi, 101) 2816 2895 >>> line = NXdata(sin(x), x) … … 2819 2898 @axes = x 2820 2899 @signal = 1 2821 x = float64(101) 2822 >>> X, Y = np.meshgrid(x, x) 2823 >>> z = NXfield(sin(X) * sin(Y), name='intensity') 2900 axis1 = float64(101) 2901 2902 2) Create the NXdata group with NXfields that have their internal 2903 names already assigned. 2904 2905 >>> x = NXfield(linspace(0,2*pi,101), name='x') 2906 >>> y = NXfield(linspace(0,2*pi,101), name='y') 2907 >>> X, Y = np.meshgrid(x, y) 2908 >>> z = NXfield(sin(X) * sin(Y), name='z') 2824 2909 >>> entry = NXentry() 2825 >>> entry.grid = NXdata(z, (x, x))2910 >>> entry.grid = NXdata(z, (x, y)) 2826 2911 >>> grid.tree() 2827 2912 entry:NXentry 2828 2913 grid:NXdata 2829 axis1= float64(101)2830 axis2= float64(101)2831 intensity= float64(101x101)2832 @axes = axis1:axis22914 x = float64(101) 2915 y = float64(101) 2916 z = float64(101x101) 2917 @axes = x:y 2833 2918 @signal = 1 2834 2919 2835 See the NXgroup documentation for more details. 2920 3) Create the NXdata group with keyword arguments defining the names 2921 and set the signal and axes using the nxsignal and nxaxes properties. 2922 2923 >>> x = linspace(0,2*pi,101) 2924 >>> y = linspace(0,2*pi,101) 2925 >>> X, Y = np.meshgrid(x, y) 2926 >>> z = sin(X) * sin(Y) 2927 >>> entry = NXentry() 2928 >>> entry.grid = NXdata(z=sin(X)*sin(Y), x=x, y=y) 2929 >>> entry.grid.nxsignal = entry.grid.z 2930 >>> entry.grid.nxaxes = [entry.grid.x.entry.grid.y] 2931 >>> grid.tree() 2932 entry:NXentry 2933 grid:NXdata 2934 x = float64(101) 2935 y = float64(101) 2936 z = float64(101x101) 2937 @axes = x:y 2938 @signal = 1 2836 2939 """ 2837 2940
Note: See TracChangeset
for help on using the changeset viewer.
