OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-rocca commits / 05df500

Improve OpencogAgent.save_percepta_atomspace
05df500bc0a33d7c11f6cc1d032fdc19477d7e46
by Nil Geisweiller <ngeiswei@gmail.com> · 2022-05-20T10:34:34.000Z

Changed files (2) +51 -30

rocca/agents/core.py +51 -8
……@@ -320,10 +320,48 @@ class OpencogAgent:
320320 self.percepta_record.append(set())
321321 self.percepta_record[i].add(timed_atom)
322322
323 def timed_percepta_to_scheme_str(self, timed_percepta: set[Atom], with_member: bool = False) -> str:
324 """Convert percepta at a given cycle into a string in Scheme format.
325
326 Percepta are preceded by a comment in human readable form.
327
328 If with_member is set to True then each perceptum is wrapped with
329 a member link from it to the percepta record concept.
330
331 """
332
333 # Wrap member to percepta record concept around each perceptum
334 if with_member:
335 timed_percepta = {self.add_to_percepta_atomspace(timed_perceptum)
336 for timed_perceptum in timed_percepta}
337 cmt = "\n".join(";; " + to_human_readable_str(tpm) for tpm in timed_percepta)
338 scm = "\n".join(tpm.long_string() for tpm in timed_percepta)
339 return "\n".join([cmt, scm])
340
341 def percepta_record_to_scheme_str(self, with_member: bool = False) -> str:
342 """Convert a percepta record into a string in Scheme format.
343
344 Each perception is preceded by a comment in human readable form.
345
346 """
347
348 return "\n\n".join([self.timed_percepta_to_scheme_str(tp, with_member)
349 for tp in self.percepta_record])
350
351 def add_to_percepta_atomspace(self, timed_atom: Atom) -> Atom:
352 """Add member link around timestamped atom to Percepta Atomspace.
353
354 Return the added member link.
355
356 """
357
358 mbr = MemberLink(timed_atom, self.percepta_record_cpt, tv=TRUE_TV)
359 return self.percepta_atomspace.add_atom(mbr)
360
323361 def record(self, atom: Atom, i: int, tv=None) -> Atom:
324362 """Timestamp and record an atom to the Percepta Record.
325363
326 That is add the following in the atomspace
364 That is add the following in the percepta atomspace
327365
328366 MemberLink (stv 1 1)
329367 AtTimeLink <tv>
……@@ -337,9 +375,8 @@ class OpencogAgent:
337375 """
338376
339377 timed_atom = timestamp(atom, i, tv)
340 mbr = MemberLink(timed_atom, self.percepta_record_cpt, tv=TRUE_TV)
341378 self.insert_to_percepta_record(timed_atom, i)
342 return self.percepta_atomspace.add_atom(mbr)
379 return self.add_to_percepta_atomspace(timed_atom)
343380
344381 def make_goal(self) -> Atom:
345382 """Define the goal of the current iteration.
……@@ -482,7 +519,7 @@ class OpencogAgent:
482519 # debugging the pattern miner
483520 agent_log.fine(
484521 "Percepta record:\n{}".format(
485 percepta_record_to_scheme_str(self.percepta_record)
522 self.percepta_record_to_scheme_str()
486523 )
487524 )
488525
……@@ -1609,10 +1646,9 @@ class OpencogAgent:
16091646
16101647 """
16111648
1612 # TODO: better print the percepta_record using
1613 # percepta_record_to_scheme_str (though the member links
1614 # should also be included).
1615 return save_atomspace(self.percepta_atomspace, filepath, overwrite)
1649 with open(filepath, "w" if overwrite else "a") as file:
1650 file.write(self.percepta_record_to_scheme_str(with_member=True) + "\n")
1651 return True
16161652
16171653 def load_percepta_atomspace(
16181654 self, filepath: str, overwrite: bool = True, fast: bool = True
……@@ -1636,10 +1672,17 @@ class OpencogAgent:
16361672 so that new percepts do not have the same timestamps as the
16371673 just loaded ones.
16381674
1675 It is assumed that the percepta record concept is
1676
1677 Concept "Percepta Record"
1678
16391679 """
16401680
16411681 success = load_atomspace(self.percepta_atomspace, filepath, overwrite)
16421682 if success:
1683 pas_roots = atomspace_roots(self.percepta_atomspace)
1684 if overwrite:
1685 self.percepta_record.clear()
16431686 print("TODO")
16441687 # NEXT: take care of filling the percepta record
16451688 # NEXT: update the cycle count as well
rocca/agents/utils.py +0 -22
……@@ -1227,28 +1227,6 @@ def atoms_to_scheme_str(atoms: set[Atom] | list[Atom], only_id: bool = False) ->
12271227 return "\n".join(msgs)
12281228
12291229
1230def timed_percepta_to_scheme_str(timed_percepta: set[Atom]) -> str:
1231 """Convert percepta at a given cycle into a string in Scheme format.
1232
1233 Percepta are preceded by a comment in human readable form.
1234
1235 """
1236
1237 cmt = "\n".join(";; " + to_human_readable_str(tpm) for tpm in timed_percepta)
1238 scm = "\n".join(tpm.long_string() for tpm in timed_percepta)
1239 return "\n".join([cmt, scm])
1240
1241
1242def percepta_record_to_scheme_str(percepta_record: list[set[Atom]]) -> str:
1243 """Convert a percepta record into a string in Scheme format.
1244
1245 Each perception is preceded by a comment in human readable form.
1246
1247 """
1248
1249 return "\n".join([timed_percepta_to_scheme_str(tp) for tp in percepta_record])
1250
1251
12521230def save_atomspace(atomspace: AtomSpace, filepath: str, overwrite: bool = True) -> bool:
12531231 """Save the given atomspace at the indicated filepath.
12541232