def test_pull_doc_examples(self): """ Pull out WCON examples from all .MD files and validate them. """ cur_path = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', '..')) md_paths = [ glob.glob(os.path.join(x[0], '*.md')) for x in os.walk(cur_path) ] md_paths = [x for x in md_paths if x != []] # Flatten, since the lists might be nested md_paths = list(flatten(md_paths)) for md_path in md_paths: print("Checking for JSON code in %s " % md_path) with open(md_path, 'r') as f: md_string = f.read() # Find all code examples code_snippets = md_string.split('```')[1::2] # Consider only JSON code examples JSON_snippets = [ s[4:] for s in code_snippets if s[:4] == 'JSON' ] for i, JSON_snippet in enumerate(JSON_snippets): print("Testing JSON code snippet %i from %s" % (i + 1, md_path)) JSON_snippet = JSON_snippet.replace('\n', '') JSON_snippet = JSON_snippet.replace('\r', '') WCONWorms.load(StringIO(JSON_snippet))
def test_pull_doc_examples(self): """ Pull out WCON examples from all .MD files and validate them. """ cur_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')) md_paths = [glob.glob(os.path.join(x[0], '*.md')) for x in os.walk(cur_path)] md_paths = [x for x in md_paths if x != []] # Flatten, since the lists might be nested md_paths = list(flatten(md_paths)) for md_path in md_paths: print("Checking for JSON code in %s " % md_path) with open(md_path, 'r') as f: md_string = f.read() # Find all code examples code_snippets = md_string.split('```')[1::2] # Consider only JSON code examples JSON_snippets = [s[4:] for s in code_snippets if s[:4] == 'JSON'] for i, JSON_snippet in enumerate(JSON_snippets): print("Testing JSON code snippet %i from %s" % (i + 1, md_path)) JSON_snippet = JSON_snippet.replace('\n', '') JSON_snippet = JSON_snippet.replace('\r', '') WCONWorms.load(StringIO(JSON_snippet))
def test_chunks(self): """ Test load_from_file with two or more chunks """ # Define our chunks chunks = [] chunks.append( ('{"files":{"current":"0.wcon", "prev":null,' '"next":["1.wcon", "2.wcon"]},' '"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":"3", "t":[1.3], ' '"x":[[3,4]], "y":[[5.4,3]]}]}')) chunks.append(('{"units":{"t":"s","x":"mm","y":"mm"},' '"files":{"current":"1.wcon", "prev":["0.wcon"],' '"next":["2.wcon"]},' '"data":[{"id":"3", "t":[1.4], ' '"x":[[5,1]], "y":[[15.4,3]]}]}')) chunks.append( ('{"units":{"t":"s","x":"mm","y":"mm"},' '"files":{"current":"2.wcon", "prev":["1.wcon", "0.wcon"],' '"next":null},' '"data":[{"id":"3", "t":[1.5], ' '"x":[[8,4.2]], "y":[[35.4,3]]}]}')) # Create filenames for our chunks chunk_filenames = [''] * len(chunks) for (chunk_index, chunk) in enumerate(chunks): chunk_filenames[chunk_index] = \ 'test_chunk_' + str(chunk_index) + '.wcon' # Save these chunks as files for (chunk_index, chunk) in enumerate(chunks): with open(chunk_filenames[chunk_index], 'w') as outfile: outfile.write(chunk) # First load one of them worm_combined_manually = WCONWorms.load(StringIO(chunks[0])) # Then merge the others sequentially to the first one for chunk in chunks[1:]: worm_chunk = WCONWorms.load(StringIO(chunk)) worm_combined_manually += worm_chunk # Validate that the chunks together are __eq__ to the files # that find each other through their "files" object for chunk_filename in chunk_filenames: # Worm from files that found each other through # the "files" object worm_from_files = WCONWorms.load_from_file(chunk_filename) self.assertEqual(worm_from_files, worm_combined_manually) # Delete the files created for chunk_filename in chunk_filenames: os.remove(chunk_filename)
def test_arrayed_software(self): WCONWorms.load(StringIO('{ "units":{"t":"s", "x":"mm", "y":"mm"}, ' '"data":[], "metadata":{' '"software":[{"name":"a"}, ' '{"name":"b"}]} }')) # With "settings" field WCONWorms.load(StringIO('{ "units":{"t":"s", "x":"mm", "y":"mm"}, ' '"data":[], "metadata":{' '"software":[{"name":"a", "settings": ' '{"h": 5}}, {"name":"b"}]} }'))
def test_merge_commutativity(self): worm1 = WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":3, "t":1.3, "x":[3,4], "y":[5.4,3]}]}')) worm2 = WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"}, ' '"data":[{"id":4, "t":1.5, "x":[5,2], "y":[1.4,6]}]}')) merged = worm1 + worm2 # merged.save_to_file('pythonMerged.wcon', pretty_print=True) merged2 = worm2 + worm1 # merged2.save_to_file('pythonMerged2.wcon', pretty_print=True) self.assertNotEqual(worm1, worm2) self.assertEqual(merged, merged2)
def test_empty_aspect_size(self): # Worms with a segment that is empty should still parse without issue. WCON_string = \ """ { "units":{"t":"s", "x":"mm", "y":"mm"}, "data":[{ "id":2, "t":1.4, "x":[125.11, 126.14, 117.12], "y":[23.3, 22.23, 21135.08] }, { "id":1, "t":1.4, "x":[1215.11, 1216.14, 1217.12], "y":[234.89, 265.23, 235.08] }, { "id":2, "t":1.5, "x":[1215.11, 1216.14, 1217.12], "y":[234.89, 265.23, 235.08] }, { "id":1, "t":[1.3,1.5], "x":[[],[1215.11, 1216.14, 1217.12]], "y":[[],[234.89, 265.23, 235.08]] } ] } """ w = WCONWorms.load(StringIO(WCON_string)) test_path = 'test_empty_aspect.wcon' w.save_to_file(test_path, pretty_print=True) w_from_saved = WCONWorms.load_from_file(test_path) self.assertEqual(w, w_from_saved) os.remove(test_path)
def test_bad_files_object(self): """ Test that a badly specified "files" object will fail """ # "this":null should be disallowed by the schema worm_file_text1 = (( '{"files":{"this":null, "prev":null, "next":["_1", "_2"]},' '"units":{"t":"s","x":"mm","y":"mm"},"data":[{"id":3, "t":1.3, ' '"x":[3,4], "y":[5.4,3]}]}')) with self.assertRaises(jsonschema.exceptions.ValidationError): WCONWorms.load(StringIO(worm_file_text1)) # "this":"" should be disallowed by the schema worm_file_text2 = (( '{"files":{"this":"", "prev":null, "next":["_1", "_2"]},' '"units":{"t":"s","x":"mm","y":"mm"},"data":[{"id":3, "t":1.3, ' '"x":[3,4], "y":[5.4,3]}]}')) with self.assertRaises(jsonschema.exceptions.ValidationError): WCONWorms.load(StringIO(worm_file_text2))
def test_merge_user_defined_constants(self): """ This example is pulled right from the specification""" w1 = WCONWorms.load( StringIO( '{"units":{"t":"s", "x":"mm", "y":"mm", "@XJ z":"mm"},' '"data":[{"id":"0", "t":[1,2], "x":[0,1], "y":[1,0],' '"@XJ z":[3,4], "@XJ g":9.8, "@XJ f":"salmon"},' '{"id":"0", "t":[3,4,5], "x":[1,0,1], "y":[2,3,2],' '"@XJ z":[5,6,5], "@XJ g":9.8, "@XJ f":"cod"}]}')) w2 = WCONWorms.load( StringIO( '{"units":{"t":"s", "x":"mm", "y":"mm", "@XJ z":"mm"},' '"data":[{"id":"0", "t":[1,2,3,4,5], ' '"x":[0,1,1,0,1], "y":[1,0,2,3,2],' '"@XJ z":[3,4,5,6,5], "@XJ g":9.8, ' '"@XJ f":["salmon", "salmon", "cod", "cod", "cod"]}]}')) self.assertEqual(w1, w2)
def test_bad_files_object(self): """ Test that a badly specified "files" object will fail """ # "this":null should be disallowed by the schema worm_file_text1 = ( ('{"files":{"this":null, "prev":null, "next":["_1", "_2"]},' '"units":{"t":"s","x":"mm","y":"mm"},"data":[{"id":3, "t":1.3, ' '"x":[3,4], "y":[5.4,3]}]}')) with self.assertRaises(jsonschema.exceptions.ValidationError): WCONWorms.load(StringIO(worm_file_text1)) # "this":"" should be disallowed by the schema worm_file_text2 = ( ('{"files":{"this":"", "prev":null, "next":["_1", "_2"]},' '"units":{"t":"s","x":"mm","y":"mm"},"data":[{"id":3, "t":1.3, ' '"x":[3,4], "y":[5.4,3]}]}')) with self.assertRaises(jsonschema.exceptions.ValidationError): WCONWorms.load(StringIO(worm_file_text2))
def test_tracker_commons_and_units(self): with self.assertRaises(ValueError): # The JSON parser shouldn't accept this as valid JSON # "ValueError: Expecting ',' delimiter: line 1 column 25 (char 24)" WCONWorms.load(StringIO('{"lalala":blahblah}')) WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"}, "data":[]}')) # This should fail because "units" is required with self.assertRaises(jsonschema.exceptions.ValidationError): WCONWorms.load(StringIO('{"data":[]}')) # The smallest valid WCON file (Empty data array should be fine) WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"}, "data":[]}')) # Duplicate keys should cause the parser to fail with self.assertRaises(KeyError): WCONWorms.load( StringIO('{"units":{"t":"s","t":"s","x":"mm","y":"mm"},' '"data":[]}'))
def test_origin_offset(self): # ox, without optional bracket w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":1, "t":1.3, "ox":5000, "oy":0,' '"x":[3,4], "y":[5.4,3]}]}')) w2 = WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, ' '"x":[5003,5004], "y":[5.4,3]}]}')) self.assertEqual(w1, w2) # oy, with optional bracket w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":1, "t":1.3, "ox":0, "oy":[4000], ' '"x":[3,4], "y":[5.4,3]}]}')) w2 = WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, ' '"x":[3,4], "y":[4005.4,4003]}]}')) self.assertEqual(w1, w2) # ox and oy, without optional brackets w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":1, "t":1.3, "ox":-500, "oy":4000, ' '"x":[3,4], "y":[5.4,3]}]}')) w2 = WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, ' '"x":[-497,-496], "y":[4005.4,4003]}]}')) self.assertEqual(w1, w2) # ox, with two time points w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox":5000, "oy":0,' '"x":[[3],[4]], "y":[[5.4],[3]]}]}')) w2 = WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], ' '"x":[[5003],[5004]], "y":[[5.4],[3]]}]}')) self.assertEqual(w1, w2)
def test_tracker_commons_and_units(self): with self.assertRaises(ValueError): # The JSON parser shouldn't accept this as valid JSON # "ValueError: Expecting ',' delimiter: line 1 column 25 (char 24)" WCONWorms.load(StringIO('{"lalala":blahblah}')) WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"}, "data":[]}')) # This should fail because "units" is required with self.assertRaises(jsonschema.exceptions.ValidationError): WCONWorms.load(StringIO('{"data":[]}')) # The smallest valid WCON file (Empty data array should be fine) WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"}, "data":[]}')) # Duplicate keys should cause the parser to fail with self.assertRaises(KeyError): WCONWorms.load( StringIO( '{"units":{"t":"s","t":"s","x":"mm","y":"mm"},' '"data":[]}'))
def test_centroid(self): # ox, with two time frames, with centroid w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":"1", "t":[1.3,1.4], "ox":[0,0],' '"oy":[5000,4990], ' '"cx":[10, 11], "cy":[13, 12], "x":[[3, 5],[4, 5]],' '"y":[[5.4, 4],[3, 2]]}]}')) w2 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":"1", "t":[1.3,1.4], "cx":[10, 11],' '"cy":[5013, 5002], ' '"x":[[3, 5],[4, 5]], "y":[[5005.4, 5004],[4993, 4992]]}]}')) self.assertEqual(w1, w2) # oy with no oy: assertionerror with self.assertRaises(AssertionError): w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":"1", "t":[1.3,1.4], "oy":[5000, 5000],' '"cx":[10, 10], "cy":[10, 10], "x":[[3,3,3],[4,4,4.2]], ' '"y":[[5.4, 5.4, 5.5],[3, 3, 7]]}]}')) # units missing for centroid with self.assertRaises(AssertionError): WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":"1", "t":[1.3,1.4], "ox":[5,5], "oy":[0,0],' ' "cx":[10, 10], "cy":[10, 10], "x":[[3],[4]], ' ' "y":[[5.4],[3]]}]}'))
def test_origin_offset(self): # ox, without optional bracket w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":1, "t":1.3, "ox":5000, "oy":0,' '"x":[3,4], "y":[5.4,3]}]}')) w2 = WCONWorms.load(StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, ' '"x":[5003,5004], "y":[5.4,3]}]}')) self.assertEqual(w1, w2) # oy, with optional bracket w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":1, "t":1.3, "ox":0, "oy":[4000], ' '"x":[3,4], "y":[5.4,3]}]}')) w2 = WCONWorms.load(StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, ' '"x":[3,4], "y":[4005.4,4003]}]}')) self.assertEqual(w1, w2) # ox and oy, without optional brackets w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":1, "t":1.3, "ox":-500, "oy":4000, ' '"x":[3,4], "y":[5.4,3]}]}')) w2 = WCONWorms.load(StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, ' '"x":[-497,-496], "y":[4005.4,4003]}]}')) self.assertEqual(w1, w2) # ox, with two time points w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox":5000, "oy":0,' '"x":[[3],[4]], "y":[[5.4],[3]]}]}')) w2 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], ' '"x":[[5003],[5004]], "y":[[5.4],[3]]}]}')) self.assertEqual(w1, w2)
def test_load_from_file(self): """ Test that .load_from_file works identically to .load """ worm_file_text3 = (( '{"units":{"t":"s","x":"mm","y":"mm"},"data":[{"id":3, "t":1.3, ' '"x":[3,4], "y":[5.4,3]}]}')) # STREAM worm_from_stream = WCONWorms.load(StringIO(worm_file_text3)) # FILE with open("test.wcon", 'w') as outfile: outfile.write(worm_file_text3) worm_from_file = WCONWorms.load_from_file("test.wcon") os.remove("test.wcon") # COMPARISON self.assertEqual(worm_from_file, worm_from_stream)
def test_load_from_file(self): """ Test that .load_from_file works identically to .load """ worm_file_text3 = ( ('{"units":{"t":"s","x":"mm","y":"mm"},"data":[{"id":3, "t":1.3, ' '"x":[3,4], "y":[5.4,3]}]}')) # STREAM worm_from_stream = WCONWorms.load(StringIO(worm_file_text3)) # FILE with open("test.wcon", 'w') as outfile: outfile.write(worm_file_text3) worm_from_file = WCONWorms.load_from_file("test.wcon") os.remove("test.wcon") # COMPARISON self.assertEqual(worm_from_file, worm_from_stream)
def test_metadata(self): WCON_string1 = \ """ { "metadata":{ "lab":{"location":"CRB, room 5020", "name":"Behavioural Genomics" }, "who":"Firstname Lastname", "timestamp":"2012-04-23T18:25:43.511Z", "temperature":23.8, "humidity":40.3, "dish":{ "type":"petri", "size":35, "units":"mm" }, "food":"none", "media":"agarose", "sex":"hermaphrodite", "stage":"adult", "age":66.8, "strain":"CB4856", "image_orientation":"imaged onto agar", "protocol":"text description of protocol", "software":{ "tracker":{"name":"Software Name", "version":"1.3.0"}, "featureID":"@OMG" }, "settings": "Any valid string with hardware and software configuration details" }, "units":{"t":"s", "x":"mm", "y":"mm", "humidity":"%", "temperature":"C", "age":"h"}, "data":[ { "id":1, "t":1.3, "x":[7.2, 5], "y":[0.5, 0.86] } ] } """ w1 = WCONWorms.load(StringIO(WCON_string1)) self.assertEqual(w1.metadata["strain"], "CB4856") self.assertEqual(w1.metadata["dish"]["size"], 35)
def test_origin_offset(self): # ox and oy w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":"1", "t":[1.3], "ox":[-500], "oy":[4000], ' '"x":[[3,4]], "y":[[5.4,3]]}]}')) w2 = WCONWorms.load(StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":"1", "t":[1.3], ' '"x":[[-497,-496]],' '"y":[[4005.4,4003]]}]}')) self.assertEqual(w1, w2) # ox with two time points w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":"1", "t":[1.3,1.4], "ox":[5000, 5000],' '"oy":[0, 0],' '"x":[[3],[4]], "y":[[5.4],[3]]}]}')) w2 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":"1", "t":[1.3,1.4], ' '"x":[[5003],[5004]], "y":[[5.4],[3]]}]}')) self.assertEqual(w1, w2) # ox specified not for each time frame should as of version 1.2 # raise an error with self.assertRaises(jsonschema.exceptions.ValidationError): w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":"1", "t":[1.3,1.4], "ox":5000, "oy":0,' '"x":[[3],[4]], "y":[[5.4],[3]]}]}')) # Try the same but wrap our single origin points in brackets # (should still fail) with self.assertRaises(AssertionError): w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":"1", "t":[1.3,1.4], "ox":[5000], "oy":[0],' '"x":[[3],[4]], "y":[[5.4],[3]]}]}'))
def test_data2(self): WCON_string = \ """ { "units":{"t":"s", "x":"mm", "y":"mm"}, "data":[{ "id":2, "t":1.4, "x":[125.11, 126.14, 117.12], "y":[23.3, 22.23, 21135.08] }, { "id":1, "t":1.4, "x":[1215.11, 1216.14, 1217.12], "y":[234.89, 265.23, 235.08] }, { "id":2, "t":1.5, "x":[1215.11, 1216.14, 1217.12], "y":[234.89, 265.23, 235.08] }, { "id":1, "t":[1.3,1.5], "x":[[1,1,1],[1215.11, 1216.14, 1217.12]], "y":[[2,2,2],[234.89, 265.23, 235.08]] } ] } """ self._validate_from_schema(WCON_string) WCONWorms.load(StringIO(WCON_string)) # Test that extra features are ignored WCON_string1 = \ """ { "units":{"t":"s", "x":"mm", "y":"mm"}, "data":[{ "id":2, "t":1.4, "x":[125.11, 126.14, 117.12], "y":[23.3, 22.23, 21135.08] }, { "id":1, "t":1.4, "x":[1215.11, 1216.14, 1217.12], "y":[234.89, 265.23, 235.08] }, { "id":2, "t":1.5, "x":[1215.11, 1216.14, 1217.12], "y":[234.89, 265.23, 235.08], "ignorethat":"yes" }, { "id":1, "t":[1.3,1.5], "ignorethis": 12, "x":[[1,1,1],[1215.11, 1216.14, 1217.12]], "y":[[2,2,2],[234.89, 265.23, 235.08]] } ] } """ self._validate_from_schema(WCON_string1) w = WCONWorms.load(StringIO(WCON_string1)) # TODO: test that "ignorethis" and "ignorethat" are not present in w # order permuted from previous example WCON_string2 = \ """ { "units":{"t":"s", "x":"mm", "y":"mm"}, "data":[{ "id":1, "t":[1.3,1.5], "x":[[1,1,1],[1215.11, 1216.14, 1217.12]], "y":[[2,2,2],[234.89, 265.23, 235.08]] }, { "id":2, "t":1.4, "x":[125.11, 126.14, 117.12], "y":[23.3, 22.23, 21135.08] }, { "id":1, "t":1.4, "x":[1215.11, 1216.14, 1217.12], "y":[234.89, 265.23, 235.08] }, { "id":2, "t":1.5, "x":[1215.11, 1216.14, 1217.12], "y":[234.89, 265.23, 235.08] } ] } """ self._validate_from_schema(WCON_string2) WCONWorms.load(StringIO(WCON_string2)) # Origin values WCON_string3 = \ """ { "units":{"t":"s", "x":"mm", "y":"mm", "ox":"mm", "oy":"mm"}, "data":[{ "id":1, "t":[1.3,1.5], "x":[[1,1,1],[1215.11, 1216.14, 1217.12]], "y":[[2,2,2],[234.89, 265.23, 235.08]], "ox":5000, "oy":0 }, { "id":2, "t":1.4, "x":[125.11, 126.14, 117.12], "y":[23.3, 22.23, 21135.08] }, { "id":1, "t":1.4, "x":[1215.11, 1216.14, 1217.12], "y":[234.89, 265.23, 235.08]}, { "id":2, "t":1.5, "x":[1215.11, 1216.14, 1217.12], "y":[234.89, 265.23, 235.08] } ] } """ self._validate_from_schema(WCON_string3) WCONWorms.load(StringIO(WCON_string3))
def test_arrayed_software(self): WCONWorms.load( StringIO('{ "units":{"t":"s", "x":"mm", "y":"mm"}, ' '"data":[], "metadata":{' '"software":[{"name":"a"}, ' '{"name":"b"}]} }'))
def test_centroid(self): # ox, with two time frames, with centroid w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox":0, "oy":5000, "cx":10, ' '"cy":10, "x":[[3],[4]], "y":[[5.4],[3]]}]}')) w2 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "cx":10, "cy":5010, ' '"x":[[-7],[-6]], "y":[[-4.6],[-7]]}]}')) self.assertEqual(w1, w2) # oy with no oxy: assertionerror with self.assertRaises(AssertionError): w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "oy":5000,' '"cx":10, "cy":10, "x":[[3, 3, 3],[4, 4, 4.2]], ' '"y":[[5.4, 5.4, 5.5],[3, 3, 7]]}]}')) # ox, with two time frames, three articulation points, with centroid w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox":0, "oy":5000, "cx":10, ' '"cy":10, "x":[[3, 3, 3],[4, 4, 4.2]], ' '"y":[[5.4, 5.4, 5.5],[3, 3, 7]]}]}')) w2 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "cx":10, "cy":5010, ' '"x":[[-7,-7,-7],[-6,-6,-5.8]], ' '"y":[[-4.6,-4.6,-4.5],[-7,-7,-3]]}]}')) self.assertEqual(w1, w2) # ox and centroid, different in different time frames w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox": 0, ' '"oy":[5000, 1000], "cx":10, ' '"cy":10, "x":[[3],[4]], "y":[[5.4],[3]]}]}')) w2 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "cx":10, "cy":[5010, 1010], ' '"x":[[-7],[-6]], "y":[[-4.6],[-7.0]]}]}')) self.assertEqual(w1, w2) # ox and centroid, different in different time frames w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox":0, "oy":[5000, 1000],' '"cx":10, "cy":10, "x":[[3],[4]], "y":[[5.4],[3]]}]}')) w2 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "cx":10, "cy":[5010, 1010], ' '"x":[[-7],[-6]], "y":[[-4.6],[-7]]}]}')) self.assertEqual(w1, w2) # units missing for centroid with self.assertRaises(AssertionError): WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox":5000, "oy":0,' ' "cx":10, "cy":10, "x":[[3],[4]], ' ' "y":[[5.4],[3]]}]}'))
def test_data1(self): # Single-valued 't' subelement should be fine WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":3, "t":1.3, ' '"x":[3,4], "y":[5.4,3]}]}')) # Two values for 'x' but four values for 'y': error with self.assertRaises(AssertionError): WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":3, "t":1.3, ' '"x":[3,4], ' '"y":[5.4,3,1,-3]}]}')) # Two values for 't' but 3 for 'x' and 'y': error with self.assertRaises(AssertionError): WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":3, "t":[1.3,8], ' '"x":[3,4,5], "y":[5.4,3,5]}]}')) # Duplicated segments are OK if there are no differences WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, "x":[3,4], "y":[5.4,3]},' '{"id":1, "t":1.3, "x":[3,4], "y":[5.4,3]}]}')) # Duplicated time indices are OK if the earlier entry was for a # different worm WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":2, "t":1.3, "x":[3,4], "y":[5.4,3]},' '{"id":1, "t":1.3, "x":[3,4], "y":[5.4,3]}]}')) # Duplicated time indices are OK if the earlier entry had those entries # missing WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, "x":[null,null], "y":[null,null]},' '{"id":1, "t":1.3, "x":[3,4], "y":[5.4,3]}]}')) # Error if data from a later segment conflicts with earlier segments with self.assertRaises(AssertionError): WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, "x":[3,4], "y":[5.4,3]},' '{"id":1, "t":1.3, "x":[3,4], "y":[5.5,3]}]}'))
def test_data1(self): # Single-valued 't' subelement should be fine WCONWorms.load(StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":3, "t":1.3, ' '"x":[3,4], "y":[5.4,3]}]}')) # Two values for 'x' but four values for 'y': error with self.assertRaises(AssertionError): WCONWorms.load(StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":3, "t":1.3, ' '"x":[3,4], ' '"y":[5.4,3,1,-3]}]}')) # Two values for 't' but 3 for 'x' and 'y': error with self.assertRaises(AssertionError): WCONWorms.load(StringIO('{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":3, "t":[1.3,8], ' '"x":[3,4,5], "y":[5.4,3,5]}]}')) # Duplicated segments are OK if there are no differences WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, "x":[3,4], "y":[5.4,3]},' '{"id":1, "t":1.3, "x":[3,4], "y":[5.4,3]}]}')) # Duplicated time indices are OK if the earlier entry was for a # different worm WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":2, "t":1.3, "x":[3,4], "y":[5.4,3]},' '{"id":1, "t":1.3, "x":[3,4], "y":[5.4,3]}]}')) # Duplicated time indices are OK if the earlier entry had those entries # missing WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, "x":[null,null], "y":[null,null]},' '{"id":1, "t":1.3, "x":[3,4], "y":[5.4,3]}]}')) # Error if data from a later segment conflicts with earlier segments with self.assertRaises(AssertionError): WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm"},' '"data":[{"id":1, "t":1.3, "x":[3,4], "y":[5.4,3]},' '{"id":1, "t":1.3, "x":[3,4], "y":[5.5,3]}]}'))
def test_centroid(self): # ox, with two time frames, with centroid w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox":0, "oy":5000, "cx":10, ' '"cy":10, "x":[[3],[4]], "y":[[5.4],[3]]}]}')) w2 = WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "cx":10, "cy":5010, ' '"x":[[-7],[-6]], "y":[[-4.6],[-7]]}]}')) self.assertEqual(w1, w2) # oy with no oxy: assertionerror with self.assertRaises(AssertionError): w1 = WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "oy":5000,' '"cx":10, "cy":10, "x":[[3, 3, 3],[4, 4, 4.2]], ' '"y":[[5.4, 5.4, 5.5],[3, 3, 7]]}]}')) # ox, with two time frames, three articulation points, with centroid w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox":0, "oy":5000, "cx":10, ' '"cy":10, "x":[[3, 3, 3],[4, 4, 4.2]], ' '"y":[[5.4, 5.4, 5.5],[3, 3, 7]]}]}')) w2 = WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "cx":10, "cy":5010, ' '"x":[[-7,-7,-7],[-6,-6,-5.8]], ' '"y":[[-4.6,-4.6,-4.5],[-7,-7,-3]]}]}')) self.assertEqual(w1, w2) # ox and centroid, different in different time frames w1 = WCONWorms.load( StringIO('{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox": 0, ' '"oy":[5000, 1000], "cx":10, ' '"cy":10, "x":[[3],[4]], "y":[[5.4],[3]]}]}')) w2 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "cx":10, "cy":[5010, 1010], ' '"x":[[-7],[-6]], "y":[[-4.6],[-7.0]]}]}')) self.assertEqual(w1, w2) # ox and centroid, different in different time frames w1 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox":0, "oy":[5000, 1000],' '"cx":10, "cy":10, "x":[[3],[4]], "y":[[5.4],[3]]}]}')) w2 = WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm",' '"cx":"mm","cy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "cx":10, "cy":[5010, 1010], ' '"x":[[-7],[-6]], "y":[[-4.6],[-7]]}]}')) self.assertEqual(w1, w2) # units missing for centroid with self.assertRaises(AssertionError): WCONWorms.load( StringIO( '{"units":{"t":"s","x":"mm","y":"mm","ox":"mm","oy":"mm"},' '"data":[{"id":1, "t":[1.3,1.4], "ox":5000, "oy":0,' ' "cx":10, "cy":10, "x":[[3],[4]], ' ' "y":[[5.4],[3]]}]}'))
def test_arrayed_software(self): WCONWorms.load(StringIO('{ "units":{"t":"s", "x":"mm", "y":"mm"}, ' '"data":[], "metadata":{' '"software":[{"name":"a"}, ' '{"name":"b"}]} }'))
# warnings.simplefilter(action = "ignore", category = RuntimeWarning) if __name__ == '__main__': JSON_path = '../../tests/minimax.wcon' w2 = WCONWorms.load_from_file(JSON_path) w2.save_to_file('example_saved_file.WCON', pretty_print=True) w3 = WCONWorms.load_from_file('example_saved_file.WCON') u = MeasurementUnit.create('cm') # io=StringIO() # json.dump([None], io) # io.getvalue() if __name__ == '__main__2': worm_file_text2 = (('{"units":{"t":"s","x":"m","y":"m"},' '"data":[{"id":3, "t":1.3, ' '"x":[3,4], "y":[5.4,3]}]}')) w1 = WCONWorms.load(StringIO(worm_file_text2)) if __name__ == '__main__3': w1 = WCONWorms.load( StringIO('{"units":{},' '"data":[{"id":3, "t":1.3, ' '"x":[3,4,4,3.2], ' '"y":[5.4,3,1,-3]}]}'))
if __name__ == '__main__': JSON_path = '../../tests/minimax.wcon' w2 = WCONWorms.load_from_file(JSON_path) w2.save_to_file('example_saved_file.WCON', pretty_print=True) w3 = WCONWorms.load_from_file('example_saved_file.WCON') u = MeasurementUnit.create('cm') # io=StringIO() # json.dump([None], io) # io.getvalue() if __name__ == '__main__2': worm_file_text2 = (('{"units":{"t":"s","x":"m","y":"m"},' '"data":[{"id":3, "t":1.3, ' '"x":[3,4], "y":[5.4,3]}]}')) w1 = WCONWorms.load(StringIO(worm_file_text2)) if __name__ == '__main__3': w1 = WCONWorms.load(StringIO('{"units":{},' '"data":[{"id":3, "t":1.3, ' '"x":[3,4,4,3.2], ' '"y":[5.4,3,1,-3]}]}'))