Module: VisualizeHelper

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

Constant Summary collapse

VERSION =
"0.0.10.31"

Class Method Summary collapse

Class Method Details

Function to generate the boxes and links of each trajectory



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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'ext/visualize_helper/visualize_helper.c', line 256

static VALUE generate_boxes_and_links(VALUE self, 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*) malloc(1000);
  char* period_s;
  char* prox_s;
  //FILE *f = fopen("/tmp/file.txt", "a");

  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(seq_key,1);
    }else{
      //sort without uniq
      seq_key = sort_uniq(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(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(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);
      RemoveSpaces(link_key);
      
      //fprintf(f,"Composicao key:\n");
      //fprintf(f,"period_s: %s\n",period_s);
      //fprintf(f,"seq_key: %s\n",seq_key_result);
      //fprintf(f,"prox_s : %s\n",prox_s);
      //fprintf(f,"prox_key_result: %s\n",prox_key_result);
      //fprintf(f,"chave final : %s\n",link_key);

      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 

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

  //fclose(f);
  //return result_final;
  return Qnil;
}

.join_teste(strings) ⇒ Object

Register the method sort



396
397
398
399
# File 'ext/visualize_helper/visualize_helper.c', line 396

static VALUE join_teste(VALUE self, VALUE strings){

    return rb_str_new2(join(strings));
}

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

Return: [ min, max, period]



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/visualize_helper/visualize_helper.c', line 213

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() ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'ext/visualize_helper/visualize_helper.c', line 104

static VALUE sort_uniq(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);
}