Skip to content

Commit

Permalink
barrier-less combine_dissims.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrover committed May 5, 2021
1 parent da3b8da commit 919ed29
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 183 deletions.
199 changes: 120 additions & 79 deletions dissim/combine_dissims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,22 @@ struct Scale
{
Real unit {NaN};
Real raw_max {NaN};
//
Real dissim_min {NaN};
Real dissim_center {NaN};


explicit Scale (const string &line/*,
Real coeff*/)
Scale (const string &line,
Real dissim_min_arg,
Real dissim_center_arg)
: dissim_min (nvlReal (dissim_min_arg, 0.0))
, dissim_center (nvlReal (dissim_center_arg, 0.0))
{
istringstream iss (line);
string unitS, raw_maxS;
iss >> unitS >> raw_maxS;
unit = str2real (unitS);
raw_max = str2real (raw_maxS);
//unit *= coeff;
if (isNan (unit))
{
QC_ASSERT (isNan (raw_max));
Expand All @@ -70,10 +75,19 @@ struct Scale
{
QC_ASSERT (unit > 0.0);
QC_ASSERT (raw_max > 0.0);
ASSERT (! isNan (dissim_max ()));
QC_ASSERT (dissim_min <= dissim_center);
QC_ASSERT (dissim_center < dissim_max ());
}
}
Scale () = default;
void saveText (ostream &os) const
{ os << unit
<< '\t' << raw_max
<< '\t' << dissim_min
<< '\t' << dissim_center
<< '\t' << dissim_max ()
<< endl;
}


bool valid () const
Expand All @@ -82,19 +96,27 @@ struct Scale
{ if (isNan (raw))
return NaN;
QC_ASSERT (raw >= 0.0);
if (! valid ())
return NaN;
if (raw > raw_max)
return NaN;
return raw * unit;
}
Real dissim_max () const
{ return raw2dissim (raw_max); }
Real getWeight (Real dissim) const
// Return: 0..1
{ if (dissim < dissim_min) // May be: dissim_min = dissim_center = 0
return 0.0;
if (dissim >= dissim_max ())
return 0.0;
if (dissim < dissim_center)
return (dissim - dissim_min) / (dissim_center - dissim_min);
if (dissim_max () == inf)
return 1.0;
return (dissim_max () - dissim) / (dissim_max () - dissim_center);
}
};




struct ThisApplication : Application
{
ThisApplication ()
Expand All @@ -104,7 +126,7 @@ struct ThisApplication : Application
addPositional ("dissims", "File with lines: <obj1> <obj2> <dissimilarity 1> <dissimilarity 2> ...");
addPositional ("scales", "File with equalizing scales and max. values for each dissimilarity, ordered by <unit> * <raw_max>.\n\
Line format: <unit> <raw_max>");
//addKey ("coeff", "Coefficient to multiply all dissimilarities, > 0", "1");
addFlag ("print_raw", "Print raw dissimilarities");
}


Expand All @@ -113,101 +135,120 @@ Line format: <unit> <raw_max>");
{
const string dissimFName = getArg ("dissims");
const string scaleFName = getArg ("scales");
//const Real coeff = str2real (getArg ("coeff"));
//QC_ASSERT (coeff > 0.0);
const bool print_raw = getFlag ("print_raw");


Vector<Scale> scales; scales. reserve (16); // PAR
{
LineInput f (scaleFName);
Scale prev_prev;
Scale prev;
while (f. nextLine ())
{
const Scale scale (f. line/*, coeff*/);
const Scale scale (f. line, prev_prev. dissim_max (), prev. dissim_max ());
scales << scale;
if ( prev. valid ()
&& scale. valid ()
&& prev. dissim_max () >= scale. dissim_max ()
)
throw runtime_error ("dissim_max must increase");
if (scale. valid ())
{
prev_prev = prev;
prev = scale;
}
}
// At least one valid Scale should exist
QC_ASSERT (prev. valid ());
QC_ASSERT (prev. raw_max == inf);
}
QC_ASSERT (scales. size () >= 2);


LineInput f (dissimFName, 1024 * 100, 1);
string name1;
string name2;
string dissimS;
Istringstream iss;
LineInput f (dissimFName, 1024 * 100, 1); // PAE
while (f. nextLine ())
{
iss. reset (f. line);
iss >> name1 >> name2;
QC_ASSERT (! name2. empty ());
if (name1 > name2)
swap (name1, name2);
ASSERT (name1 < name2);

Real dissim = NaN;
{
Real dissim_prev = NaN;
const Scale* scale_prev = nullptr;
const Scale* scale_prev_prev = nullptr;
for (const Scale& scale : scales)
{
dissimS. clear ();
iss >> dissimS;
QC_ASSERT (! dissimS. empty ());
const Real raw = str2real (dissimS);
QC_IMPLY (! isNan (raw), raw >= 0.0);
if (! scale. valid ())
continue;
dissim = scale. raw2dissim (raw);
if (dissim == 0.0 && scale_prev)
dissim = NaN;
if (isNan (dissim))
{
if (! isNan (dissim_prev))
{
dissim = dissim_prev;
break;
}
}
else
stringstream iss (f. line);

{
string name1;
string name2;
iss >> name1 >> name2;
QC_ASSERT (! name2. empty ());
if (name1 > name2)
swap (name1, name2);
ASSERT (name1 < name2);
cout << name1
<< '\t' << name2;
}

Real dissim_weighted_sum = 0.0;
Real weight_sum = 0.0;
bool first = true;
size_t n = 0;
for (const Scale& scale : scales)
{
string dissimS;
iss >> dissimS;
QC_ASSERT (! dissimS. empty ());

if (! scale. valid ())
continue;

const Real raw = str2real (dissimS);
if (print_raw)
cout << '\t' << raw;
if (isNan (raw))
{
if (n)
break;
}
else
{
if (n >= 2)
break;
const Real dissim = scale. raw2dissim (raw);
const Real weight = scale. getWeight (dissim);
ASSERT (dissim >= 0.0);
ASSERT (weight >= 0.0);
dissim_weighted_sum += weight * dissim;
weight_sum += weight;
n++;
if (verbose ())
{
ASSERT (dissim >= 0.0);
if (isNan (dissim_prev))
dissim_prev = dissim;
else
{
// Merging dissimilarities of adjacent scales
ASSERT (scale_prev);
const Real dissim_prev_max = scale_prev->dissim_max ();
ASSERT (dissim_prev <= dissim_prev_max);
const Real dissim_prev_prev_max = scale_prev_prev ? scale_prev_prev->dissim_max () : 0.0;
ASSERT (dissim_prev_prev_max < dissim_prev_max);
if (dissim_prev <= dissim_prev_prev_max)
dissim = dissim_prev;
else
{
const Prob weight = (dissim_prev - dissim_prev_prev_max) / (dissim_prev_max - dissim_prev_prev_max);
dissim = weight * dissim + (1.0 - weight) * dissim_prev;
}
break;
}
cout << endl;
cout << "scale: ";
scale. saveText (cout);
PRINT (raw);
PRINT (dissim);
PRINT (weight);
PRINT (first);
PRINT (dissim_weighted_sum);
PRINT (weight_sum);
}
scale_prev_prev = scale_prev;
scale_prev = & scale;
}

if ( first
&& dissim_weighted_sum == 0.0
&& weight_sum > 0.0
)
break;

first = false;
}
if ( ! first
&& dissim_weighted_sum == 0.0
&& weight_sum > 0.0
)
weight_sum = 0.0;
if (verbose ())
{
PRINT (first);
PRINT (dissim_weighted_sum);
PRINT (weight_sum);
}
cout << name1
<< '\t' << name2
<< '\t' << dissim

cout << '\t' << dissim_weighted_sum / weight_sum
<< endl;
}
}
Expand Down
32 changes: 18 additions & 14 deletions dissim/evolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@ namespace DM_sp
{


Real intersection2dissim (Real size1,
Real size2,
Real intersection,
Real intersection_min,
Prob sizes_ratio_min,
bool ave_arithP)
Real maps2dissim (Real size1,
Real size2,
Real maps1,
Real maps2,
Real maps_min,
Prob sizes_ratio_min,
bool ave_arithP)
{
ASSERT (size1 >= 0);
ASSERT (size2 >= 0);
ASSERT (intersection <= size1);
ASSERT (intersection <= size2);
ASSERT (size1 >= 0.0);
ASSERT (size2 >= 0.0);
ASSERT (maps1 <= size1);
ASSERT (maps2 <= size2);
ASSERT (isProb (sizes_ratio_min));


if (verbose ())
cout << '\t' << intersection
cout << '\t' << maps1
<< '\t' << maps2
<< '\t' << size1
<< '\t' << size2
<< endl;
Expand All @@ -75,11 +77,13 @@ Real intersection2dissim (Real size1,
)
return NaN;

if (intersection < intersection_min)
if (maps1 < maps_min)
return NaN;
if (maps2 < maps_min)
return NaN;

const Real dissim1 = log (size1 / intersection);
const Real dissim2 = log (size2 / intersection);
const Real dissim1 = log (size1 / maps1);
const Real dissim2 = log (size2 / maps2);
ASSERT (dissim1 >= 0.0);
ASSERT (dissim2 >= 0.0);

Expand Down
23 changes: 16 additions & 7 deletions dissim/evolution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,24 @@ namespace DM_sp
{


Real intersection2dissim (Real size1,
Real size2,
Real intersection,
Real intersection_min,
Prob sizes_ratio_min,
bool ave_arithP);
Real maps2dissim (Real size1,
Real size2,
Real maps1,
Real maps2,
Real maps_min,
Prob sizes_ratio_min,
bool ave_arithP);
// Input: ave_arithP; false <=> ave_harm()
// Return: >= 0; may be NaN
// Symmetric

inline Real intersection2dissim (Real size1,
Real size2,
Real intersection,
Real intersection_min,
Prob sizes_ratio_min,
bool ave_arithP)
{ return maps2dissim (size1, size2, intersection, intersection, intersection_min, sizes_ratio_min, ave_arithP); }
// Symmetric



Expand Down
Loading

0 comments on commit 919ed29

Please sign in to comment.