class_docstr: Basic tests on the util functions in TorchAgent.
class_docstr_tok: ["Basic","tests","on","the","util","functions","in","TorchAgent","."]
docstr: Make sure the batchify function sets up the right fields.
docstr_tok: ["Make","sure","the","batchify","function","sets","up","the","right","fields","."]
code: def test_batchify(self):
      """
      Make sure the batchify function sets up the right fields.
      """
      agent = get_agent(rank_candidates=True)
      obs_labs = [
          Message(
              {
                  "text": "It's only a flesh wound.",
                  "labels": ["Yield!"],
                  "episode_done": True,
              }
          ),
          Message(
              {
                  "text": "The needs of the many outweigh...",
                  "labels": ["The needs of the few."],
                  "episode_done": True,
              }
          ),
          Message(
              {
                  "text": "Hello there.",
                  "labels": ["General Kenobi."],
                  "episode_done": True,
              }
          ),
      ]
      obs_elabs = [
          Message(
              {
                  "text": "It's only a flesh wound.",
                  "eval_labels": ["Yield!"],
                  "episode_done": True,
              }
          ),
          Message(
              {
                  "text": "The needs of the many outweigh...",
                  "eval_labels": ["The needs of the few."],
                  "episode_done": True,
              }
          ),
          Message(
              {
                  "text": "Hello there.",
                  "eval_labels": ["General Kenobi."],
                  "episode_done": True,
              }
          ),
      ]
      for obs_batch in (obs_labs, obs_elabs):
          lab_key = (
              "labels" if "labels" in obs_batch[0] else "eval_labels"
          )
  
          # nothing has been vectorized yet so should be empty
          batch = agent.batchify(obs_batch)
          self.assertIsNone(batch.text_vec)
          self.assertIsNone(batch.label_vec)
          self.assertIsNone(batch.labels)
          self.assertIsNone(batch.candidates)
          self.assertIsNone(batch.candidate_vecs)
          self.assertIsNone(batch.image)
  
          obs_vecs = []
          for o in obs_batch:
              agent.history.reset()
              agent.history.update_history(o)
              obs_vecs.append(
                  agent.vectorize(
                      o, agent.history, add_start=False, add_end=False
                  )
              )
  
          # is_valid should map to nothing
          def is_valid(obs):
              return False
  
          agent.is_valid = is_valid
  
          batch = agent.batchify(obs_batch)
          self.assertIsNone(batch.text_vec)
          self.assertIsNone(batch.label_vec)
          self.assertIsNone(batch.labels)
          self.assertIsNone(batch.candidates)
          self.assertIsNone(batch.candidate_vecs)
          self.assertIsNone(batch.image)
  
          # is_valid should check for text_vec
          def is_valid(obs):
              return "text_vec" in obs
  
          agent.is_valid = is_valid
  
          batch = agent.batchify(obs_vecs)
          # which fields were filled vs should be empty?
          self.assertIsNotNone(batch.text_vec)
          self.assertIsNotNone(batch.label_vec)
          self.assertIsNotNone(batch.labels)
          self.assertIsNone(batch.candidates)
          self.assertIsNone(batch.candidate_vecs)
          self.assertIsNone(batch.image)
  
          # contents of certain fields:
          self.assertEqual(
              batch.text_vec.tolist(),
              [
                  [1, 2, 3, 4, 5, 0],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 0, 0, 0, 0],
              ],
          )
          self.assertEqual(
              batch.label_vec.tolist(),
              [[1, 0, 0, 0, 0], [1, 2, 3, 4, 5], [1, 2, 0, 0, 0]],
          )
          self.assertEqual(
              batch.labels, [o[lab_key][0] for o in obs_batch]
          )
          self.assertEqual(list(batch.valid_indices), [0, 1, 2])
  
          # now sort the batch, make sure fields are in sorted order
          batch = agent.batchify(obs_vecs, sort=True)
          self.assertEqual(
              batch.text_vec.tolist(),
              [
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 0],
                  [1, 2, 0, 0, 0, 0],
              ],
          )
          self.assertEqual(
              batch.label_vec.tolist(),
              [[1, 2, 3, 4, 5], [1, 0, 0, 0, 0], [1, 2, 0, 0, 0]],
          )
          labs = [o[lab_key][0] for o in obs_batch]
          self.assertEqual(batch.labels, [labs[i] for i in [1, 0, 2]])
          self.assertEqual(list(batch.valid_indices), [1, 0, 2])
  
          # now sort just on ys
          new_vecs = [vecs.copy() for vecs in obs_vecs]
          for vec in new_vecs:
              vec.pop("text")
              vec.pop("text_vec")
  
          def is_valid(obs):
              return "labels_vec" in obs or "eval_labels_vec" in obs
  
          agent.is_valid = is_valid
  
          batch = agent.batchify(new_vecs, sort=True)
          self.assertIsNone(batch.text_vec)
          self.assertIsNotNone(batch.label_vec)
          self.assertEqual(
              batch.label_vec.tolist(),
              [[1, 2, 3, 4, 5], [1, 2, 0, 0, 0], [1, 0, 0, 0, 0]],
          )
          labs = [o[lab_key][0] for o in new_vecs]
          self.assertEqual(batch.labels, [labs[i] for i in [1, 2, 0]])
          self.assertEqual(list(batch.valid_indices), [1, 2, 0])
  
          # test is_valid
          def is_valid(obs):
              return "text_vec" in obs and len(obs["text_vec"]) < 3
  
          agent.is_valid = is_valid
  
          batch = agent.batchify(obs_vecs)
          self.assertEqual(batch.text_vec.tolist(), [[1, 2]])
          self.assertEqual(batch.label_vec.tolist(), [[1, 2]])
          self.assertEqual(batch.labels, obs_batch[2][lab_key])
          self.assertEqual(list(batch.valid_indices), [2])
  
      agent.history.reset()
      obs_cands = [
          agent.vectorize(
              Message({"label_candidates": ["A", "B", "C"]}),
              agent.history,
          ),
          agent.vectorize(
              Message(
                  {"label_candidates": ["1", "2", "5", "3", "Sir"]}
              ),
              agent.history,
          ),
          agent.vectorize(
              Message({"label_candidates": ["Do", "Re", "Mi"]}),
              agent.history,
          ),
          agent.vectorize(
              Message({"label_candidates": ["Fa", "So", "La", "Ti"]}),
              agent.history,
          ),
      ]
  
      # is_valid should check for label candidates vecs
      def is_valid(obs):
          return "label_candidates_vecs" in obs
  
      agent.is_valid = is_valid
  
      batch = agent.batchify(obs_cands)
      self.assertTrue(
          agent.rank_candidates, "Agent not set up to rank."
      )
      self.assertIsNone(batch.text_vec)
      self.assertIsNone(batch.label_vec)
      self.assertIsNone(batch.labels)
      self.assertIsNotNone(batch.valid_indices)
      self.assertIsNotNone(batch.candidates)
      self.assertIsNotNone(batch.candidate_vecs)
      self.assertEqual(list(batch.valid_indices), [0, 1, 2, 3])
      self.assertEqual(
          batch.candidates, [o["label_candidates"] for o in obs_cands]
      )
      self.assertEqual(len(batch.candidate_vecs), len(obs_cands))
      for i, cs in enumerate(batch.candidate_vecs):
          self.assertEqual(
              len(cs), len(obs_cands[i]["label_candidates"])
          )
  
imports: import os
  import unittest
  from parlai.core.agents import create_agent_from_shared
  from parlai.utils.testing import tempdir
  from parlai.utils.misc import Message
label_desc: Use image node_id 6 for calling the TestTorchAgent obj's underlying member method code with example usage: obj.test_batchify() and returns: unknown, False, unknown, unknown, unknown