是否可以做到“无状态”? matlab编程/如何避免检查数据完整性?

发布于 2025-01-02 21:14:02 字数 911 浏览 1 评论 0原文

我的日常工作流程是这样的:

  • 获取原始数据(~50GB)
  • 解析原始数据计时信息并根据计时信息构建原始数据结构(结构/对象)(什么事件发生在何时、以何种顺序、以什么方式发生)文件,同时发生的其他事件等...)
  • 仅将原始数据的必要部分加载到从先前的计时信息中选择的结构/对象中(基本上这是一种子选择数据的方法)
  • 对于每个原始数据块,计算/提取某些指标,例如信号的 RMS、事件,其中数据>阈值、d'/z 分数,并将它们与
  • 给定先前计算的指标的结构/对象保存在一起,从不同数据通道加载相同时间片段的一些原始数据并比较某些内容等...
  • 可视化结果 x, y , z

我有两种处理这种数据/工作流程的方法:

  1. 使用 struct()
  2. 使用对象

两种情况都有一定的优点/缺点:

  1. struct:

    • 可以动态添加属性/字段
    • 每次将结构传递给函数时都必须检查结构的状态
    • 继续重写某些函数,因为每次我稍微更改结构时,我 a) 往往会忘记它已经存在一个函数,或者 b) 我编写一个新版本来处理结构状态的特殊情况。
  2. 对象:

    • 使用“get.property()”方法,我可以在函数/方法内访问属性之前检查属性的状态 ->允许进行数据一致性检查。
    • 我始终知道哪些方法适用于我的对象,因为它们是对象定义的一部分。
    • 每次添加新属性或方法时都需要清除类 - 非常烦人!

现在我的问题是:其他人如何处理这种情况?你如何组织你的数据?在结构中?在物体中?你如何处理状态检查?有没有办法在 matlab 中进行“无状态”编程?

My day to day work flow is something like this:

  • acquire raw data (~50GB)
  • parse raw data timing-information and build raw data structure (struct / object) from timing-information (what event occurred when, in which order, in what file, what other events occurred at the same time, etc ...)
  • load only the necessary parts of raw data into struct / object as selected from previous timing information (basically this is a way to sub-select data)
  • for each raw data chunk, calculate / extract certain metrics like RMS of signal, events where data > threshold, d' / z-score, and save them with struct / object
  • given the the previously calculated metrics, load some raw-data of same time episodes from different data channel and compare certain things, etc ...
  • visualize results x, y, z

I have two ways of dealing with this kind of data / workflow:

  1. use struct()
  2. use objects

There are certain advantages / disadvantages to both cases:

  1. struct:

    • can add properties / fields on the fly
    • have to check for state of struct every single time that I pass a struct to a function
    • keep re-writing certain functions because every time that I change the struct slightly I a) tend to forget that a function already exists for it or b) I write a new version that handles a special case of the struct state.
  2. objects:

    • using 'get.property()' methods, I can check the state of a property before it get's accessed inside a function / method -> allows to do data consistency checks.
    • I always know which methods work with my object, since they are part of the object definition.
    • need to clear classes every time I add a new property or method - very annoying!

Now my question is: how do other people deal with this kind of situation? how do you organize your data? in structs? in objects? how do you handle state checks? is there a way to do 'stateless' programming in matlab?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装纯掩盖桑 2025-01-09 21:14:02

我喜欢使用物体。您不需要在每次更改时都调用明确的类。删除“旧”对象的所有实例就足够了。

我经常继承的两个非常强大的附加功能是句柄和动态道具。

  1. 句柄使对象表现为句柄。很高兴能够解决 matlab 的更改时复制行为。
  2. 动态道具 - 进行一些元编程。

关于一致性检查 - 为什么在使用 set.property 时不进行一致性检查?


编辑 1:

使用数据库的简化类:

classdef measurement
   class
   id
properties (SetAccess = private)

end
methods
function obj = measurement(varargin)
  obj.id = varargin{1};
end

    function cs = get.class(obj)
       if isempty(obj.id)
                    cs = '';
                    return
       end
       mc = mydb.local; % use some tricks here to keep the connection open
         tmp = mym(mc,...
                    'SELECT class FROM measurements WHERE id = {Si}'...
                    ,obj.id);
         cs = tmp{1};
     end
end

编辑 2:事件示例 - 观察者

classdef ObservableClass < handle

    properties 
        ListObservers=[]
        data
    end

    methods
        function obj = ObservableClass(varargin)
            obj.data = rand(100,2);
        end

        function addObserver(obj,observer)
            obj.ListObservers = [obj.ListObservers,observer];
        end

        function fireUpdate(obj)
            for i=1:numel(obj.ListObservers)
                obj.ListObservers(i).update();
            end
        end

        function set.data(obj,newData)
            obj.data = newData;
            obj.fireUpdate;
        end
    end
end

