Module: VisualizeHelper

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

Constant Summary collapse

VERSION =
"0.0.10.28"

Class Method Summary collapse

Class Method Details

Function to generate the boxes and links of each trajectory



225
226
227
228
229
230
231
232
233
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
# File 'ext/visualize_helper/visualize_helper.c', line 225

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)
{

  VALUE seq_key_result;
  VALUE 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),seq_key_result);
    if (boxes_period_value  == Qnil) {
      rb_hash_aset(rb_ary_entry(boxes,period),seq_key_result, value);
    }else {
      rb_hash_aset(rb_ary_entry(boxes,period),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(StringValuePtr(seq_key_result)) + strlen(prox_s) + strlen(StringValuePtr(prox_key_result)) + 3);
      sprintf(link_key,"%s_%s;%s_%s", period_s,StringValuePtr(seq_key_result),prox_s,StringValuePtr(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)));
      }    



    }//end prox < aggr_size

  }// end for 

  VALUE ab = rb_ary_new();
  rb_ary_push(ab,1);
  rb_ary_push(ab,2);
  //return result_final;
  return Qnil;
}

.joinObject

static VALUE join(VALUE self, VALUE strings){



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'ext/visualize_helper/visualize_helper.c', line 46

static VALUE join(VALUE strings){

  // initial variables
  char* joined;
  int strings_size = RARRAY_LEN(strings);
  int string_size;
  VALUE string_temp;
  VALUE result;
  
  string_temp = rb_ary_entry(strings,0);
  string_size = strlen(StringValuePtr(string_temp));
  joined = (char*) malloc(strings_size + 1);
  sprintf(joined,"%s", StringValuePtr(string_temp));

  for (int i = 1 ; i < strings_size; i++) {
    
    string_temp = rb_ary_entry(strings,i);
    string_size = strlen(StringValuePtr(string_temp));
      
    joined = (char*)  realloc(joined, string_size + strlen(joined) + 1);
    sprintf(joined,"%s,%s", joined, StringValuePtr(string_temp));

  }
  result = rb_str_new2(joined);  
  free(joined);
  return  result;
}

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

Return: [ min, max, period]



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'ext/visualize_helper/visualize_helper.c', line 182

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'ext/visualize_helper/visualize_helper.c', line 79

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);
}