
    1j"                        d Z ddlmZ ddlmZ ddlZddlmZ ddlZ	 ed       G d d	             Z
 e
d
 eh d            Z e
d eh d            Z e
d eh d            Z e
d eh d            Z e
d eh d            Z e
d eh d            Z e
d edh            ZeeeeeeeeeeeeeeeefZeeeeeeeefZdZ ee      ez   Z e e ee      e            ZddZd dZddZd dZ	 	 	 	 	 	 	 	 d!dZ	 	 	 	 	 	 	 	 d"dZ y)#a'  Interpretable input routing for the grouped MLP experiment.

Each first-layer unit receives one coherent family of features.  Later layers
remain dense, so the model can learn interactions *between* those family-level
representations without mixing unrelated raw signals in the first transform.
    )annotations)	dataclassN)SequenceT)frozenc                  &    e Zd ZU dZded<   ded<   y)FeatureFamilyzDA named set of raw features routed to one or more first-layer units.strnamezfrozenset[str]featuresN)__name__
__module____qualname____doc____annotations__     H/Users/jameslopez/projects/TradingBot25/strategies/mlp_feature_groups.pyr   r      s    N
Ir   r   local_technical>   osc_norm
stoch_normm2_div_normm3_div_normm2_tiny_normbb_pct_b_normm2_nooff_normvwap_div_normmacd_pred_normrsi_subtf_normstoch_div_normstoch_peak_normm3_momentum_normon_chain>	   hr_norm	nupl_norm	sopr_normnewaddr_normactive1y_normsendaddr_normtotalvol_normmvrv_zscore_contmvrv_zscore_valuecandlesticks>   shooting_star_scorebullish_hammer_scorebearish_engulfing_scorebullish_engulfing_scoremacro>   gc_positiondxy_roc_normbtc_spx_corr_30vix_pctrank_invbtc_dom_roc_signfed_net_liq_signgold_roc_pctrankspy_above_200emayield_curve_signus2y_roc_inv_signus10y_roc_inv_signrsid_structure>
   rsid_rt_bear_normrsid_rt_bull_normrsid_hid_bear_normrsid_hid_bull_normrsid_reg_bear_normrsid_reg_bull_normrsid_slow_bear_normrsid_slow_bull_normrsid_delayed_dip_normrsid_delayed_peak_normmarket_flow>   cvd_norm	rvol_norm
basis_normoi_roc_normusdt_d_normcross_assetbtc_gold_norm   c                l   |t        t              k7  rt        dt        t               d| d       t               j                  d t        D         }t        |       |z
  }|rt        dt        |             t        j                  t        D cg c]  }| D cg c]  }||j                  v  c}  c}}t              }|j                  d      j                         s?t        | |j                  d            D cg c]
  \  }}|r	| }}}t        d	|       |S c c}w c c}}w c c}}w )
u   Return the allowed ``(hidden_size, n_features)`` first-layer edges.

    The experiment deliberately targets the established 55→16→8→1 shape.
    Failing on a different width prevents a silently arbitrary allocation.
    z!Grouped input structure requires  first-layer units; received .c              3  4   K   | ]  }|j                     y wNr   .0familys     r   	<genexpr>z+grouped_first_layer_mask.<locals>.<genexpr>^   s     V>UF>U   z.Grouped input structure has unknown features: dtyper   axisz0Grouped input structure left features unrouted: )lenGROUPED_UNIT_ALLOCATION
ValueError	frozensetunionsetsortednparrayr   boolanyallzip)	feature_colshidden_sizeknownunknownrY   featuremaskpresentmissings	            r   grouped_first_layer_maskru   S   s?    c122/4K0L/M N#}A'
 	
 IKV>UVWE,%'GI&QX/IZ[\\88.	0.V 5A	AL'V__
$L	A.	0D
 888!36|TXXSTXEU3Vf3V/w^e73VfKG9UVVK 
B 	0
 gs$   	D*
D%1D*

D0D0%D*
c           
        t        | |       t        t        j                  t                    }dt        D cg c]  }|j
                   c}|D ci c]!  j
                  t        fd| D              # c}dS c c}w c c}w )z>Serializable provenance saved beside an experimental artifact.grouped_first_layer_v1c              3  :   K   | ]  }|j                   v   y wrU   rV   )rX   rq   rY   s     r   rZ   z-grouped_structure_metadata.<locals>.<genexpr>u   s     T|GW7|s   )r
   unit_familiesfamily_feature_counts)ru   tupledictfromkeysra   r
   sum)rm   rn   familiesrY   s      `r   grouped_structure_metadatar   m   s    \;7T]]#:;<H(4KL4K&&++4KL #"
" KKT|TTT""
 L"
s   A>&Bc                   |t         k7  rt        dt          d| d       t               j                  d t        D         }t        |       |z
  }|rt        dt        |             t        j                  t        D cg c]  }| D cg c]  }||j                  v  c}  c}}t              }t        j                  t        t              t        |       ft              }t        j                  ||f      S c c}w c c}}w )a^  Return the v1 specialist-plus-dense first-layer connectivity mask.

    Rows 0--7 are hard-routed specialists, in the documented family order.
    Rows 8--15 remain fully trainable across every input feature.  Keeping the
    architecture check strict makes the allocation auditable rather than an
    accidental choice for another hidden width.
    z(Hybrid grouped input structure requires rR   rS   c              3  4   K   | ]  }|j                     y wrU   rV   rW   s     r   rZ   z2hybrid_grouped_first_layer_mask.<locals>.<genexpr>   s     c>bF>br[   z5Hybrid grouped input structure has unknown features: r\   )HYBRID_GROUPED_HIDDEN_SIZErb   rc   rd   $HYBRID_GROUPED_SPECIALIST_ALLOCATIONre   rf   rg   rh   r   ri   onesr`    HYBRID_GROUPED_DENSE_ROW_INDICESvstack)rm   rn   ro   rp   rY   rq   specialist_rows