和侦听器:

 classdef ObservingPlot
    properties
        fig
        observedClass
    end

    methods
        function obj = ObservingPlot(varargin)
            obj.observedClass = varargin{1};
            obj.createPlot;
            obj.observedClass.addObserver(obj);
        end

        function createPlot(obj)
            obj.fig=figure;
            plot(obj.observedClass.data);
        end

        function update(obj)
            gcf(obj.fig)
            clf
            plot(obj.observedClass.data);
        end
    end

end

示例:

a = ObservableClass()
b = ObservingPlot(a)

然后您可以在执行以下操作时进行观察:a.data=rand(100,3) - 情节将立即改变。


编辑 3:一个简单的保存类

classdef SavingClass < handle

    properties 
        saveName
        data
    end

    methods
        function set.data(obj,input)
            if isempty(obj.saveName)
                obj.saveName = [tempname '.mat'];
            end
            save(obj.saveName,'input')
        end

        function out = get.data(obj)            
                out = [];
               if exist(obj.saveName,'file')                   
                   tmp = load(obj.saveName);
                   out = tmp.input;
               end
        end
    end

end

示例:

a = SavingClass;
b=rand(1000,1000);
a.data = b;

看看 `whos':

Name         Size                Bytes  Class          Attributes

  a            1x1                    60  SavingClass              
  ans          1x5                    10  char                     
  b         1000x1000            8000000  double          

虽然您可以进行像 d = a.data-b 这样的计算 - a 在内存中只占用 60 个字节- 与约 8 MB 的 b 相反。


编辑4:经常改变功能的技巧。当您将逻辑放入外部命令中时,当您更改那里的函数定义时,matlab 不会抱怨。

classdef MyOftenEditedClass < handle

    properties
        a
    end

    methods
        function set.a(obj,val)
            mySetFunctionA(obj,val)
        end

        function out=get.a(obj)
            out = myGetFunctionA(obj);
        end
    end

end

I like to use objects. You don't need to call clear classes on every change. It is enough to delete all instances of the "old" object.

Two very powerful additions I inherit often are handle and dynamicprops.

  1. Handle makes the object behave as handle. Very nice to come around matlabs copy-on-change behavior.
  2. Dynamic props - to do some meta programming.

About the consistency checks - why no do them when you use set.property?


Edit 1:

a simplified class that uses the database:

classdef measurement
   class
   id
properties (SetAccess = private)

end
methods
function obj = measurement(varargin)
  obj.id = varargin{1};
end

    function cs = get.class(obj)
       if isempty(obj.id)
                    cs = '';
                    return
       end
       mc = mydb.local; % use some tricks here to keep the connection open
         tmp = mym(mc,...
                    'SELECT class FROM measurements WHERE id = {Si}'...
                    ,obj.id);
         cs = tmp{1};
     end
end

Edit 2: Example for Event - Observer

classdef ObservableClass < handle

    properties 
        ListObservers=[]
        data
    end

    methods
        function obj = ObservableClass(varargin)
            obj.data = rand(100,2);
        end

        function addObserver(obj,observer)
            obj.ListObservers = [obj.ListObservers,observer];
        end

        function fireUpdate(obj)
            for i=1:numel(obj.ListObservers)
                obj.ListObservers(i).update();
            end
        end

        function set.data(obj,newData)
            obj.data = newData;
            obj.fireUpdate;
        end
    end
end

and a listener:

 classdef ObservingPlot
    properties
        fig
        observedClass
    end

    methods
        function obj = ObservingPlot(varargin)
            obj.observedClass = varargin{1};
            obj.createPlot;
            obj.observedClass.addObserver(obj);
        end

        function createPlot(obj)
            obj.fig=figure;
            plot(obj.observedClass.data);
        end

        function update(obj)
            gcf(obj.fig)
            clf
            plot(obj.observedClass.data);
        end
    end

end

The example:

a = ObservableClass()
b = ObservingPlot(a)

you can then observe when you do a: a.data=rand(100,3) - the plot will change immediatly.


Edit 3: a simple saving class

classdef SavingClass < handle

    properties 
        saveName
        data
    end

    methods
        function set.data(obj,input)
            if isempty(obj.saveName)
                obj.saveName = [tempname '.mat'];
            end
            save(obj.saveName,'input')
        end

        function out = get.data(obj)            
                out = [];
               if exist(obj.saveName,'file')                   
                   tmp = load(obj.saveName);
                   out = tmp.input;
               end
        end
    end

end

Example:

a = SavingClass;
b=rand(1000,1000);
a.data = b;

look at `whos':

Name         Size                Bytes  Class          Attributes

  a            1x1                    60  SavingClass              
  ans          1x5                    10  char                     
  b         1000x1000            8000000  double          

although you can do calculations like d = a.data-b - a takes just 60 bytes in memory - as opposed to the ~8 MB of b.


Edit 4: trick for often changing functions. When you put the logic in external commands matlab will not complain when you change the function definition there.

classdef MyOftenEditedClass < handle

    properties
        a
    end

    methods
        function set.a(obj,val)
            mySetFunctionA(obj,val)
        end

        function out=get.a(obj)
            out = myGetFunctionA(obj);
        end
    end

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文