5
$\begingroup$

I have just encountered what I would term an unexpected disagreement in behaviour between Dataset and List. Consider the following:

ClearAll[dat];
dat = Dataset[{
    <|"sml" -> "CCSCCSP(=S)(OCC)OCC", "pur" -> "A"|>,
    <|"sml" -> "NC(=O)C=3C(=O)[C@@]4(O)C(O)=C2C(=O)c1c(O)cccc1[C@@](C)(O)[C@H]2[C@H](O)[C@H]4[C@@H](C=3O)N(C)C", "pur" -> "D"|>,
    <|"sml" -> "CN1C[C@@H](C[C@@H]2c3cccc4ncc(C[C@@H]12)c34)C(=O)N[C@@]8(O[C@]7(O)N([C@@H](Cc5ccccc5)C(=O)N6CCC[C@H]67)C8=O)C(C)C", "pur" -> "A"|>
}];

ClearAll[tmp1, tmp2];

(* Choose all the rows whose SMILES are parsed as proper molecules. *)
tmp1 = dat // (Query[Select[Key["sml"] /* Molecule /* MoleculeQ]] /* Quiet);

(* Convert the dataset to a list, then choose the rows that can be parsed correctly. *)
tmp2 = dat // (
    Query[All, Values] /* Normal /*
    Select[First /* Molecule /* MoleculeQ] /*
    Quiet
);

tmp1
(* Failure[Molecule, <|"MessageTemplate" :> MessageName[Molecule, "aromat"], "MessageParameters" -> {{7, 8, 9, 10, 11, 12, 13, 14, 17}}|>] *)

tmp2
(*
{
    {"CCSCCSP(=S)(OCC)OCC", "A"},
    {"NC(=O)C=3C(=O)[C@@]4(O)C(O)=C2C(=O)c1c(O)cccc1[C@@](C)(O)[C@H]2[C@H](O)[C@H]4[C@@H](C=3O)N(C)C", "D"},
    {"CN1C[C@@H](C[C@@H]2c3cccc4ncc(C[C@@H]12)c34)C(=O)N[C@@]8(O[C@]7(O)N([C@@H](Cc5ccccc5)C(=O)N6CCC[C@H]67)C8=O)C(C)C", "A"}
}
*)

In other words, in the case of tmp2, Mathematica evaluated the test as successful for every line in the list, in spite of the Failure message that would have been printed in the absence of the Quiet function.

But in the case of tmp1, Mathematica decided to abort the computation as soon as it encountered the failure and not return the entire Dataset.

In this behaviour intended?

$\endgroup$
0

2 Answers 2

7
$\begingroup$

Either move the Quiet inside the body of Query, as in

Query[Select[Quiet[MoleculeQ[Molecule[#sml]]] &]][dat]

enter image description here

or use the FailureAction option for Query:

Query[Select[MoleculeQ[Molecule[#sml]] &], FailureAction -> None][dat]

enter image description here

But returning a Failure when encountering a message seems to be the default behavior for Query.

$\endgroup$
3
$\begingroup$

The error messages comes from different parts in the two examples.

Consider the first part of the example with tmp1:

dat // (Query[Select[Key["sml"]]]) // FullForm

This query returns an empty dataset. I am not sure but I think the Select is the culprit.

On the other hand, the first part of tmp2 works:

t=dat // (Query[All, Values]/*Normal)

But the test in Select screws up. Consider the Select with an additional Print statement and traditional style:

Select[t , (Print["Inp=", #]; MoleculeQ[Molecule[First[#]]]) &]

If this test is done on the third element of "t" it throws the error mentioned above. We can test this by:

Molecule[First[{"CN1C[C@@H](C[C@@H]2c3cccc4ncc(C[C@@H]12)c34)C(=O)N[C@@]\
8(O[C@]7(O)N([C@@H](Cc5ccccc5)C(=O)N6CCC[C@H]67)C8=O)C(C)C", "A"}]]

![

Even if an error is thrown, the result still has the Head "Molecule" and "MoleculeQ" returns "True":

MoleculeQ[
 Molecule[First[{"CN1C[C@@H](C[C@@H]2c3cccc4ncc(C[C@@H]12)c34)C(=O)N[\
C@@]8(O[C@]7(O)N([C@@H](Cc5ccccc5)C(=O)N6CCC[C@H]67)C8=O)C(C)C", 
    "A"}]]]

(* True *)

Therefore, tmp2 gives a result.

$\endgroup$
1
  • $\begingroup$ This query returns an empty dataset. I am not sure but I think the Select is the culprit. We should expect an empty dataset for this, no? After all, Key["sml"] by itself is not a predicate. It becomes one when composed with the other two functions: Key["sml"] /* Molecule /* MoleculeQ. As regards the point about the head Molecule, I get the same results when I replace MoleculeQ with FailureQ. $\endgroup$ Jan 24 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.