GitInsight
    
    
  
  
  
view release on metacpan or search on metacpan
        #or using the module
        my $Insight= GitInsight->new(no_day_stats => 0, username => "markov", accuracy=> 1);
        my $Result= $Insight->process;
        my $accuracy = $Insight->{accuracy};
        $Result = $Insight->{result};
        # $Result contains the next week predictions and is an arrayref of arrayrefs    [  [ 'Sat', 1, '2014-07-1', [ 0 ,  '0.151515151515152', '0.0606060606060606', '0.0404040404040404',  0  ]  ],   ..   [            'DayofWeek',      'winner_label'...
DESCRIPTION
    GitInsight is module that allow you to predict your github contributions
    in the "calendar contribution" style of github (the table of
    contribution that you see on your profile page).
HOW DOES IT WORK?
    GitInsight generates a transation probrability matrix from your github
    contrib_calendar to compute the possibles states for the following days.
    Given that GitHub split the states thru 5 states (or here also called
    label), the probability can be inferenced by using Bayesian methods to
    update the beliefs of the possible state transition, while markov chain
    is used to predict the states. The output of the submitted data is then
    plotted using Cellular Automata.
  THEORY
    We trace the transitions states in a matrix and increasing the count as
    far as we observe a transition
    (<https://en.wikipedia.org/wiki/Transition_matrix>), then we inference
  file_output
    here you can choose the file output name for ca.
  accuracy
    Enable/disable accuracy calculation (1/0)
  verbose
    Enable/disable verbosity (1/0)
METHODS
  contrib_calendar($username)
    Fetches the github contrib_calendar of the specified user
  process
    Calculate the predictions and generate the CA
  start_day
    Returns the first day of the contrib_calendar
  last_day
    Returns the last day of the contrib calendar (prediction included)
  prediction_start_day
    Returns the first day of the prediction (7 days of predictions)
AUTHOR
    mudler <mudler@dark-lab.net>
COPYRIGHT
    Copyright 2014- mudler
lib/GitInsight.pm view on Meta::CPAN
use Storable qw(dclone);
use POSIX;
use Time::Local;
use GitInsight::Util
    qw(markov markov_list LABEL_DIM gen_m_mat gen_trans_mat info error warning wday label prob label_step);
use List::Util qw(max);
use LWP::UserAgent;
use POSIX qw(strftime ceil);
has [qw(username contribs calendar)];
has 'verbose'      => sub {0};
has 'no_day_stats' => sub {0};
has 'statistics'   => sub {0};
has 'ca_output'    => sub {1};
has 'accuracy'     => sub {0};
has [qw(left_cutoff cutoff_offset file_output)];
sub contrib_calendar {
    my $self = shift;
    my $username = shift || $self->username;
    $self->username($username) if !$self->username;
    my $ua = LWP::UserAgent->new;
    $ua->timeout(10);
    $ua->env_proxy;
    my $response
        = $ua->get(
        'https://github.com/users/' . $username . '/contributions' );
    info "Getting "
lib/GitInsight.pm view on Meta::CPAN
}
# useful when interrogating the object
sub start_day            { shift->{first_day}->{data} }
sub last_day             { @{ shift->{result} }[-1]->[2] }
sub prediction_start_day { @{ shift->{result} }[0]->[2] }
sub _accuracy {
    my $self = shift;
    my ( @chunks, @commits );
    push @chunks, [ splice @{ $self->calendar }, 0, 7 ]
        while @{ $self->calendar };
    #@chunks contain a list of arrays of 7 days each
    my $total_days = 0;
    my $accuracy   = 0;
    for (@chunks) {
        # next if @{$_} < 4;
        push( @commits, @{$_} );
        my $Insight = GitInsight->new(
lib/GitInsight.pm view on Meta::CPAN
            $total_days++;
        }
    }
    my $accuracy_prob = prob( $total_days, $accuracy );
    $self->{accuracy} = $accuracy_prob;
    info "Accuracy is $accuracy / $total_days" if $self->verbose;
    info sprintf( "%.5f", $accuracy_prob * 100 ) . " \%" if $self->verbose;
    return $self;
}
sub _decode_calendar {
    shift;
    my $content = shift;
    my @out;
    push( @out, [ $2, $1 ] )
        while ( $content =~ m/data\-count="(.*?)" data\-date="(.*?)"/g );
    return \@out;
}
# first argument is the data:
# it should be a string in the form [ [2013-01-20, 9], ....    ] a stringified form of arrayref. each element must be an array ref containing in the first position the date, and in the second the commits .
sub decode {
    my $self = shift;
    #my $response = ref $_[0] ne "ARRAY" ? eval(shift) : shift;
    my $response
        = ref $_[0] ne "ARRAY" ? $self->_decode_calendar(shift) : shift;
    $self->calendar( dclone($response) );
    my %commits_count;
    my $min = $self->left_cutoff || 0;
    $self->{result} = [];    #empty the result
    $min = 0 if ( $min < 0 );    # avoid negative numbers
    my $max
        = $self->cutoff_offset || ( scalar( @{$response} ) - 1 );
    $max = scalar( @{$response} )
        if $max > scalar( @{$response} )
        ;    # maximum cutoff boundary it's array element number
    info "$min -> $max portion" if $self->verbose;
    my $max_commit
        = max( map { $_->[1] } @{$response} );    #Calculating label steps
    label_step( 0 .. $max_commit );   #calculating quartiles over commit count
    info( "Max commit is: " . $max_commit ) if $self->verbose;
    $self->{first_day}->{day} = wday( $response->[0]->[0] )
        ; #getting the first day of the commit calendar, it's where the ca will start
    my ($index)
        = grep { $GitInsight::Util::wday[$_] eq $self->{first_day}->{day} }
        0 .. $#GitInsight::Util::wday;
    $self->{first_day}->{index} = $index;
    $self->{first_day}->{data}  = $response->[$min]->[0];
    push( @{ $self->{ca} }, [ 255, 255, 255 ] )
        for (
        0 .. scalar(@GitInsight::Util::wday)    #white fill for labels
        + ( $index - 1 )
        );                                      #white fill for no contribs
lib/GitInsight.pm view on Meta::CPAN
        }
    );
    return $self->contribs;
}
sub process {
    my $self = shift;
    croak "process() called while you have not specified an username"
        if !$self->username;
    $self->contrib_calendar( $self->username )
        if !$self->contribs and $self->username;
    $self->_transition_matrix;
    $self->_markov;
    $self->_gen_stats if ( $self->statistics );
    $self->{png} = $self->draw_ca( @{ $self->{ca} } )
        if ( $self->ca_output == 1 );
    $self->{steps} = \%GitInsight::Util::LABEL_STEPS;
    $self->_accuracy if $self->accuracy and $self->accuracy == 1;
    return $self;
}
lib/GitInsight.pm view on Meta::CPAN
    my $Insight= GitInsight->new(no_day_stats => 0, username => "markov", accuracy=> 1);
    my $Result= $Insight->process;
    my $accuracy = $Insight->{accuracy};
    $Result = $Insight->{result};
    # $Result contains the next week predictions and is an arrayref of arrayrefs    [  [ 'Sat', 1, '2014-07-1', [ 0 ,  '0.151515151515152', '0.0606060606060606', '0.0404040404040404',  0  ]  ],   ..   [            'DayofWeek',      'winner_label',  '...
