Exemplo n.º 1
0
 def test_flatten_with_array(self):
     sr = SidecarMap()
     sidecar1 = {"a": {"c": ["blech3", "blech3a"], "d": "blech4", "e": "blech5"}, "b": "blech2"}
     try:
         sr.flatten(sidecar1)
     except HedFileError:
         pass
     except Exception:
         self.fail('process threw the wrong exception when array included in JSON')
     else:
         self.fail('process should have thrown a HedFileError when array included in JSON')
Exemplo n.º 2
0
    def test_flatten_sidecar_with_empty(self):
        sr = SidecarMap()
        # top level value is empty
        sidecar1 = {"a": {"c": "blech3", "d": "blech4", "e": "blech5"}, "b": "",
                    "af": {"c": {"af": "blech6"}, "df_new": "blech7"}}
        df1 = sr.flatten(sidecar1)
        self.assertEqual(12, len(df1), "flatten should return a dataframe with correct number of rows")
        self.assertEqual(2, len(df1.columns), "flatten should return a dataframe with 2 columns")

        # other value is empty
        sidecar2 = {"a": {"c": "blech3", "d": "blech4", "e": "blech5"}, "b": "blech2",
                    "af": {"c": {"af": "blech6"}, "df_new": ""}}
        df2 = sr.flatten(sidecar2)
        self.assertEqual(12, len(df2), "flatten should return a dataframe with correct number of rows")
        self.assertEqual(2, len(df2.columns), "flatten should return a dataframe with 2 columns")
Exemplo n.º 3
0
def sidecar_flatten(json_sidecar):
    """Converts a sidecar from long to short unless unless the command is not COMMAND_TO_LONG then converts to short

    Parameters
    ----------
    json_sidecar: Sidecar
        Previously created Sidecar

    Returns
    -------
    dict
        A downloadable dictionary file or a file containing warnings
    """

    json_string = json_sidecar.get_as_json_string()
    sidecar = json.loads(json_string)
    sr = SidecarMap()
    df = sr.flatten(sidecar)
    data = df.to_csv(None, sep='\t', index=False, header=True)
    display_name = json_sidecar.name
    file_name = generate_filename(display_name,
                                  name_suffix='flattened',
                                  extension='.tsv')
    return {
        base_constants.COMMAND: base_constants.COMMAND_FLATTEN,
        'data': data,
        'output_display_name': file_name,
        'msg_category': 'success',
        'msg': f'JSON sidecar {display_name} was successfully flattened'
    }
Exemplo n.º 4
0
 def test_unflatten(self):
     sr = SidecarMap()
     sidecar1 = {"a": "blech1", "d": "blech4", "e": "blech5"}
     df1 = sr.flatten(sidecar1)
     unflat1 = sr.unflatten(df1)
     self.assertEqual(sidecar1, unflat1, "unflatten should unflatten when sidecar unnested")
     sidecar2 = {"a": {"c1": "blech3", "c2": "blech3a"}, "d": "apple"}
     df2 = sr.flatten(sidecar2)
     unflat2 = sr.unflatten(df2)
     self.assertEqual(sidecar2, unflat2, "unflatten should unflatten when sidecar has single dictionary")
     sidecar3 = {"b": "banana", "a": {"c1": "blech3", "c2": "blech3a"}, "d": "apple"}
     df3 = sr.flatten(sidecar3)
     unflat3 = sr.unflatten(df3)
     self.assertEqual(sidecar3, unflat3, "unflatten should unflatten when sidecar has embedded dictionary")
     sidecar4 = {"a": {"c": {"c1": "blech3", "c2": "blech3a"}, "d": "blech4", "e": "blech5"}, "b": "blech2"}
     df4 = sr.flatten(sidecar4)
     unflat4 = sr.unflatten(df4)
     self.assertEqual(sidecar4, unflat4, "unflatten should unflatten when sidecar has nested dictionaries")
Exemplo n.º 5
0
 def test_flatten(self):
     sr = SidecarMap()
     sidecar1 = {"a": {"c": {"c1": "blech3", "c2": "blech3a"}, "d": "blech4", "e": "blech5"}, "b": "blech2"}
     df1 = sr.flatten(sidecar1)
     self.assertEqual(9, len(df1), "flatten should return a dataframe with correct number of rows")
     self.assertEqual(2, len(df1.columns), "flatten should return a dataframe with 2 columns")