Module: VisualizeHelper

Defined in:
lib/visualize_helper/version.rb,
ext/visualize_helper/visualize_helper.c

Constant Summary collapse

VERSION =
"0.0.10.29"

Class Method Summary collapse

Class Method Details

Function to generate the boxes and links of each trajectory



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'ext/visualize_helper/visualize_helper.c', line 234

static VALUE generate_boxes_and_links(VALUE self, VALUE min, VALUE max, VALUE aggr, VALUE boxes, VALUE links, VALUE dict, VALUE type_agroupment, VALUE value)
{

  char* seq_key_result;
  char* prox_key_result;
  // Initial Variables
  int length_seq_sorted;
  int length_prox_sorted;
  VALUE result_final;
  VALUE boxes_period_value;
  VALUE links_period_value;
  VALUE seq_key;
  VALUE prox_key;
  VALUE seq;
  VALUE aggr_prox;
  int seq_size;
  int aggr_prox_size;
  int prox;
  int aggr_size = RARRAY_LEN(aggr);
  char* link_key;
  char* period_s;
  char* prox_s;

  for(int period = 0; period < aggr_size; period++ ){
     seq_key = rb_ary_new();

     if (period < aggr_size - 1) {
      prox_key = rb_ary_new();
     } 
     seq = rb_ary_entry(aggr,period);
     seq_size = (int) RARRAY_LEN(seq);

      // Translate sequences with dict
      if (seq_size == 0) {
        rb_ary_push(seq_key,rb_hash_aref(dict, rb_str_new2("M-2"))); 
      }else{

        for(int i = 0; i < seq_size; i++ ) {
          rb_ary_push(seq_key,rb_hash_aref(dict,rb_ary_entry(seq,i)));  
        }  
      }

    // agroup by unique or not
    if ( strcmp(StringValuePtr(type_agroupment),"s") == 0 ) {
      //sort with uniq 
      seq_key = sort_uniq(self,seq_key,1);
    }else{
      //sort without uniq
      seq_key = sort_uniq(self,seq_key,0);
    }

    // if there is "no-event" and other one selected, remove the "no-event"
    length_seq_sorted  =  (strcmp(StringValuePtr(type_agroupment),"s") == 0 ) ? RARRAY_LEN(seq_key)  : RARRAY_LEN(uniq(seq_key)) ;
    if (length_seq_sorted != 1) {
      seq_key = remove_entry_from_array(seq_key,"M-3");
    }

    // Generate the key
    seq_key_result = join(seq_key);

    //
    boxes_period_value = rb_hash_aref(rb_ary_entry(boxes,period),rb_str_new2(seq_key_result));
    if (boxes_period_value  == Qnil) {
      rb_hash_aset(rb_ary_entry(boxes,period),rb_str_new2(seq_key_result), value);
    }else {
      rb_hash_aset(rb_ary_entry(boxes,period),rb_str_new2(seq_key_result),INT2FIX(FIX2INT(boxes_period_value) + FIX2INT(value)));
    }    

    prox = period +1;

    if (prox < aggr_size ) {
      aggr_prox = rb_ary_entry(aggr,prox); 
      aggr_prox_size = (int) RARRAY_LEN(aggr_prox);
      if ( aggr_prox_size == 0) {
        rb_ary_push(prox_key,rb_hash_aref(dict, rb_str_new2("M-2"))); 
      }else{
        for(int i = 0; i < aggr_prox_size ; i++ ) {
          rb_ary_push(prox_key,rb_hash_aref(dict,rb_ary_entry(aggr_prox,i)));  
        }  
      }     

       // agroup by unique or not
      if ( strcmp(StringValuePtr(type_agroupment),"n") == 0 ) {
        //sort with uniq
        prox_key = sort_uniq(self,prox_key,1);
      }else{
        //sort without uniq
        prox_key = prox_key;
      }

  //    
      // if there is "no-event" and other one selected, remove the "no-event"
      length_prox_sorted  =  (strcmp(StringValuePtr(type_agroupment),"n") == 0 ) ? RARRAY_LEN(prox_key)  : RARRAY_LEN(sort_uniq(self,prox_key,1)) ;
      if (length_prox_sorted != 1) {
        prox_key = remove_entry_from_array(prox_key,"M-3");
      }

      // Generate the key
      prox_key_result = join(prox_key);

      // generate a key link
      period_s = ( period == 0 ) ? (char*) malloc(1)  : (char*) malloc(floor(log10(abs(period))) + 1);
      prox_s = ( prox == 0 ) ? (char*) malloc(1)  : (char*) malloc(floor(log10(abs(prox))) + 1);
      sprintf(period_s,"%d",period);
      sprintf(prox_s,"%d",prox);
      link_key =  (char*) malloc( strlen(period_s) + strlen(seq_key_result) + strlen(prox_s) + strlen(prox_key_result) + 3);
      sprintf(link_key,"%s_%s;%s_%s", period_s,seq_key_result,prox_s,prox_key_result);

      links_period_value = rb_hash_aref(rb_ary_entry(links,period),rb_str_new2(link_key));
      if (links_period_value  == Qnil) {
        rb_hash_aset(rb_ary_entry(links,period),rb_str_new2(link_key),value);
      }else {
        rb_hash_aset(rb_ary_entry(links,period),rb_str_new2(link_key), INT2FIX(FIX2INT(links_period_value) + FIX2INT(value)));
      }    

      free(period_s); 
      free(prox_s); 
      free(link_key); 

    }//end prox < aggr_size

  }// end for 

  //return result_final;
  return Qnil;
}

.min_max_period(min, max, hash, intervals, aggr) ⇒ Object

Return: [ min, max, period]



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/visualize_helper/visualize_helper.c', line 191

static VALUE min_max_period(VALUE self, VALUE min, VALUE max, VALUE hash, VALUE intervals, VALUE aggr)
{
    int  period = Qnil;

    // Create Ruby array that will be the result
    VALUE array  = rb_ary_new();

    // Push initial values of the result
    rb_ary_push(array,min);  
    rb_ary_push(array,max);  
    rb_ary_push(array,period);  
    rb_ary_push(array,intervals);  
    rb_ary_push(array,aggr);  
    
    
    // iterate on hash calling "encontra_min_max_period" for each
    rb_hash_foreach(hash,encontra_min_max_period,array);
      
    // Return results
    return array;
}

.sort_uniq(strings, ) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/visualize_helper/visualize_helper.c', line 88

static VALUE sort_uniq(VALUE self, VALUE strings, int unique)
{
    int strings_size = RARRAY_LEN(strings);
    const char *input[strings_size];

    for (int i = 0; i< strings_size; i++){
      VALUE string  = rb_ary_entry(strings,i);
      input[i] = StringValuePtr(string);
    }

    int stringLen = sizeof(input) / sizeof(char *);
    qsort(input, stringLen, sizeof(char *), myCompare);


    // Transform the result input into a ruby array
    VALUE resultado = rb_ary_new();
    for (int i=0; i<stringLen; ++i) {
      rb_ary_push(resultado,rb_str_new2(input[i]));
    } 

    return (unique == 0 ) ? resultado : uniq(resultado);
}