def test_basic(dims, lengths): in_unit = sc.units.m in_dtype = sc.dtype.float32 var = sc.Variable(dims, unit=in_unit, dtype=in_dtype, values=np.random.rand(*lengths), variances=np.random.rand(*lengths)) html = BeautifulSoup(make_html(var), features="html.parser") # Variable's "name" actually contains Dims and their extents name = html.find_all(class_=VAR_NAME_CSS_CLASS) assert len(name) == 1 assert_dims(dims, name[0].text) assert_lengths(lengths, name[0].text) assert_unit(in_unit, html.find_all(class_=UNIT_CSS_CLASS)) value = html.find_all(class_=VALUE_CSS_CLASS) assert len(value) == 1 values, variances = [child.text for child in value[0].children] assert "..." in values assert VARIANCE_PREFIX in variances
def test_events_does_not_repeat_dense_coords(): events = sc.Variable(['y', 'z'], shape=(3, sc.Dimensions.Events)) events.values[0].extend(np.arange(0)) events.values[2].extend(np.arange(0)) events.values[1].extend(np.arange(0)) d = sc.Dataset() d['a'] = sc.Variable(['y', 'x', 'z'], shape=(3, 2, 4), variances=True) d['b'] = sc.DataArray( events, coords={ 'y': sc.Variable(['y'], values=np.arange(4)), 'z': sc.Variable(['z'], shape=(sc.Dimensions.Events, )), "binedge": sc.Variable(['y'], values=np.random.rand(4)) }, attrs={"attr": sc.Variable(['y'], values=np.random.rand(3))}) html = BeautifulSoup(make_html(d), features="html.parser") sections = html.find_all(class_="xr-section-summary") assert ["Coordinates" in section.text for section in sections].count(True) == 2 attr_section = next(section for section in sections if "Attributes" in section.text) # check that this section is a subsection assert "sc-subsection" in attr_section.parent.attrs["class"] variables = html.find_all(class_="xr-var-name") # check that each dim is only present once assert ["z" in var.text for var in variables].count(True) == 1 assert ["y" in var.text for var in variables].count(True) == 1
def test_basic(dims, lengths): in_unit = sc.units.m in_dtype = sc.dtype.float32 data = sc.Variable(dims, unit=in_unit, dtype=in_dtype, values=np.random.rand(*lengths), variances=np.random.rand(*lengths)) data_name = "testdata" bin_edge_data_name = "testdata-binedges" lengths[0] += 1 bin_edges = sc.Variable(dims, unit=in_unit, dtype=in_dtype, values=np.random.rand(*lengths), variances=np.random.rand(*lengths)) dataset = sc.Dataset({ data_name: data, bin_edge_data_name: bin_edges }, coords={ dims[0]: bin_edges, LABEL_NAME: bin_edges, }) dataset[data_name].coords["attr"] = data dataset[data_name].masks["mask"] = bin_edges html = BeautifulSoup(make_html(dataset), features="html.parser") sections = html.find_all(class_="xr-section-item") expected_sections = [ "Dimensions", "Coordinates", "Data", "Masks", "Coordinates (unaligned)" ] assert len(sections) == 5 for actual_section, expected_section in zip(sections, expected_sections): assert expected_section in actual_section.text attr_section = sections.pop(len(sections) - 1) assert_section(attr_section, ATTR_NAME, dims, in_dtype, in_unit) data_section = sections.pop(2) assert_section(data_section, [data_name, bin_edge_data_name], dims, in_dtype, in_unit, has_bin_edges=[True, False]) dim_section = sections.pop(0) assert_dims_section(data, dim_section) data_names = [LABEL_NAME, MASK_NAME] for section, name in zip(sections, data_names): assert_section(section, name, dims, in_dtype, in_unit, has_bin_edges=True)
def test_bin_edge_and_events(dims, lengths): in_unit = sc.units.m in_dtype = sc.dtype.event_list_float32 data = sc.Variable(dims=dims, shape=lengths, unit=in_unit, dtype=in_dtype) # attribute data without the events dimension non_events_data = sc.Variable(dims=dims, shape=lengths, unit=in_unit, dtype=in_dtype) # makes the first dimension be bin-edges lengths[0] += 1 non_events_bin_edges = sc.Variable(dims=dims, shape=lengths, unit=in_unit, dtype=in_dtype) data_array = sc.DataArray(data, coords={ dims[0]: non_events_bin_edges, LABEL_NAME: non_events_bin_edges }, attrs={ATTR_NAME: non_events_data}, masks={MASK_NAME: non_events_bin_edges}) html = BeautifulSoup(make_html(data_array), features="html.parser") sections = html.find_all(class_="xr-section-item") expected_sections = [ "Dimensions", "Coordinates", "Data", "Masks", "Attributes" ] assert len(sections) == 5 for actual_section, expected_section in zip(sections, expected_sections): assert expected_section in actual_section.text attr_section = sections.pop(len(sections) - 1) assert_section(attr_section, ATTR_NAME, dims, in_dtype, in_unit) data_section = sections.pop(2) assert_section(data_section, "", dims, in_dtype, in_unit, has_events=True) dim_section = sections.pop(0) assert_dims_section(data, dim_section) data_names = [dims[0], MASK_NAME] for section, name in zip(sections, data_names): assert_section(section, name, dims, in_dtype, in_unit, has_bin_edges=True)
def test_empty_events_1d_variable(): in_dtype = sc.dtype.event_list_float32 in_unit = sc.units.K var = sc.Variable([], [], unit=in_unit, dtype=in_dtype) html = BeautifulSoup(make_html(var), features="html.parser") assert_common(html, in_dtype) name = html.find_all(class_=VAR_NAME_CSS_CLASS) assert len(name) == 1 assert_dims([], '') assert_unit(in_unit, html.find_all(class_=UNIT_CSS_CLASS)) value = html.find_all(class_=VALUE_CSS_CLASS) assert len(value) == 1
def test_bin_edge(dims, lengths): in_unit = sc.units.m in_dtype = sc.dtype.float32 data = sc.Variable(dims, unit=in_unit, dtype=in_dtype, values=np.random.rand(*lengths), variances=np.random.rand(*lengths)) # makes the first dimension be bin-edges lengths[-1] += 1 edges = sc.Variable(dims, unit=in_unit, dtype=in_dtype, values=np.random.rand(*lengths)) data_array = sc.DataArray(data, coords={ dims[-1]: edges, LABEL_NAME: edges }, unaligned_coords={ATTR_NAME: data}, masks={MASK_NAME: data}) html = BeautifulSoup(make_html(data_array), features="html.parser") sections = html.find_all(class_="xr-section-item") assert len(sections) == 5 expected_sections = [ "Dimensions", "Coordinates", "Data", "Masks", "Coordinates (unaligned)" ] for actual_section, expected_section in zip(sections, expected_sections): assert expected_section in actual_section.text attr_section = sections.pop(len(sections) - 1) assert_section(attr_section, ATTR_NAME, dims, in_dtype, in_unit) data_section = sections.pop(2) assert_section(data_section, "", dims, in_dtype, in_unit) dim_section = sections.pop(0) assert_dims_section(data, dim_section) data_names = [LABEL_NAME, MASK_NAME] for section, name in zip(sections, data_names): assert_section(section, name, dims, in_dtype, in_unit, has_bin_edges=name == LABEL_NAME)
def test_events(dims, lengths): in_unit = sc.units.m in_dtype = sc.dtype.float32 data = sc.Variable(dims, lengths, unit=in_unit, dtype=in_dtype) in_attr_dims = [dims[0]] attr = sc.Variable(in_attr_dims, unit=in_unit, dtype=in_dtype, values=np.random.rand(lengths[0])) data_array = sc.DataArray(data, coords={ dims[0]: data, LABEL_NAME: attr }, unaligned_coords={ATTR_NAME: attr}, masks={MASK_NAME: attr}) html = BeautifulSoup(make_html(data_array), features="html.parser") sections = html.find_all(class_="xr-section-item") assert len(sections) == 5 expected_sections = [ "Dimensions", "Coordinates", "Data", "Masks", "Attributes" ] for actual_section, expected_section in zip(sections, expected_sections): assert expected_section in actual_section.text data_section = sections.pop(2) assert_section(data_section, "", dims, in_dtype, in_unit, has_events=True) dim_section = sections.pop(0) assert_dims_section(data, dim_section) coord_section = sections.pop(0) # the original dim used as a label (dim[0]) is not shown, # instead the events dim is shown assert_section(coord_section, LABEL_NAME, dims, in_dtype, in_unit, has_events=True) data_names = [MASK_NAME, ATTR_NAME] for section, name in zip(sections, data_names): assert_section(section, name, in_attr_dims, in_dtype, in_unit)
def test_empty_sparse_1d_variable(): in_dtype = sc.dtype.float32 in_unit = sc.units.K var = sc.Variable([Dim.X], [sc.Dimensions.Sparse], unit=in_unit, dtype=in_dtype) html = BeautifulSoup(make_html(var), features="html.parser") assert_common(html, in_dtype) name = html.find_all(class_=VAR_NAME_CSS_CLASS) assert len(name) == 1 assert_dims([Dim.X], name[0].text, has_sparse=True) assert_unit(in_unit, html.find_all(class_=UNIT_CSS_CLASS)) value = html.find_all(class_=VALUE_CSS_CLASS) assert len(value) == 1
def test_events(dims, lengths): in_dtype = sc.dtype.event_list_float32 in_unit = sc.units.deg var = sc.Variable(dims, lengths, unit=in_unit, dtype=in_dtype) length = 10 var.values[0].extend(np.arange(length)) html = BeautifulSoup(make_html(var), features="html.parser") assert_common(html, in_dtype) name = html.find_all(class_=VAR_NAME_CSS_CLASS) assert len(name) == 1 assert_dims(dims, name[0].text) assert_unit(in_unit, html.find_all(class_=UNIT_CSS_CLASS)) value = html.find_all(class_=VALUE_CSS_CLASS) assert len(value) == 1 assert f"len={length}" in value[0].text
def test_events_1d_variable(): in_dtype = sc.dtype.event_list_float32 in_unit = sc.units.deg var = sc.Variable([], [], unit=in_unit, dtype=in_dtype) length = 10 var.values.extend(np.arange(length)) html = BeautifulSoup(make_html(var), features="html.parser") assert_common(html, in_dtype) name = html.find_all(class_=VAR_NAME_CSS_CLASS) assert len(name) == 1 assert_dims([], '') # This checks if the 'size' of the Events dim is being added # which would add a useless 'None' assert "None" not in name[0].text assert_unit(in_unit, html.find_all(class_=UNIT_CSS_CLASS)) value = html.find_all(class_=VALUE_CSS_CLASS) assert len(value) == 1
def test_basic(dims, lengths): in_unit = sc.units.m in_dtype = sc.dtype.float32 data = sc.Variable(dims, unit=in_unit, dtype=in_dtype, values=np.random.rand(*lengths), variances=np.random.rand(*lengths)) data_array = sc.DataArray(data, coords={ dims[0]: data, LABEL_NAME: data }, unaligned_coords={ATTR_NAME: data}, masks={MASK_NAME: data}) html = BeautifulSoup(make_html(data_array), features="html.parser") sections = html.find_all(class_="xr-section-item") assert len(sections) == 5 expected_sections = [ "Dimensions", "Coordinates", "Data", "Masks", "Coordinates (unaligned)" ] for actual_section, expected_section in zip(sections, expected_sections): assert expected_section in actual_section.text dim_section = sections.pop(0) assert_dims_section(data, dim_section) data_names = [ LABEL_NAME, "", # dataarray does not have a data name MASK_NAME, ATTR_NAME ] assert len(sections) == len( data_names), "Sections and expected data names do not match" for section, name in zip(sections, data_names): assert_section(section, name, dims, in_dtype, in_unit)
def test_data_not_elided(): dims = ['x'] lengths = (3, ) in_unit = sc.units.m in_dtype = sc.dtype.float32 var = sc.Variable(dims, unit=in_unit, values=np.random.rand(*lengths), dtype=in_dtype) html = BeautifulSoup(make_html(var), features="html.parser") assert_common(html, in_dtype) name = html.find_all(class_=VAR_NAME_CSS_CLASS) assert len(name) == 1 assert_dims(dims, name[0].text) assert_lengths(lengths, name[0].text) assert_unit(in_unit, html.find_all(class_=UNIT_CSS_CLASS)) value = html.find_all(class_=VALUE_CSS_CLASS) assert len(value) == 1 assert "..." not in value[0].text
def test_sparse_does_not_repeat_dense_coords(): sparse = sc.Variable([Dim.Y, Dim.Z], shape=(3, sc.Dimensions.Sparse)) sparse.values[0].extend(np.arange(0)) sparse.values[2].extend(np.arange(0)) sparse.values[1].extend(np.arange(0)) d = sc.Dataset() d['a'] = sc.Variable([Dim.Y, Dim.X, Dim.Z], shape=(3, 2, 4), variances=True) d['b'] = sc.DataArray( sparse, coords={ Dim.Y: sc.Variable([Dim.Y], values=np.arange(4)), Dim.Z: sc.Variable([Dim.Z], shape=(sc.Dimensions.Sparse, )), }, labels={"binedge": sc.Variable([Dim.Y], values=np.random.rand(4))}, attrs={"attr": sc.Variable([Dim.Y], values=np.random.rand(3))}) html = BeautifulSoup(make_html(d), features="html.parser") sections = html.find_all(class_="xr-section-summary") assert ["Coordinates" in section.text for section in sections].count(True) == 2 attr_section = next(section for section in sections if "Attributes" in section.text) # check that this section is a subsection assert "sc-subsection" in attr_section.parent.attrs["class"] variables = html.find_all(class_="xr-var-name") # check that each dim is only present once assert ["Dim.Z" in var.text for var in variables].count(True) == 1 assert ["Dim.Y" in var.text for var in variables].count(True) == 1