dense_rowss           r   hybrid_grouped_first_layer_maskr   {   s    0067Q6R S++6-q:
 	
 IKc>bcdE,%'GPQWX_Q`Pabcchh;	=;V 5A	AL'V__
$L	A;	=O
 #>?\ARS[_`J99oz233 
B 	=s   	D

C<D
<D
c                   t        | |      }dj                  |       }t        t              }ddt	        t              D ci c]  \  }}t        |      |j                   c}}t        t              t        j                  |j                               j                         t        |j                               t        |j                  |j                         z
        |j                  d      D cg c]  }t        |       c}|D cg c]%  }t!        j"                  |      j%                         ' c}|t        t              dS c c}}w c c}w c c}w )z@Return complete, reproducible provenance for the hybrid v1 mask.hybrid_grouped_first_layer_v1   r^   )r
   versionspecialist_row_familiesdense_row_indicesfeature_cols_sha256active_edgesblocked_edgesrow_active_edgesrow_feature_indicesspecialist_row_countdense_row_count)r   joinr`   r   	enumerater	   r
   listr   hashlibsha256encode	hexdigestintr~   sizerg   flatnonzerotolist)rm   rn   rr   feature_textspecialist_countrowrY   counts           r   !hybrid_grouped_structure_metadatar      s   *<ED;;|,L?@/  ))MN$
NV Hfkk!N$
 ""BC&~~l.A.A.CDNNPDHHJTYY3459XX1X5EF5EESZ5EFHLMs 3 : : <M 0?@ $
 GMs    E	
3E*Ec                2   t        | |      }t        j                  j                  |      }t        j                  |t
              }t        |j                  d            D ]2  \  }}|j                  t        |       t        |      d      }d|||f<   4 |S )aw  Return a seeded random control mask with the grouped row capacities.

    This deliberately samples features independently per hidden unit.  It is a
    capacity-matched control, not a second semantic feature routing scheme.
    Calling the grouped builder first preserves its architecture and feature
    validation while providing the exact per-row active-edge counts.
    r\   r   r^   F)r   replaceT)ru   rg   randomdefault_rng
zeros_likeri   r   r~   choicer`   r   )	rm   rn   	mask_seedgrouped_maskrngrr   	row_indexr   selecteds	            r   random_sparse_first_layer_maskr      s     ,L+FL
))

	
*C==T2D#,\-=-=1-=-E#F	<::c,/c,6GQV:W$(Y ! $G Kr   c                   t        | ||      }dj                  |       }d|dt        j                  |j	                               j                         t        |j                               t        |j                  |j                         z
        |j                  d      D cg c]  }t        |       c}|D cg c]%  }t        j                  |      j                         ' c}dS c c}w c c}w )zBReturn complete, serializable provenance for a random sparse mask.r   random_sparse_first_layer_v1zDnumpy.default_rng(seed).choice(n_features, row_count, replace=False)r   r^   )r
   r   	generatorr   r   r   r   r   )r   r   r   r   r   r   r   r~   r   rg   r   r   )rm   rn   r   rr   r   r   r   s          r    random_sparse_structure_metadatar      s     *,YOD;;|,L.[&~~l.A.A.CDNNPDHHJTYY3459XX1X5EF5EESZ5EFHLMs 3 : : <M	 	 GMs   #C*;*C/)rm   Sequence[str]rn   r   return
np.ndarray)rm   r   rn   r   r   dict[str, object])rm   r   rn   r   r   r   r   r   )rm   r   rn   r   r   r   r   r   )!r   
__future__r   dataclassesr   r   typingr   numpyrg   r   rc   LOCAL_TECHNICALON_CHAINCANDLESTICKSMACRORSIDMARKET_FLOWCROSS_ASSETra   r   HYBRID_GROUPED_DENSE_UNIT_COUNTr`   r   r{   ranger   ru   r   r   r   r   r   r   r   r   <module>r      s   # !    $     19 > 4  Y 0 &  ^Y 8 .  	gy *   	
 %y 2 (  M9 6 ,  M9o5F+GH _oh	5%$  _	( $ #$  !EFIhh #(	#2
35OP$  
444..1>A&.1>Ar   