# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: loco
#     language: python
#     name: loco
# ---

# # Notebook for some basic data analysis for the Replica Dataset

replica_root = '/datasets01/replica/061819/18_scenes'

# +
import os
import json
scenes = os.listdir(replica_root)

scenes = ['apartment_0',
 'office_2',
 'room_0',
 'frl_apartment_5',
#  'office_0',
 'frl_apartment_4',
 'frl_apartment_1',
 'frl_apartment_0']

import matplotlib.pyplot as plt
# %matplotlib inline

plt.rcParams["figure.figsize"] = (20,3)

# exclude categories - wall, wall-plug, ceiling, floor
exc = ['blinds', 'wall', 'wall-plug', 'ceiling', 'floor', 'undefined', 'other-leaf', 'anonymize_picture', 'anonymize_text', 'wall-cabinet', 'base_cabinet']
# exc = []


#
oc = {}
cur = None

import pandas as pd

for x in scenes:
    print(f"\nScene {x}")
    habitat_semantic_json = os.path.join(replica_root, x, 'habitat', 'info_semantic.json')
    print(habitat_semantic_json)
    with open(habitat_semantic_json, "r") as f:
        habitat_semantic_data = json.load(f)
#     print(type(habitat_semantic_data))
    
    
    # save id_to_category
    cats = {}
    for obj_cls in habitat_semantic_data["classes"]:
        cats[obj_cls["id"]] = obj_cls["name"]
#         cats.append({"id": obj_cls["id"], "name": obj_cls["name"], "supercategory": "shape"})
    #     if obj_cls['name'] in ('floor', 'wall', 'ceiling', 'wall-plug'):
#         print(obj_cls['id'], obj_cls['name'])
    if cur is None:
        cur = cats
        df = pd.DataFrame(index=cur.values(), columns=scenes)
        df = df.fillna(0) # with 0s rather than NaNs

    else:
        if cur != cats:
            print("Unequal Categories")
    
    
    def count_objects(h):
        c = {}
        for x in h['objects']:
            cid = x['class_name']
            if cid not in exc:
                if cid  in c:
                    c[cid] += 1
                else:
                    c[cid] = 1
                
        c = {k:v for k,v in sorted(c.items(), key=lambda kv: kv[1], reverse=True) if v > 0}
        
            
        print(c)
        plt.figure(figsize=(20,3), dpi=100)
        plt.bar(c.keys(), c.values(), color='green')
        plt.xticks(rotation = 90)
        plt.show()
        return c
    
    c = count_objects(habitat_semantic_data)
    
    # update overall count
    for k, v in c.items():
        df.at[k, x] = v
#         if k in oc:
#             oc[k].append({x: v})
#         else:
#             oc[k] = [{x:v}]
# #     break

print(oc)

# for each train test split, find the intersection of objects (take the instace count )
# find objects in each scene 
# for each row pick label if it has a minimum count in each scene
minv = 1
ql = []

for index, row in df.iterrows():
    q = True
    for ix, val in row.items():
#         print(index, ix, val)
        if val < minv:
            q = False
    if q:
        ql.append(index)

print(len(ql), ql)
        
# -

cur



StarcoderdataJupyterScriptsDedupFiltered