@@ -4307,3 +4307,202 @@ def test_optional_arguments(self):
43074307 # This test does not work for ilr interpolation
43084308 print (len (fig .data ))
43094309 assert len (fig .data ) == ncontours + 2 + arg_set ["showscale" ]
4310+
4311+
4312+ class TestHexbinMapbox (NumpyTestUtilsMixin , TestCaseNoTemplate ):
4313+ def test_aggregation (self ):
4314+
4315+ lat = [0 , 1 , 1 , 2 , 4 , 5 , 1 , 2 , 4 , 5 , 2 , 3 , 2 , 1 , 5 , 3 , 5 ]
4316+ lon = [1 , 2 , 3 , 3 , 0 , 4 , 5 , 0 , 5 , 3 , 1 , 5 , 4 , 0 , 1 , 2 , 5 ]
4317+ color = np .ones (len (lat ))
4318+
4319+ fig1 = ff .create_hexbin_mapbox (lat = lat , lon = lon , nx_hexagon = 1 )
4320+
4321+ actual_geojson = {
4322+ "type" : "FeatureCollection" ,
4323+ "features" : [
4324+ {
4325+ "type" : "Feature" ,
4326+ "id" : "-8.726646259971648e-11,-0.031886255679892235" ,
4327+ "geometry" : {
4328+ "type" : "Polygon" ,
4329+ "coordinates" : [
4330+ [
4331+ [- 5e-09 , - 4.7083909316316985 ],
4332+ [2.4999999999999996 , - 3.268549270944215 ],
4333+ [2.4999999999999996 , - 0.38356933397072673 ],
4334+ [- 5e-09 , 1.0597430482129082 ],
4335+ [- 2.50000001 , - 0.38356933397072673 ],
4336+ [- 2.50000001 , - 3.268549270944215 ],
4337+ [- 5e-09 , - 4.7083909316316985 ],
4338+ ]
4339+ ],
4340+ },
4341+ },
4342+ {
4343+ "type" : "Feature" ,
4344+ "id" : "-8.726646259971648e-11,0.1192636916419258" ,
4345+ "geometry" : {
4346+ "type" : "Polygon" ,
4347+ "coordinates" : [
4348+ [
4349+ [- 5e-09 , 3.9434377827164666 ],
4350+ [2.4999999999999996 , 5.381998306154031 ],
4351+ [2.4999999999999996 , 8.248045720432454 ],
4352+ [- 5e-09 , 9.673766164509932 ],
4353+ [- 2.50000001 , 8.248045720432454 ],
4354+ [- 2.50000001 , 5.381998306154031 ],
4355+ [- 5e-09 , 3.9434377827164666 ],
4356+ ]
4357+ ],
4358+ },
4359+ },
4360+ {
4361+ "type" : "Feature" ,
4362+ "id" : "0.08726646268698293,-0.031886255679892235" ,
4363+ "geometry" : {
4364+ "type" : "Polygon" ,
4365+ "coordinates" : [
4366+ [
4367+ [5.0000000049999995 , - 4.7083909316316985 ],
4368+ [7.500000009999999 , - 3.268549270944215 ],
4369+ [7.500000009999999 , - 0.38356933397072673 ],
4370+ [5.0000000049999995 , 1.0597430482129082 ],
4371+ [2.5 , - 0.38356933397072673 ],
4372+ [2.5 , - 3.268549270944215 ],
4373+ [5.0000000049999995 , - 4.7083909316316985 ],
4374+ ]
4375+ ],
4376+ },
4377+ },
4378+ {
4379+ "type" : "Feature" ,
4380+ "id" : "0.08726646268698293,0.1192636916419258" ,
4381+ "geometry" : {
4382+ "type" : "Polygon" ,
4383+ "coordinates" : [
4384+ [
4385+ [5.0000000049999995 , 3.9434377827164666 ],
4386+ [7.500000009999999 , 5.381998306154031 ],
4387+ [7.500000009999999 , 8.248045720432454 ],
4388+ [5.0000000049999995 , 9.673766164509932 ],
4389+ [2.5 , 8.248045720432454 ],
4390+ [2.5 , 5.381998306154031 ],
4391+ [5.0000000049999995 , 3.9434377827164666 ],
4392+ ]
4393+ ],
4394+ },
4395+ },
4396+ {
4397+ "type" : "Feature" ,
4398+ "id" : "0.04363323129985823,0.04368871798101678" ,
4399+ "geometry" : {
4400+ "type" : "Polygon" ,
4401+ "coordinates" : [
4402+ [
4403+ [2.4999999999999996 , - 0.38356933397072673 ],
4404+ [5.0000000049999995 , 1.0597430482129082 ],
4405+ [5.0000000049999995 , 3.9434377827164666 ],
4406+ [2.4999999999999996 , 5.381998306154031 ],
4407+ [- 5.0000001310894304e-09 , 3.9434377827164666 ],
4408+ [- 5.0000001310894304e-09 , 1.0597430482129082 ],
4409+ [2.4999999999999996 , - 0.38356933397072673 ],
4410+ ]
4411+ ],
4412+ },
4413+ },
4414+ ],
4415+ }
4416+
4417+ actual_agg = [2.0 , 2.0 , 1.0 , 3.0 , 9.0 ]
4418+
4419+ self .assert_dict_equal (fig1 .data [0 ].geojson , actual_geojson )
4420+ assert np .array_equal (fig1 .data [0 ].z , actual_agg )
4421+
4422+ fig2 = ff .create_hexbin_mapbox (
4423+ lat = lat , lon = lon , nx_hexagon = 1 , color = color , agg_func = np .mean ,
4424+ )
4425+
4426+ assert np .array_equal (fig2 .data [0 ].z , np .ones (5 ))
4427+
4428+ fig3 = ff .create_hexbin_mapbox (
4429+ lat = np .random .randn (1000 ), lon = np .random .randn (1000 ), nx_hexagon = 20 ,
4430+ )
4431+
4432+ assert fig3 .data [0 ].z .sum () == 1000
4433+
4434+ def test_build_dataframe (self ):
4435+ np .random .seed (0 )
4436+ N = 10000
4437+ nx_hexagon = 20
4438+ n_frames = 3
4439+
4440+ lat = np .random .randn (N )
4441+ lon = np .random .randn (N )
4442+ color = np .ones (N )
4443+ frame = np .random .randint (0 , n_frames , N )
4444+ df = pd .DataFrame (
4445+ np .c_ [lat , lon , color , frame ],
4446+ columns = ["Latitude" , "Longitude" , "Metric" , "Frame" ],
4447+ )
4448+
4449+ fig1 = ff .create_hexbin_mapbox (lat = lat , lon = lon , nx_hexagon = nx_hexagon )
4450+ fig2 = ff .create_hexbin_mapbox (
4451+ data_frame = df , lat = "Latitude" , lon = "Longitude" , nx_hexagon = nx_hexagon
4452+ )
4453+
4454+ assert isinstance (fig1 , go .Figure )
4455+ assert len (fig1 .data ) == 1
4456+ self .assert_dict_equal (
4457+ fig1 .to_plotly_json ()["data" ][0 ], fig2 .to_plotly_json ()["data" ][0 ]
4458+ )
4459+
4460+ fig3 = ff .create_hexbin_mapbox (
4461+ lat = lat ,
4462+ lon = lon ,
4463+ nx_hexagon = nx_hexagon ,
4464+ color = color ,
4465+ agg_func = np .sum ,
4466+ min_count = 0 ,
4467+ )
4468+ fig4 = ff .create_hexbin_mapbox (
4469+ lat = lat , lon = lon , nx_hexagon = nx_hexagon , color = color , agg_func = np .sum ,
4470+ )
4471+ fig5 = ff .create_hexbin_mapbox (
4472+ data_frame = df ,
4473+ lat = "Latitude" ,
4474+ lon = "Longitude" ,
4475+ nx_hexagon = nx_hexagon ,
4476+ color = "Metric" ,
4477+ agg_func = np .sum ,
4478+ )
4479+
4480+ self .assert_dict_equal (
4481+ fig1 .to_plotly_json ()["data" ][0 ], fig3 .to_plotly_json ()["data" ][0 ]
4482+ )
4483+ self .assert_dict_equal (
4484+ fig4 .to_plotly_json ()["data" ][0 ], fig5 .to_plotly_json ()["data" ][0 ]
4485+ )
4486+
4487+ fig6 = ff .create_hexbin_mapbox (
4488+ data_frame = df ,
4489+ lat = "Latitude" ,
4490+ lon = "Longitude" ,
4491+ nx_hexagon = nx_hexagon ,
4492+ color = "Metric" ,
4493+ agg_func = np .sum ,
4494+ animation_frame = "Frame" ,
4495+ )
4496+
4497+ fig7 = ff .create_hexbin_mapbox (
4498+ lat = lat ,
4499+ lon = lon ,
4500+ nx_hexagon = nx_hexagon ,
4501+ color = color ,
4502+ agg_func = np .sum ,
4503+ animation_frame = frame ,
4504+ )
4505+
4506+ assert len (fig6 .frames ) == n_frames
4507+ assert len (fig7 .frames ) == n_frames
4508+ assert fig6 .data [0 ].geojson == fig1 .data [0 ].geojson
0 commit comments