-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
176 changed files
with
10,076 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
% | ||
% Timer class. This is used to output the remaining time on the log | ||
% of a long computation. See konect_clusco.m for example usage. | ||
% | ||
% Note: it is important to assign the result of konect_timer_tick() | ||
% to the timer object. | ||
% | ||
% ABOUT | ||
% This file is part of the KONECT Matlab Toolbox version 0.3. | ||
% konect.uni-koblenz.de | ||
% (c) Jerome Kunegis 2014; this is Free Software released under | ||
% the GPLv3, see COPYING. | ||
% | ||
|
||
classdef konect_timer | ||
|
||
properties | ||
time_begin | ||
time_last | ||
time_threshold | ||
n | ||
count_my; | ||
end | ||
|
||
methods | ||
|
||
% Create a timer with N iterations. | ||
function this = konect_timer(n) | ||
persistent count; | ||
|
||
if (n < 0) | ||
count = count - 1; | ||
return; | ||
end | ||
|
||
this.time_begin = clock; | ||
this.time_last = this.time_begin; | ||
this.n = n; | ||
if ~size(count), count = 0; end; | ||
this.count_my = count; | ||
count = count + 1; | ||
this.time_threshold = 10; % seconds | ||
end | ||
|
||
function this = konect_timer_tick(this, i) | ||
|
||
assert(i >= 0); | ||
|
||
time_now = clock; | ||
|
||
time_diff = etime(time_now, this.time_last); | ||
if (time_diff < this.time_threshold) | ||
return; | ||
end | ||
|
||
this.time_last = time_now; | ||
|
||
time_diff = etime(time_now, this.time_begin); | ||
left = time_diff * (this.n - i + 1) / (i - 1); | ||
text = konect_timer_text(this, left); | ||
if this.count_my > 0 | ||
fprintf(1, '%d of %d {%s left}\n', i - 1, this.n, text); | ||
else | ||
fprintf(1, '%d of %d [%s left]\n', i - 1, this.n, text); | ||
end | ||
end | ||
|
||
function text = konect_timer_text(this, t) | ||
if t < 60 | ||
text = sprintf('%ds', floor(t)); | ||
elseif t < 3600 | ||
text = sprintf('%d:%02d', floor(t/60), mod(floor(t), 60)); | ||
elseif t < 3600 * 24 | ||
text = sprintf('%d:%02d:%02d', floor(t/3600), mod(floor(t/60), 60), mod(floor(t), 60)); | ||
else | ||
text = sprintf('%dd %02d:%02d:%02d', floor(t/3600/24), mod(floor(t/3600), 24), mod(floor(t/60), 60), mod(floor(t), 60)); | ||
end | ||
end | ||
|
||
function this = konect_timer_end(this) | ||
e = etime(this.time_last, this.time_begin); | ||
if e ~= 0 | ||
text = konect_timer_text(this, 0); | ||
if this.count_my > 0 | ||
fprintf(1, '%d of %d {%s left}\n', this.n, this.n, text); | ||
else | ||
fprintf(1, '%d of %d [%s left]\n', this.n, this.n, text); | ||
end | ||
end | ||
konect_timer(-1); | ||
end | ||
|
||
% Set new value of N, i.e. of the total number of | ||
% iterations | ||
function this = konect_timer_set(this, n_new) | ||
this.n = n_new; | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
% | ||
% Wrapper for abs() that also accepts sparse logical matrices and just | ||
% returns them, as opposed to abs() which does not work for logical | ||
% matrices. | ||
% | ||
% RESULT | ||
% ret Absolute value of the argument matrix | ||
% | ||
% PARAMETERS | ||
% A Matrix of which the absolute value is to be computed; | ||
% may be a logical matrix | ||
% | ||
% ABOUT | ||
% This file is part of the KONECT Matlab Toolbox version 0.3. | ||
% konect.uni-koblenz.de | ||
% (c) Jerome Kunegis 2014; this is Free Software released under | ||
% the GPLv3, see COPYING. | ||
% | ||
|
||
function ret = konect_absx(A) | ||
|
||
if islogical(A) | ||
ret = A; | ||
else | ||
ret = abs(A); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
% | ||
% Compute the average precision. | ||
% | ||
% If target is not a 0/1 vector, its values are rounded to 0 or 1. | ||
% | ||
% PARAMETERS | ||
% prediction (e*1) Prediction scores | ||
% target (e*1) Correct scores | ||
% | ||
% RESULT | ||
% ret The average precision | ||
% | ||
% ABOUT | ||
% This file is part of the KONECT Matlab Toolbox version 0.3. | ||
% konect.uni-koblenz.de | ||
% (c) Jerome Kunegis 2014; this is Free Software released under | ||
% the GPLv3, see COPYING. | ||
% | ||
|
||
function ret = konect_ap(prediction, target) | ||
|
||
if length(prediction) ~= length(target), error('*** both vectors must have same length'); end; | ||
|
||
e = length(prediction); | ||
|
||
% Round target to 0/1 | ||
target = target > 0; | ||
|
||
% Randomize order | ||
p = randperm(e); | ||
prediction = prediction(p); | ||
target = target(p); | ||
|
||
% Compte MAP | ||
[tmp,i] = sort(-prediction); | ||
a = target(i); | ||
p_sum = 0; | ||
nz = 0; | ||
|
||
for j = 1 : e | ||
if a(j) ~= 0 | ||
nz = nz + a(j); | ||
p_sum = p_sum + a(j) * nz / j; | ||
end; | ||
end; | ||
|
||
ret = p_sum / nz; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
% | ||
% Compute the average precision, given only the 0/1 vector sorted by | ||
% scores, which don't have to be given. | ||
% | ||
% PARAMETERS | ||
% t (e*1) The 0/1 vector, ranked by descending scores | ||
% | ||
% ABOUT | ||
% This file is part of the KONECT Matlab Toolbox version 0.3. | ||
% konect.uni-koblenz.de | ||
% (c) Jerome Kunegis 2014; this is Free Software released under | ||
% the GPLv3, see COPYING. | ||
% | ||
|
||
function ap = konect_ap_sorted(t) | ||
|
||
ap = mean((1:sum(t))' ./ find(t)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
% | ||
% The area under the ROC curve, i.e. the AUC. | ||
% | ||
% If target is not a 0/1 vector, its values are rounded to 0 or 1. | ||
% | ||
% PARAMETERS | ||
% prediction (e*1) vector of predicted scores | ||
% target (e*1) vector of values to be predicted | ||
% | ||
% RESULT | ||
% ret The AUC | ||
% | ||
% ABOUT | ||
% This file is part of the KONECT Matlab Toolbox version 0.3. | ||
% konect.uni-koblenz.de | ||
% (c) Jerome Kunegis 2014; this is Free Software released under | ||
% the GPLv3, see COPYING. | ||
% | ||
|
||
function ret = konect_auc(prediction, target) | ||
|
||
assert(length(prediction) == length(target)); | ||
|
||
% All values passed are finite | ||
assert(sum(~isfinite(prediction)) == 0); | ||
assert(sum(~isfinite(target)) == 0); | ||
|
||
target = target > 0; | ||
|
||
e = length(prediction); | ||
k = sum(target); | ||
|
||
if k == e | k == 0 | ||
ret = 0; | ||
return; | ||
end | ||
|
||
% Randomize order | ||
p = randperm(e); | ||
prediction = prediction(p); | ||
target = target(p); | ||
|
||
[tmp,x] = sort(prediction, 'descend'); | ||
a = target(x); | ||
|
||
s = 0; | ||
c = e-k; | ||
for i = 1 : e | ||
if a(i) == 0 | ||
c = c - 1; | ||
else | ||
s = s + c; | ||
end | ||
end | ||
|
||
ret = s / (k * (e-k)); |
Oops, something went wrong.