Skip to content

Commit

Permalink
Improved export of LFs with compute templates. Bug fixes (core and ME…
Browse files Browse the repository at this point in the history
…ME.bf)
  • Loading branch information
spond committed Oct 22, 2024
1 parent ff51046 commit a88d72c
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 22 deletions.
2 changes: 1 addition & 1 deletion res/TemplateBatchFiles/SelectionAnalyses/MEME.bf
Original file line number Diff line number Diff line change
Expand Up @@ -1723,7 +1723,7 @@ lfunction meme.store_results (node, result, arguments) {
if (has_psi) {
result_row[has_psi] = estimators.GetGlobalMLE (result[utility.getGlobalValue("terms.alternative")],^"terms.meme.fg_param_prefix"+ ^"terms.parameters.triple_hit_rate");
if (None == result_row[has_psi]) {
result_row[has_delta] = ((scalers['BG'])[^"terms.parameters.triple_hit_rate"])["scaler"];
result_row[has_psi] = ((scalers['BG'])[^"terms.parameters.triple_hit_rate"])["scaler"];
}
^"meme.site_multihit_string" += "/" + Format (result_row[has_psi],0,2);
}
Expand Down
11 changes: 9 additions & 2 deletions src/core/batchlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,18 @@ hyBLFunctionType GetBFFunctionType (long idx) {
}

//____________________________________________________________________________________
_String const ExportBFFunction (long idx, bool recursive) {
_String const ExportBFFunction (long idx, bool recursive, _AVLList* tracker) {


_StringBuffer bf (8192UL);
if (IsBFFunctionIndexValid(idx)) {

if (tracker) {
if (tracker->FindLong (idx) >= 0) {
return bf;
}
tracker->InsertNumber(idx);
}

_String hbf_name = GetBFFunctionNameByIndex (idx);
_ExecutionList * body = &GetBFFunctionBody(idx);
Expand Down Expand Up @@ -567,7 +574,7 @@ _String const ExportBFFunction (long idx, bool recursive) {
bf << "\n/*----- Called function '"
<< *a_name
<< "' ------*/\n"
<< ExportBFFunction (FindBFFunctionName(*a_name), false)
<< ExportBFFunction (FindBFFunctionName(*a_name), tracker ? recursive : false, tracker)
<< "\n\n";
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/batchlanruntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,8 @@ bool _ElementaryCommand::HandleConstructCategoryMatrix (_ExecutionList& cur
_String ("WEIGHTS"),_hyphyLFConstructCategoryMatrixWeights,
_String ("SITE_LOG_LIKELIHOODS"), _hyphyLFConstructCategoryMatrixSiteProbabilities,
_String ("CLASSES"), _hyphyLFConstructCategoryMatrixClasses,
_String ("SHORT"), _hyphyLFConstructCategoryMatrixClasses
_String ("SHORT"), _hyphyLFConstructCategoryMatrixClasses,
_String ("PARTITIONS"), _hyphyLFConstructCategoryMatrixPartitions
);


Expand Down
18 changes: 15 additions & 3 deletions src/core/formula.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,25 @@ _StringBuffer const _Formula::toRPN (_hyFormulaStringConversionMode mode, _List*
return r;
}
//__________________________________________________________________________________
BaseRef _Formula::toStr (_hyFormulaStringConversionMode mode, _List* matched_names, bool drop_tree) {
BaseRef _Formula::toStr (_hyFormulaStringConversionMode mode, _List* matched_names, bool drop_tree, _StringBuffer * hbl_dependencies) {
ConvertToTree(false);

_StringBuffer * result = new _StringBuffer (64UL);

long savepd = print_digit_specification;
print_digit_specification = 0L;



if (theTree) { // there is something to do
SubtreeToString (*result, theTree, -1, matched_names, nil, mode);
if (hbl_dependencies) {
_SimpleList _tracker;
_AVLList tracker (&_tracker);
SubtreeToString (*result, theTree, -1, matched_names, nil, mode, hbl_dependencies,&tracker);

} else {
SubtreeToString (*result, theTree, -1, matched_names, nil, mode);
}
} else {
if (theFormula.countitems()) {
(*result) << "RPN:" << toRPN (mode, matched_names);
Expand Down Expand Up @@ -990,7 +999,7 @@ bool _Formula::InternalSimplify (node<long>* top_node) {


//__________________________________________________________________________________
void _Formula::SubtreeToString (_StringBuffer & result, node<long>* top_node, int op_level, _List* match_names, _Operation* this_node_op, _hyFormulaStringConversionMode mode) {
void _Formula::SubtreeToString (_StringBuffer & result, node<long>* top_node, int op_level, _List* match_names, _Operation* this_node_op, _hyFormulaStringConversionMode mode, _StringBuffer * hbl_dependencies, _AVLList* hbl_tracker) {

if (!this_node_op) {
if (!top_node) {
Expand Down Expand Up @@ -1142,6 +1151,9 @@ void _Formula::SubtreeToString (_StringBuffer & result, node<long>* top_node, in
if (node_op_count < 0L) {
// a user-defined function
long func_id = this_node_op->UserFunctionID();
if (hbl_dependencies && hbl_tracker) {
(*hbl_dependencies) << ExportBFFunction(func_id, true, hbl_tracker);
}
result<< & GetBFFunctionNameByIndex(func_id);
if (top_node) {
result<<'(';
Expand Down
2 changes: 1 addition & 1 deletion src/core/global_things.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ namespace hy_global {
kErrorStringMatrixExportError ("Export matrix called with a non-polynomial matrix argument"),
kErrorStringNullOperand ("Attempting to operate on an undefined value; this is probably the result of an earlier 'soft' error condition"),
kErrorNumerical ("To treat numerical errors as warnings, please specify \"ENV=TOLERATE_NUMERICAL_ERRORS=1;\" as the command line argument. This often resolves the issue, which is indicative of numerical instability."),
kHyPhyVersion = _String ("2.5.62"),
kHyPhyVersion = _String ("2.5.63"),

kNoneToken = "None",
kNullToken = "null",
Expand Down
2 changes: 1 addition & 1 deletion src/core/include/batchlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ _ExecutionList&
GetBFFunctionBody (long);

_String const
ExportBFFunction (long, bool = true);
ExportBFFunction (long, bool = true, _AVLList * = nil);


void ClearBFFunctionLists (long = -1L);
Expand Down
4 changes: 2 additions & 2 deletions src/core/include/formula.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class _Formula {
void Duplicate (_Formula const *, bool deep_copy = false);
void DuplicateReference (const _Formula*);
BaseRef makeDynamic (void) const;
BaseRef toStr (_hyFormulaStringConversionMode mode, _List* matchNames = nil, bool = false);
BaseRef toStr (_hyFormulaStringConversionMode mode, _List* matchNames = nil, bool = false, _StringBuffer* hbl_dependencies = nil);
_StringBuffer const toRPN (_hyFormulaStringConversionMode mode, _List* matchNames = nil);

long ObjectClass (void);
Expand Down Expand Up @@ -288,7 +288,7 @@ class _Formula {

protected:

void SubtreeToString (_StringBuffer & result, node<long>* top_node, int op_level, _List* match_names, _Operation* this_node_op, _hyFormulaStringConversionMode mode = kFormulaStringConversionNormal);
void SubtreeToString (_StringBuffer & result, node<long>* top_node, int op_level, _List* match_names, _Operation* this_node_op, _hyFormulaStringConversionMode mode = kFormulaStringConversionNormal, _StringBuffer * hbl_dependencies = nil, _AVLList* hbl_tracker = nil);
void ConvertToTree (bool err_msg = true);
void ConvertFromTree (void);
bool CheckSimpleTerm (HBLObjectRef);
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/likefunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define _hyphyLFConstructCategoryMatrixWeights 2
#define _hyphyLFConstructCategoryMatrixPosteriors 3
#define _hyphyLFConstructCategoryMatrixSiteProbabilities 4
#define _hyphyLFConstructCategoryMatrixPartitions 5


/* likelihood seialization model */

Expand Down
44 changes: 33 additions & 11 deletions src/core/likefunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,15 @@ _Matrix* _LikelihoodFunction::ConstructCategoryMatrix (const _SimpleList& whi


PrepareToCompute();
if (runMode == _hyphyLFConstructCategoryMatrixPartitions) {
_Matrix * block_likelihoods = new _Matrix (1, whichParts.lLength, false, true);
whichParts.Each([this, block_likelihoods] (long item, unsigned long i) -> void {
block_likelihoods->Store (0, i, ComputeBlock(item));
});
DoneComputing();
return block_likelihoods;
}

if (runMode == _hyphyLFConstructCategoryMatrixConditionals || runMode == _hyphyLFConstructCategoryMatrixWeights)
// just return the matrix with class weights
{
Expand Down Expand Up @@ -1608,9 +1617,8 @@ _Matrix* _LikelihoodFunction::ConstructCategoryMatrix (const _SimpleList& whi



if (runMode == _hyphyLFConstructCategoryMatrixClasses || runMode == _hyphyLFConstructCategoryMatrixSiteProbabilities)
if (runMode == _hyphyLFConstructCategoryMatrixClasses || runMode == _hyphyLFConstructCategoryMatrixSiteProbabilities) {
// just return the maximum conditional likelihood category
{
_Matrix *result = new _Matrix (hDim,vDim,false,true),
*cache = nil;
_SimpleList *scalerCache = nil;
Expand Down Expand Up @@ -10110,6 +10118,22 @@ void _LikelihoodFunction::SerializeLF(_StringBuffer & rec, char opt,
rec.AppendAnAssignmentToBuffer(&hy_env::assume_reversible, new _String(hy_env::EnvVariableGetNumber(hy_env::assume_reversible, 0.)));
rec.AppendAnAssignmentToBuffer(&kUseLastResults, new _String(hy_env::EnvVariableGetNumber(kUseLastResults, 0.)));

/** 20241022 SLKP
if there's a computing template defined, we need to export HBL functions that in may be referencing
*/

_StringBuffer exported_compute_template, template_code;


if (computingTemplate && templateKind != _hyphyLFComputationalTemplateNone) {

template_code << ",\"";
template_code << (_String *)computingTemplate->toStr(kFormulaStringConversionNormal, nil, false, &exported_compute_template);
template_code << '"';
}

rec << exported_compute_template;

if (frequencyVectors.empty()) {
rec << "LikelihoodFunction " << *lfName << " = (";

Expand All @@ -10124,6 +10148,7 @@ void _LikelihoodFunction::SerializeLF(_StringBuffer & rec, char opt,
rec << *GetFilterName(redirector->get(idx)) << ',' << *LocateVar(redirectorT->list_data[idx])->GetName();
}
} else {

rec << "LikelihoodFunction3 " << *lfName << " = (";

long dsID = 0;
Expand All @@ -10137,11 +10162,7 @@ void _LikelihoodFunction::SerializeLF(_StringBuffer & rec, char opt,
}
}

if (computingTemplate && templateKind == 1) {
rec << ",\"";
rec << (_String *)computingTemplate->toStr(kFormulaStringConversionNormal);
rec << '"';
}
rec << template_code;

if (opt == _hyphyLFSerializeModeOptimize) {
rec << ");\n";
Expand Down Expand Up @@ -10201,11 +10222,12 @@ void _LikelihoodFunction::SerializeLF(_StringBuffer & rec, char opt,
//_______________________________________________________________________________________

BaseRef _LikelihoodFunction::toStr (unsigned long) {
hyFloat longOrShort,

hyFloat longOrShort = hy_env::EnvVariableGetNumber (likefuncOutput, 2.0),
value = 0.0;

checkParameter(likefuncOutput,longOrShort,2.0);


if (longOrShort < 4.0) {
PrepareToCompute();
value = Compute();
Expand Down Expand Up @@ -10403,7 +10425,7 @@ BaseRef _LikelihoodFunction::toStr (unsigned long) {
_Variable* v = GetIthDependentVar(i);
_String value ((_String*)v->GetFormulaString(kFormulaStringConversionNormal), kAppendAnAssignmentToBufferPlain);
value = value & " = " & _String ((_String*)v->toStr());
res->AppendAnAssignmentToBuffer(GetIthDependentName(i), &value);
res->AppendAnAssignmentToBuffer(GetIthDependentName(i), &value, kAppendAnAssignmentToBufferPlain);
}
} else {
* res << _String (value, "%15.15g");
Expand Down

0 comments on commit a88d72c

Please sign in to comment.