Re: [julia-users] Best method for inputting logic as a variable in a function
Martin Fowler published a (rather long) blog post on the topic of refactoring code into data, which I think is a nice addition in this thread even though you already have a working solution: http://martinfowler.com/articles/refactoring-adaptive-model.html His solution to this problem would have been to build a data model for the logic, and then a custom logic engine which can understand and make decisions based on the provided data. Perhaps overkill for this use case, but it's an interesting approach, IMO. // T On Tuesday, December 1, 2015 at 1:22:51 PM UTC+1, NotSoRecentConvert wrote: > > It worked quite well. Here is the latest version. > > function qcl_valve_plot(Dr::AbstractString,mindate::DateTime,maxdate:: > DateTime,dt::Dates.Period,col::Symbol,valveLogic::Function) > > ... > > # Valve Zones > f4 = [] > f4 = find(valveLogic(statuses) .== true) > > One of the valveLogic examples, "(V3 & !(V1 & V2)) == true", would now be > x -> x.Valve3 & !(x.Valve1 & x.Valve2). > > Thanks for the tip. >
Re: [julia-users] Best method for inputting logic as a variable in a function
It worked quite well. Here is the latest version. function qcl_valve_plot(Dr::AbstractString,mindate::DateTime,maxdate:: DateTime,dt::Dates.Period,col::Symbol,valveLogic::Function) ... # Valve Zones f4 = [] f4 = find(valveLogic(statuses) .== true) One of the valveLogic examples, "(V3 & !(V1 & V2)) == true", would now be x -> x.Valve3 & !(x.Valve1 & x.Valve2). Thanks for the tip.
Re: [julia-users] Best method for inputting logic as a variable in a function
You can use functions as variables, and that's far better than eval'ing a string. For example: julia> filter(x->x>0, [1,2,-3,5]) 3-element Array{Int64,1}: 1 2 5 See the manual section on "anonymous functions." --Tim On Monday, November 30, 2015 06:35:59 AM NotSoRecentConvert wrote: > I need to plot data (D::DataFrame) during certain valve configurations. > Each row of data has a corresponding status value for each valve > (statuses.Valve#). First I have to find the periods of time where the > valves were in that configuration. Then I can plot the chosen column of > data (col::Symbol). > > As long as the method isn't too complicated I'm happy to change what I > have. The method I talked about above works so I'm just looking something > different/better to improve my Julia programming skills. Short of > preprogramming all possible cases (time consuming and frustrating for all) > I couldn't think of any other method that would be so broadly applicable.
Re: [julia-users] Best method for inputting logic as a variable in a function
Le lundi 30 novembre 2015 à 07:25 -0800, NotSoRecentConvert a écrit : > D = [1:10;] > statusesValve1 = > [false;false;true;true;true;false;true;true;true;false] > valveLogic = "V1 == true" > > # Valve Zones > f4 = [] > valveLogic = replace(valveLogic," =="," .==") > valveLogic = replace(valveLogic,"V","statusesValve") > valveLogic = "find(" * valveLogic * ")" > f4 = eval(parse(valveLogic)) > f4diff = diff(f4) > f4loc_start = [f4[1];f4[find(f4diff .!= 1) + 1]] # Start > f4loc_end = [f4[find(f4diff .!= 1)];f4[end]] # End > > # Plot > ... > > Let's keep it simple and plot D where statusesValve1 is true. In this > case there are two periods where valve 1 is on, [3;4;5] and [7;8;9]. > The code above finds where each period starts (f4loc_start) and ends > (f4loc_end). With f4loc_start and f4loc_end I have the indices of > where the periods each start and end so I can plot the periods > individually. > > Hopefully that explains it better. It does. But what Tim suggested is a good solution. Regards
Re: [julia-users] Best method for inputting logic as a variable in a function
D = [1:10;] statusesValve1 = [false;false;true;true;true;false;true;true;true;false] valveLogic = "V1 == true" # Valve Zones f4 = [] valveLogic = replace(valveLogic," =="," .==") valveLogic = replace(valveLogic,"V","statusesValve") valveLogic = "find(" * valveLogic * ")" f4 = eval(parse(valveLogic)) f4diff = diff(f4) f4loc_start = [f4[1];f4[find(f4diff .!= 1) + 1]] # Start f4loc_end = [f4[find(f4diff .!= 1)];f4[end]] # End # Plot ... Let's keep it simple and plot D where statusesValve1 is true. In this case there are two periods where valve 1 is on, [3;4;5] and [7;8;9]. The code above finds where each period starts (f4loc_start) and ends (f4loc_end). With f4loc_start and f4loc_end I have the indices of where the periods each start and end so I can plot the periods individually. Hopefully that explains it better.
Re: [julia-users] Best method for inputting logic as a variable in a function
@Tim Holy, I thought about that but wasn't sure how I'd deal with variable inputs. It should be able to use any of four valves (statuses.Valve1, statuses.Valve2, etc.) in any combination and number. I'll take another look.
[julia-users] Best method for inputting logic as a variable in a function
I've written a function which loads a bunch of data and plots individual periods where valves are in a certain configuration. My function works but I am curious about best methods. function qcl_valve_plot(Dr::AbstractString,mindate::DateTime,maxdate:: DateTime,dt::Dates.Period,col::Symbol,valveLogic::AbstractString) ... # Valve Zones f4 = [] valveLogic = replace(valveLogic," =="," .==") valveLogic = replace(valveLogic,"V","statuses.Valve") valveLogic = "find(" * valveLogic * ")" f4 = eval(parse(valveLogic)) statuses is a custom type with four Valve boolean arrays among other parameters (Valve1, Valve2, Valve3, Valve4) valveLogic is a string, such as "(V3 & !(V1 & V2)) == true". When it's properly formatted by the code it looks more like "find((statuses.Valve3 & !(statuses.Valve1 & statuses.Valve2)) .== true)". Is there a better method which still keeps the input pretty simple?
Re: [julia-users] Best method for inputting logic as a variable in a function
Le lundi 30 novembre 2015 à 04:20 -0800, NotSoRecentConvert a écrit : > I've written a function which loads a bunch of data and plots > individual periods where valves are in a certain configuration. My > function works but I am curious about best methods. > > function > qcl_valve_plot(Dr::AbstractString,mindate::DateTime,maxdate::DateTime > ,dt::Dates.Period,col::Symbol,valveLogic::AbstractString) > > ... > > # Valve Zones > f4 = [] > valveLogic = replace(valveLogic," =="," .==") > valveLogic = replace(valveLogic,"V","statuses.Valve") > valveLogic = "find(" * valveLogic * ")" > f4 = eval(parse(valveLogic)) > > statuses is a custom type with four Valve boolean arrays among other > parameters (Valve1, Valve2, Valve3, Valve4) > > valveLogic is a string, such as "(V3 & !(V1 & V2)) == true". When > it's properly formatted by the code it looks more like > "find((statuses.Valve3 & !(statuses.Valve1 & statuses.Valve2)) .== > true)". > > Is there a better method which still keeps the input pretty simple? Could you give more details about the goal of this code? Do you really need to create a string an eval() it? Regards
Re: [julia-users] Best method for inputting logic as a variable in a function
I need to plot data (D::DataFrame) during certain valve configurations. Each row of data has a corresponding status value for each valve (statuses.Valve#). First I have to find the periods of time where the valves were in that configuration. Then I can plot the chosen column of data (col::Symbol). As long as the method isn't too complicated I'm happy to change what I have. The method I talked about above works so I'm just looking something different/better to improve my Julia programming skills. Short of preprogramming all possible cases (time consuming and frustrating for all) I couldn't think of any other method that would be so broadly applicable.
Re: [julia-users] Best method for inputting logic as a variable in a function
Le lundi 30 novembre 2015 à 06:35 -0800, NotSoRecentConvert a écrit : > I need to plot data (D::DataFrame) during certain valve > configurations. Each row of data has a corresponding status value for > each valve (statuses.Valve#). First I have to find the periods of > time where the valves were in that configuration. Then I can plot the > chosen column of data (col::Symbol). > > As long as the method isn't too complicated I'm happy to change what > I have. The method I talked about above works so I'm just looking > something different/better to improve my Julia programming skills. > Short of preprogramming all possible cases (time consuming and > frustrating for all) I couldn't think of any other method that would > be so broadly applicable. Sorry, but I still don't get it. Could you show a small example maybe? Regards