=head1 DESCRIPTION
GitInsight is module that allow you to predict your github contributions in the "calendar contribution" style of github (the table of contribution that you see on your profile page).
=head1 HOW DOES IT WORK?
GitInsight generates a transation probrability matrix from your github contrib_calendar to compute the possibles states for the following days. Given that GitHub split the states thru 5 states (or here also called label), the probability can be infer...
=head2 THEORY
We trace the transitions states in a matrix and increasing the count as far as we observe a transition (L<https://en.wikipedia.org/wiki/Transition_matrix>), then we inference the probabilities using Bayesan method L<https://en.wikipedia.org/wiki/Baye...
=head1 INSTALLATION
GitInsight requires the installation of gsl (GNU scientific library), gd(http://libgd.org/), PDL and PDL::Stats  (to be installed after the gsl library set).
on Debian:
lib/GitInsight.pm view on Meta::CPAN
=head2 accuracy
Enable/disable accuracy calculation (1/0)
=head2 verbose
Enable/disable verbosity (1/0)
=head1 METHODS
=head2 contrib_calendar($username)
Fetches the github contrib_calendar of the specified user
=head2 process
Calculate the predictions and generate the CA
=head2 start_day
Returns the first day of the contrib_calendar
=head2 last_day
Returns the last day of the contrib calendar (prediction included)
=head2 prediction_start_day
Returns the first day of the prediction (7 days of predictions)
=head1 AUTHOR
mudler E<lt>mudler@dark-lab.netE<gt>
=head1 COPYRIGHT
lib/GitInsight/Util.pm view on Meta::CPAN
requires 3 arguments: the matrix that selects the state (generated by C<gen_m_mat>), the transiction matrix, and the power that must be applied at the transition matrix.
It returns the maximum probability between the future states.
=head2 label
requires 1 argument: the number of commit to be assigned the appropriate label
It returns the assigned label
=head2 label_step
requires an array composed by [0,n] where n is the maximum commit count ever had in a day of the contribution calendar.
it sets the internal data structure to be able to call C<label()>
=head2 info/error/warning
Just used to print the output to the terminal:
    info "All ok"
    error "Something bad happened"
    warning "uh oh!"
scripts/gitinsight view on Meta::CPAN
die("At least you must enter a github username") if !$username;
$output ||= $username;
my $Insight = GitInsight->new(
    no_day_stats  => $no_day_stats,
    left_cutoff   => $left_cutoff,
    cutoff_offset => $cutoff_offset,
    file_output   => $output,
    accuracy      => $accuracy,
    verbose       => 1,
);
$Insight->contrib_calendar($username);
$Insight->process;
( run in 0.447 second using v1.01-cache-2.11-cpan-5dc5da66d9d )