Methods from Class Arrays - arrays

I have several classes with a lot of methods and functions, which normally handles mx1 arrays. For a given class:
S1.x=randn(m,1);
S1+2*randn(m,1); % Plus Override
S1.smooth; % Class Methods
S1.detrend;
Now i wish to handle class arrays for the same given class, on a way like this;
S=[S1 S2 S3 S4];
S.smooth; % Methods for Class Array
S.detrend;
Question:
Is there a simple way for doing this, without rewritting all the functions implementing the class properties and methods?
I am looking for some specific addition, redefinition, piece of code, trick, etc. in order to do that in a clean way. The purpose of this is code functionality, not performance -the few performance critical functions are already vectorized-.
Greetings,

How about that:
classdef TestClass
methods
function smooth(obj)
if numel(obj) == 1
disp('Hello')
else
for i=1:numel(obj)
obj(i).smooth;
end
end
end
end
end
Called as follow:
>> t1 = TestClass;
>> t1.smooth
Hello
>> t2 = [TestClass TestClass TestClass];
>> t2.smooth
Hello
Hello
Hello
If you really wanted to you should also be able to overload the subsref operator to automatically do that on all your methods (it gives you access to the . operator) . However in my experience overloading subsref correctly is not straight forward and might be more effort than what is worth.
Here is an example of this idea. It's simplistic and you will likely will need further refinement on your part but should get you started. Note the cheer amount of hackery :)
classdef TestClass
properties
Value
end
methods
function obj = TestClass(x)
obj.Value = x;
end
function smooth(obj)
fprintf('I am %d\n', obj.Value)
end
function res = opposite(obj)
res = -obj.Value;
end
function [res1,res2,res3] = test(obj)
res1 = obj.Value;
res2 = res1*res1;
res3 = res2*res1;
end
function varargout = subsref(A,S)
if numel(A) > 1 && strcmp(S(1).type, '.')
if nargout == 0
feval(S.subs, A(1));
else
nout = nargout(['TestClass>TestClass.' S.subs]);
if nout < 0
nout = -nout;
end
if nout == 0
arrayfun(#(x)feval(S.subs, x), A);
varargout = cell(1, nargout);
else
for i=1:nargout
[output{1:nout}] = feval(S.subs, A(i));
varargout{i} = output;
output = {};
end
end
end
else
if nargout == 0
builtin('subsref', A, S);
else
varargout{:} = builtin('subsref', A, S);
end
end
end
end
Example of use:
>> t1 = TestClass(5);
>> t1.smooth;
I am 5
>> t2 = [TestClass(1) TestClass(2) TestClass(3)];
>> t2(2).smooth;
I am 2
>> t2.smooth;
I am 1
I am 2
I am 3
>> t2(1:2).smooth
I am 1
I am 2
>> t2(2:3).smooth
I am 2
I am 3
>> t2([1 3]).smooth
I am 1
I am 3
>> t2.test
ans =
[1] [1] [1]
ans =
[2] [4] [8]
ans =
[3] [9] [27]

Related

Run another DAG with TriggerDagRunOperator multiple times

i have a DAG (DAG1) where i copy a bunch of files. I would then like to kick off another DAG (DAG2) for each file that was copied. As the number of files copied will vary per DAG1 run, i would like to essentially loop over the files and call DAG2 with the appropriate parameters.
eg:
with DAG( 'DAG1',
description="copy files over",
schedule_interval="* * * * *",
max_active_runs=1
) as dag:
t_rsync = RsyncOperator( task_id='rsync_data',
source='/source/',
target='/destination/' )
t_trigger_preprocessing = TriggerDagRunOperator( task_id='trigger_preprocessing',
trigger_daq_id='DAG2',
python_callable=trigger
)
t_rsync >> t_trigger_preprocessing
i was hoping to use the python_callable trigger to pull the relevant xcom data from t_rsync and then trigger DAG2; but its not clear to me how to do this.
i would prefer to put the logic of calling DAG2 here to simplify the contents of DAG2 (and also provide stacking schematics with the max_active_runs)
ended up writing my own operator:
class TriggerMultipleDagRunOperator(TriggerDagRunOperator):
def execute(self, context):
count = 0
for dro in self.python_callable(context):
if dro:
with create_session() as session:
dbag = DagBag(settings.DAGS_FOLDER)
trigger_dag = dbag.get_dag(self.trigger_dag_id)
dr = trigger_dag.create_dagrun(
run_id=dro.run_id,
state=State.RUNNING,
conf=dro.payload,
external_trigger=True)
session.add(dr)
session.commit()
count = count + 1
else:
self.log.info("Criteria not met, moving on")
if count == 0:
raise AirflowSkipException('No external dags triggered')
with a python_callable like
def trigger_preprocessing(context):
for base_filename,_ in found.items():
exp = context['ti'].xcom_pull( task_ids='parse_config', key='experiment')
run_id='%s__%s' % (exp['microscope'], datetime.utcnow().replace(microsecond=0).isoformat())
dro = DagRunOrder(run_id=run_id)
d = {
'directory': context['ti'].xcom_pull( task_ids='parse_config', key='experiment_directory'),
'base': base_filename,
'experiment': exp['name'],
}
LOG.info('triggering dag %s with %s' % (run_id,d))
dro.payload = d
yield dro
return
and then tie it all together with:
t_trigger_preprocessing = TriggerMultipleDagRunOperator( task_id='trigger_preprocessing',
trigger_dag_id='preprocessing',
python_callable=trigger_preprocessing
)

Ruby Array Reference Function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I don't understand the Ruby reference relationship of arrays variables (best and vector) with the return of the functions (fitness or bitstring for example).
Concidere this case:
best[:fitness]
At where best is array and fitness is a function, what will be required of the variable ? and what will be referenced by the function ?
See the complete code:
def objective_function(vector)
return vector.inject(0.0) {|sum, x| sum + (x ** 2.0)}
end
def random_bitstring(num_bits)
return (0...num_bits).inject(""){|s,i| s<<((rand<0.5) ? "1" : "0")}
end
def decode(bitstring, search_space, bits_per_param)
vector = []
search_space.each_with_index do |bounds, i|
off, sum = i*bits_per_param, 0.0
param = bitstring[off...(off+bits_per_param)].reverse
param.size.times do |j|
sum += ((param[j].chr=='1') ? 1.0 : 0.0) * (2.0 ** j.to_f)
end
min, max = bounds
vector << min + ((max-min)/((2.0**bits_per_param.to_f)-1.0)) * sum
end
return vector
end
def fitness(candidate, search_space, param_bits)
candidate[:vector]=decode(candidate[:bitstring], search_space, param_bits)
candidate[:fitness] = objective_function(candidate[:vector])
end
def binary_tournament(pop)
i, j = rand(pop.size), rand(pop.size)
j = rand(pop.size) while j==i
return (pop[i][:fitness] < pop[j][:fitness]) ? pop[i] : pop[j]
end
def point_mutation(bitstring, rate=1.0/bitstring.size)
child = ""
bitstring.size.times do |i|
bit = bitstring[i].chr
child << ((rand()<rate) ? ((bit=='1') ? "0" : "1") : bit)
end
return child
end
def crossover(parent1, parent2, rate)
return ""+parent1 if rand()>=rate
child = ""
parent1.size.times do |i|
child << ((rand()<0.5) ? parent1[i].chr : parent2[i].chr)
end
return child
end
def reproduce(selected, pop_size, p_cross, p_mut)
children = []
selected.each_with_index do |p1, i|
p2 = (i.modulo(2)==0) ? selected[i+1] : selected[i-1]
p2 = selected[0] if i == selected.size-1
child = {}
child[:bitstring] = crossover(p1[:bitstring], p2[:bitstring], p_cross)
child[:bitstring] = point_mutation(child[:bitstring], p_mut)
children << child
break if children.size >= pop_size
end
return children
end
def bitclimber(child, search_space, p_mut, max_local_gens, bits_per_param)
current = child
max_local_gens.times do
candidate = {}
candidate[:bitstring] = point_mutation(current[:bitstring], p_mut)
fitness(candidate, search_space, bits_per_param)
current = candidate if candidate[:fitness] <= current[:fitness]
end
return current
end
def search(max_gens, search_space, pop_size, p_cross, p_mut, max_local_gens, p_local, bits_per_param=16)
pop = Array.new(pop_size) do |i| {:bitstring=>random_bitstring(search_space.size*bits_per_param)}
end
pop.each{|candidate| fitness(candidate, search_space, bits_per_param) }
gen, best = 0, pop.sort{|x,y| x[:fitness] <=> y[:fitness]}.first
max_gens.times do |gen|
selected = Array.new(pop_size){|i| binary_tournament(pop)}
children = reproduce(selected, pop_size, p_cross, p_mut)
children.each{|cand| fitness(cand, search_space, bits_per_param)}
pop = []
children.each do |child|
if rand() < p_local
child = bitclimber(child, search_space, p_mut, max_local_gens, bits_per_param)
end
pop << child
end
pop.sort!{|x,y| x[:fitness] <=> y[:fitness]}
best = pop.first if pop.first[:fitness] <= best[:fitness]
puts ">gen=#{gen}, f=#{best[:fitness]}, b=#{best[:bitstring]}"
end
return best
end
if __FILE__ == $0
# problem configuration
problem_size = 3
search_space = Array.new(problem_size) {|i| [-5, +5]}
# algorithm configuration
max_gens = 100
pop_size = 100
p_cross = 0.98
p_mut = 1.0/(problem_size*16).to_f
max_local_gens = 20
p_local = 0.5
# execute the algorithm
best = search(max_gens, search_space, pop_size, p_cross, p_mut, max_local_gens, p_local)
puts "done! Solution: f=#{best[:fitness]}, b=#{best[:bitstring]}, v=#{best[:vector].inspect}"
end
ps: This code is an implementation of a Memetic Algorithms , which in turn is a variation of an Evolutionary Algorithm.
A Memetic Altorithms technically search the best solution to a problem in a number of solutions from an integrated global search results to an optimization every cycle / generation that selects variations of a good solution from a local search results.
My goal is to translate the code to Matlab programming.

Class-Array Interaction Ruby

I'm trying to set up a program to help me take care of grading for students in a class. I've set it up to make a class of student then to read in from the file (something I'm not very familiar with in Ruby) via an array. My programming experience is in java so if there are errors that can be explained by that I apologize. Thank you in advance for your help.
class Student
def initialize(str_LastName, str_FirstName, arr_Score)
#str_LastName = str_LastName
#str_FirstName = str_FirstName
#arr_Score = arr_Score
str_Grade = ""
int_OutOf = 415
end
def get_LastName
str_LastName
end
def get_FirstName
str_FirstName
end
def get_Grade
str_Grade
end
def set_TotalScore()
sum = 0
arr_Score.each do |item|
sum += item
end
arr_Score[12] = sum
end
def set_Grade
if arr_Score[12]/int_OutOf >= 0.9
str_Grade = "A"
elsif arr_Score[12]/int_OutOf >= 0.8
str_Grade = "B"
elsif arr_Score[12]/int_OutOf >= 0.7
str_Grade = "C"
elsif arr_Score[12]/int_OutOf >= 0.6
str_Grade = "D"
else
str_Grade = "F"
end
end
end
def main
file_name = "Grades"
arr_students = Array.new(31)
arr_scores = Array.new(12)
int_i = 0
file_io = open(file_name).readlines.each do |line|
array = line.split(",").map(&:strip)
student = Student.new(array[0],array[1],array[2..-2]) #the final element in the array is for the final score
arr_students[int_i] = student
puts "read #{arr_students[int_i]}"
end
file_name = "Graded"
file_io = open(file_name,"a+")
arr_students.each do |student|
set_TotalScore
set_Grade
file.io_write(student)
puts "write #{student}"
end
end
main if __FILE__==$0
Here is my run at it. I tried to stay true in general to the original intent of your code while introducing more Rubyish ways of doing things.
class Student
def initialize(firstname, lastname, *scores)
#firstname, #lastname, #scores = firstname, lastname, scores
end
def total_score
#scores.map(&:to_i).inject(:+)
end
def grade
raise "TOO HIGH!" if total_score > MAX_SCORE
case total_score / MAX_SCORE
when 0.9..1.0; "A"
when 0.8...0.9; "B"
when 0.7...0.8; "C"
when 0.6...0.7; "D"
else "F"
end
end
def to_s
"#{#lastname}, #{#firstname}: #{total_score}, #{grade}"
end
end
MAX_SCORE = 415.0
DATA.each_line do |line|
arr = line.split(",").map(&:strip)
student = Student.new *arr
puts student
end
__END__
Herb,Goldberg,22,99,44,22,88,88
Mark,Sullivan,77,88,88,44,33
You can read and write to files like this(not tested):
outfile = File.open("Graded", "a+")
File.open("Grades").each_line do |line|
...
outfile.puts student
end
outfile.close
We can not easily reproduce your code because you open a file called "Grades" and we do not have or know of its content.
You should also add some code to first check whether your file exists, before continuing - right now your script exits with a Errno::ENOENT.
I would also suggest putting the logic in main into your class instead - let your class handle everything.
In the part:
if __FILE__ == $PROGRAM_NAME
end
You can then simply initialize your class with a simple call such as:
Foobar.new(ARGV)
You described the "Grades" file but I did not understand what you wrote - it would be easier if you could link in to a sample, like via a pastie or gist, then link it in; and to also say what the part is that is not working, which is also unclear.
The style issues are secondary, I consider your code ok - the other poster here does not.
You should go through codecademy to get your ruby syntax down.
To access your initialized instance variables (#str_LastName (which should be #last_name), etc) you need to use "attr_reader :str_LastName", preferably at the top of the class. That'll definite you getter (setter is attr_writer, both is attr_accessor).
You can also do a sum on an array like this: [1,4,6,7].inject(:+).
Does Java not allow case statements? You should use that in set_grade. You also don't need to initialize str_Grade. In set grade, you could do #grade_letter ||= "A", and then calling set_grade will return that value on each call.
I didn't look through your main method. It's ugly though. Ruby methods probably shouldn't be more than 5 lines long.

Evaluate cells of strings in a cell array

I have a cell array containing strings and cells, similar to this:
theCellArray = {{'aa1' {'bb'; 'cc'}}; {'aa2' {'dd'; 'ee'}}};
Now I would like to be able to concatenate the names and get something similar to this :
aa1.bb
aa1.cc
aa2.dd
aa2.ee
The number of element might change (so for aa1, there might be bb, cc, dd, ee, etc).
I tried various things, but I'm always unable to make Matlab evaluate the second step of string (the one containing bb, cc...). Any ideas?
EDIT:
There might be more than 2 levels, so theCellArray could be :
theCellArray = {{'aa1' {'bb' {'b1' {'b11' 'b12'} 'b2'}; 'cc'}}; {'aa2' {'dd'; 'ee'}}};
theCellArray is like a tree, so the number of level is unknown.
Here is a recursive solution:
function t = recCat(s)
if ~iscell(s)
t = s;
elseif size(s,1) > 1,
t = [recCat(s(1,:)); recCat(s(2:end,:))];
elseif size(s,2) > 1,
t0 = cellfun(#(x) strcat('.', x), ...
cellfun(#recCat, s(2:end), 'UniformOutput', false), ...
'UniformOutput', false);
t = strcat(s{1}, t0{:});
elseif ischar(s{1})
t = s;
else
t = recCat(s{1});
end
end
Here is the result for first example:
>> theCellArray = {{'aa1' {'bb'; 'cc'}}; {'aa2' {'dd'; 'ee'}}};
>> recCat(theCellArray)
ans =
'aa1.bb'
'aa1.cc'
'aa2.dd'
'aa2.ee'
The second, as it stands now fails because of dimension issues in concatenation. I put 'bb' {'b1' {'b11' 'b12'} 'b2'} into another cell so that it has the same number of columns as 'cc' then you get
>> theCellArray = {{'aa1' {{'bb' {'b1' {'b11' 'b12'} 'b2'}}; 'cc'}}; {'aa2' {'dd'; 'ee'}}};
>> recCat(theCellArray)
ans =
'aa1.bb.b1.b11.b12.b2'
'aa1.cc'
'aa2.dd'
'aa2.ee'
However, you probably meant b11 and b12 to be on the same column not row so in that case:
>> theCellArray = {{'aa1' {{'bb' {'b1' {'b11';'b12'} 'b2'}}; 'cc'}}; {'aa2' {'dd'; 'ee'}}};
>> recCat(theCellArray)
ans =
'aa1.bb.b1.b11.b2'
'aa1.bb.b1.b12.b2'
'aa1.cc'
'aa2.dd'
'aa2.ee'
Here's a sweet one:
out = cellfun(#(y) cellfun(#(x) [ y{1} '.' x],y{2},'UniformOutput',false),theCellArray,'UniformOutput',false)
out{:}
ans =
'aa1.bb'
'aa1.cc'
ans =
'aa2.dd'
'aa2.ee'
Super One liner! (but not very efficient) And only works with original pose of question with 2 levels of cell strings.

Fastest and most effective way of comparing two array of hashes of different format

I have two arrays of hashes with the format:
hash1
[{:root => root_value, :child1 => child1_value, :subchild1 => subchild1_value, bases => hit1,hit2,hit3}...]
hash2
[{:path => root_value/child1_value/subchild1_value, :hit1_exist => t ,hit2_exist => t,hit3_exist => f}...]
IF I do this
Def sample
results = nil
project = Project.find(params[:project_id])
testrun_query = "SELECT root_name, suite_name, case_name, ic_name, executed_platforms FROM testrun_caches WHERE start_date >= '#{params[:start_date]}' AND start_date < '#{params[:end_date]}' AND project_id = #{params[:project_id]} AND result <> 'SKIP' AND result <> 'N/A'"
if !params[:platform].nil? && params[:platform] != [""]
#yell_and_log "platform not nil"
platform_query = nil
params[:platform].each do |platform|
if platform_query.nil?
platform_query = " AND (executed_platforms LIKE '%#{platform.to_s},%'"
else
platform_query += " OR executed_platforms LIKE '%#{platform.to_s},%'"
end
end
testrun_query += ")" + platform_query
end
if !params[:location].nil? &&!params[:location].empty?
#yell_and_log "location not nil"
testrun_query += "AND location LIKE '#{params[:location].to_s}%'"
end
testrun_query += " GROUP BY root_name, suite_name, case_name, ic_name, executed_platforms ORDER BY root_name, suite_name, case_name, ic_name"
ic_query = "SELECT ics.path, memberships.pts8210, memberships.sv6, memberships.sv7, memberships.pts14k, memberships.pts22k, memberships.pts24k, memberships.spb32, memberships.spb64, memberships.sde, projects.name FROM ics INNER JOIN memberships on memberships.ic_id = ics.id INNER JOIN test_groups ON test_groups.id = memberships.test_group_id INNER JOIN projects ON test_groups.project_id = projects.id WHERE deleted = 'false' AND (memberships.pts8210 = true OR memberships.sv6 = true OR memberships.sv7 = true OR memberships.pts14k = true OR memberships.pts22k = true OR memberships.pts24k = true OR memberships.spb32 = true OR memberships.spb64 = true OR memberships.sde = true) AND projects.name = '#{project.name}' GROUP BY path, memberships.pts8210, memberships.sv6, memberships.sv7, memberships.pts14k, memberships.pts22k, memberships.pts24k, memberships.spb32, memberships.spb64, memberships.sde, projects.name ORDER BY ics.path"
if params[:ic_type] == "never_run"
runtest = TestrunCache.connection.select_all(testrun_query)
alltest = TrsIc.connection.select_all(ic_query)
(alltest.length).times do |i|
#exec_pltfrm = test['executed_platforms'].split(",")
unfinishedtest = comparison(runtest[i],alltest[i])
yell_and_log("test = #{unfinishedtest}")
yell_and_log("#{runtest[i]}")
yell_and_log("#{alltest[i]}")
end
end
end
I get in my log:
test = true
array of hash 1 = {"root_name"=>"BSDPLATFORM", "suite_name"=>"cli", "case_name"=>"functional", "ic_name"=>"cli_sanity_test", "executed_platforms"=>"pts22k,pts24k,sv7,"}
array of hash 2 = {"path"=>"BSDPLATFORM/cli/functional/cli_sanity_test", "pts8210"=>"f", "sv6"=>"f", "sv7"=>"t", "pts14k"=>nil, "pts22k"=>"t", "pts24k"=>"t", "spb32"=>nil, "spb64"=>nil, "sde"=>nil, "name"=>"pts_6_20"}
test = false
array of hash 1 = {"root_name"=>"BSDPLATFORM", "suite_name"=>"infrastructure", "case_name"=>"bypass_pts14k_copper", "ic_name"=>"ic_packet_9", "executed_platforms"=>"sv6,"}
array of hash 2 = {"path"=>"BSDPLATFORM/infrastructure/build/copyrights", "pts8210"=>"f", "sv6"=>"t", "sv7"=>"t", "pts14k"=>"f", "pts22k"=>"t", "pts24k"=>"t", "spb32"=>"f", "spb64"=>nil, "sde"=>nil, "name"=>"pts_6_20"}
test = false
array of hash 1 = {"root_name"=>"BSDPLATFORM", "suite_name"=>"infrastructure", "case_name"=>"bypass_pts14k_copper", "ic_name"=>"ic_status_1", "executed_platforms"=>"sv6,"}
array of hash 2 = {"path"=>"BSDPLATFORM/infrastructure/build/ic_1", "pts8210"=>"f", "sv6"=>"t", "sv7"=>"t", "pts14k"=>"f", "pts22k"=>"t", "pts24k"=>"t", "spb32"=>"f", "spb64"=>nil, "sde"=>nil, "name"=>"pts_6_20"}
test = false
array of hash 1 = {"root_name"=>"BSDPLATFORM", "suite_name"=>"infrastructure", "case_name"=>"bypass_pts14k_copper", "ic_name"=>"ic_status_2", "executed_platforms"=>"sv6,"}
array of hash 2 = {"path"=>"BSDPLATFORM/infrastructure/build/ic_files", "pts8210"=>"f", "sv6"=>"t", "sv7"=>"f", "pts14k"=>"f", "pts22k"=>"t", "pts24k"=>"t", "spb32"=>"f", "spb64"=>nil, "sde"=>nil, "name"=>"pts_6_20"}
SO I get only the first to match but rest becomes different and I get result of one instead of 4230
I would like some way to match by path and root/suite/case/ic and then compare the executed platforms passed in array of hashes 1 vs platforms set to true in array of hash2
Not sure if this is fastest, and I wrote this based on your original question that didn't provide sample code, but:
def compare(h1, h2)
(h2[:path] == "#{h1[:root]}/#{h1[:child1]}/#{h1[:subchild1]}") && \
(h2[:hit1_exist] == ((h1[:bases][0] == nil) ? 'f' : 't')) && \
(h2[:hit2_exist] == ((h1[:bases][1] == nil) ? 'f' : 't')) && \
(h2[:hit3_exist] == ((h1[:bases][2] == nil) ? 'f' : 't'))
end
def compare_arr(h1a, h2a)
(h1a.length).times do |i|
compare(h1a[i],h2a[i])
end
end
Test:
require "benchmark"
h1a = []
h2a = []
def rstr
# from http://stackoverflow.com/a/88341/178651
(0...2).map{65.+(rand(26)).chr}.join
end
def rnil
rand(2) > 0 ? '' : nil
end
10000.times do
h1a << {:root => rstr(), :child1 => rstr(), :subchild1 => rstr(), :bases => [rnil,rnil,rnil]}
h2a << {:path => '#{rstr()}/#{rstr()}/#{rstr()}', :hit1_exist => 't', :hit2_exist => 't', :hit3_exist => 'f'}
end
Benchmark.measure do
compare_arr(h1a,h2a)
end
Results:
=> 0.020000 0.000000 0.020000 ( 0.024039)
Now that I'm looking at your code, I think it could be optimized by removing array creations, and splits and joins which are creating arrays and strings that need to be garbage collected which also will slow things down, but not by as much as you mention.
Your database queries may be slow. Run explain/analyze or similar on them to see why each is slow, optimize/reduce your queries, add indexes where needed, etc. Also, check cpu and memory utilization, etc. It might not just be the code.
But, there are some definite things that need to be fixed. You also have several risks of SQL injection attack, e.g.:
... start_date >= '#{params[:start_date]}' AND start_date < '#{params[:end_date]}' AND project_id = #{params[:project_id]} ...
Anywhere that params and variables are put directly into the SQL may be a danger. You'll want to make sure to use prepared statements or at least SQL escape the values. Read this all the way through: http://guides.rubyonrails.org/active_record_querying.html
([element_being_tested].each do |el|
[hash_array_1, hash_array_2].reject do |x, y|
x[el] == y[el]
end
end).each {|x, y| puts (x[bases] | y[bases])}
Enumerate the hash elements to test.
[element_being_tested].each do |el|
Then iterate through the hash arrays themselves, comparing the given hashes by the elements of the given comparison defined by the outer loop, rejecting those not appropriately equal. (The == may actually need to be != but you can figure that much out)
[hash_array_1, hash_array_2].reject do |x, y|
x[el] == y[el]
end
Finally, you again compare the hashes taking the set union of their elements.
.each {|x, y| puts (x[bases] | y[bases])}
You may need to test the code. It's not meant for production so much as demonstration because I wasn't sure I read your code right. Please post a larger sample of the source including the data structures in question if this answer is unsatisfactory.
Regarding speed: if you're iterating through a large data set and comparing multiple there's probably nothing you can do. Perhaps you can invert the loops I presented and make the hash arrays the outer loop. You're not going to get lightning speed here in Ruby (really any language) if the data structure is large.

Resources