Skip to content

Commit 857424e

Browse files
refactor: Remove unused pruneEmptyModelParameters function and related tests
fix: Enhance Registry function to validate and conditionally set considerations and performance metrics
1 parent 7994ce4 commit 857424e

File tree

5 files changed

+22
-78
lines changed

5 files changed

+22
-78
lines changed

cmd/enrich.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func init() {
150150
enrichCmd.Flags().StringVar(&enrichSpecVersion, "spec", "", "CycloneDX spec version for output (default: same as input)")
151151

152152
enrichCmd.Flags().StringVar(&enrichStrategy, "strategy", "interactive", "Enrichment strategy: interactive|file")
153-
enrichCmd.Flags().StringVar(&enrichConfigFile, "file", "", "Path to enrichment config file (YAML)")
153+
enrichCmd.Flags().StringVar(&enrichConfigFile, "file", "/config/enrichment.yaml", "Path to enrichment config file (YAML)")
154154
enrichCmd.Flags().BoolVar(&enrichRequiredOnly, "required-only", false, "Only prompt for required fields")
155155
enrichCmd.Flags().Float64Var(&enrichMinWeight, "min-weight", 0.0, "Only prompt for fields with weight >= this value")
156156
enrichCmd.Flags().BoolVar(&enrichRefetch, "refetch", false, "Refetch model metadata from Hugging Face before enrichment")

cmd/generate.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,14 @@ func init() {
203203
generateCmd.Flags().StringVarP(&generatePath, "input", "i", "", "Path to scan (default: current directory)")
204204
generateCmd.Flags().StringVarP(&generateOutput, "output", "o", "", "Output file path (directory is used; default: dist/aibom.json)")
205205
generateCmd.Flags().StringVarP(&generateOutputFormat, "format", "f", "auto", "Output BOM format: json|xml|auto (default: auto)")
206-
generateCmd.Flags().StringVar(&generateSpecVersion, "spec", "", "CycloneDX spec version for output (e.g., 1.3, 1.4, 1.5, 1.6)")
206+
generateCmd.Flags().StringVar(&generateSpecVersion, "spec", "", "CycloneDX spec version for output (e.g., 1.4, 1.5, 1.6)")
207207
generateCmd.Flags().StringVar(&hfMode, "hf-mode", "online", "Hugging Face metadata mode: online|dummy (default: online)")
208208
generateCmd.Flags().IntVar(&hfTimeoutSec, "hf-timeout", 10, "HTTP timeout in seconds for Hugging Face API")
209209
generateCmd.Flags().StringVar(&hfToken, "hf-token", "", "Hugging Face access token (string)")
210210
generateCmd.Flags().BoolVar(&enrich, "enrich", false, "Prompt for missing fields and compute completeness")
211211
generateCmd.Flags().StringVar(&generateLogLevel, "log-level", "standard", "Log level: quiet|standard|debug (default: standard)")
212212
}
213213

214-
func discoveryModelID(d scanner.Discovery) string {
215-
if strings.TrimSpace(d.ID) != "" {
216-
return strings.TrimSpace(d.ID)
217-
}
218-
return strings.TrimSpace(d.Name)
219-
}
220-
221214
func bomMetadataComponentName(bom *cdx.BOM) string {
222215
if bom == nil || bom.Metadata == nil || bom.Metadata.Component == nil {
223216
return ""

internal/builder/bom_builder.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ func (b BOMBuilder) Build(ctx BuildContext) (*cdx.BOM, error) {
4545
}
4646
}
4747

48-
// Optional cleanup: drop empty ModelParameters
49-
pruneEmptyModelParameters(comp)
50-
5148
logf(ctx.ModelID, "build ok")
5249
return bom, nil
5350
}
@@ -68,17 +65,3 @@ func buildMetadataComponent(ctx BuildContext) *cdx.Component {
6865
ModelCard: &cdx.MLModelCard{},
6966
}
7067
}
71-
72-
func pruneEmptyModelParameters(comp *cdx.Component) {
73-
if comp == nil || comp.ModelCard == nil || comp.ModelCard.ModelParameters == nil {
74-
return
75-
}
76-
mp := comp.ModelCard.ModelParameters
77-
emptyDatasets := mp.Datasets == nil || len(*mp.Datasets) == 0
78-
if strings.TrimSpace(mp.Task) == "" &&
79-
strings.TrimSpace(mp.ArchitectureFamily) == "" &&
80-
strings.TrimSpace(mp.ModelArchitecture) == "" &&
81-
emptyDatasets {
82-
comp.ModelCard.ModelParameters = nil
83-
}
84-
}

internal/builder/bom_builder_test.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -220,47 +220,3 @@ func TestBOMBuilder_Build_DoesNotIncludeEvidenceProperties_WhenDisabled(t *testi
220220
t.Fatalf("missing/incorrect aibomgen.evidence: ok=%v value=%q", ok, v)
221221
}
222222
}
223-
224-
// Test pruneEmptyModelParameters function
225-
func TestPruneEmptyModelParameters_RemovesWhenAllFieldsEmpty(t *testing.T) {
226-
comp := &cdx.Component{
227-
ModelCard: &cdx.MLModelCard{
228-
ModelParameters: &cdx.MLModelParameters{
229-
// all empty
230-
Task: "",
231-
ArchitectureFamily: "",
232-
ModelArchitecture: "",
233-
Datasets: nil, // also try empty slice if you want
234-
},
235-
},
236-
}
237-
238-
pruneEmptyModelParameters(comp)
239-
240-
if comp.ModelCard == nil {
241-
t.Fatalf("ModelCard unexpectedly nil")
242-
}
243-
if comp.ModelCard.ModelParameters != nil {
244-
t.Fatalf("ModelParameters = %#v, want nil (pruned)", comp.ModelCard.ModelParameters)
245-
}
246-
}
247-
248-
// Test pruneEmptyModelParameters keeps the parameters when any field is present
249-
func TestPruneEmptyModelParameters_KeepsWhenAnyFieldPresent(t *testing.T) {
250-
comp := &cdx.Component{
251-
ModelCard: &cdx.MLModelCard{
252-
ModelParameters: &cdx.MLModelParameters{
253-
Task: "text-classification", // makes it non-empty
254-
},
255-
},
256-
}
257-
258-
pruneEmptyModelParameters(comp)
259-
260-
if comp.ModelCard == nil || comp.ModelCard.ModelParameters == nil {
261-
t.Fatalf("ModelParameters was pruned, want it kept")
262-
}
263-
if comp.ModelCard.ModelParameters.Task != "text-classification" {
264-
t.Fatalf("Task = %q, want %q", comp.ModelCard.ModelParameters.Task, "text-classification")
265-
}
266-
}

internal/metadata/fieldspecs.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -767,10 +767,11 @@ func Registry() []FieldSpec {
767767
if tgt.ModelCard == nil || src.Readme == nil {
768768
return
769769
}
770-
cons := ensureConsiderations(tgt.ModelCard)
771-
if cons.UseCases != nil && len(*cons.UseCases) > 0 {
770+
// Check existing first
771+
if tgt.ModelCard.Considerations != nil && tgt.ModelCard.Considerations.UseCases != nil && len(*tgt.ModelCard.Considerations.UseCases) > 0 {
772772
return
773773
}
774+
// Collect data
774775
useCases := []string{}
775776
if s := strings.TrimSpace(src.Readme.DirectUse); s != "" {
776777
useCases = append(useCases, s)
@@ -782,6 +783,8 @@ func Registry() []FieldSpec {
782783
if len(useCases) == 0 {
783784
return
784785
}
786+
// Only create structure when we have data
787+
cons := ensureConsiderations(tgt.ModelCard)
785788
cons.UseCases = &useCases
786789
logf(src.ModelID, "apply %s set=%s", ModelCardConsiderationsUseCases, summarizeValue(useCases))
787790
},
@@ -827,15 +830,18 @@ func Registry() []FieldSpec {
827830
if tgt.ModelCard == nil || src.Readme == nil {
828831
return
829832
}
830-
cons := ensureConsiderations(tgt.ModelCard)
831-
if cons.TechnicalLimitations != nil && len(*cons.TechnicalLimitations) > 0 {
833+
// Check existing first
834+
if tgt.ModelCard.Considerations != nil && tgt.ModelCard.Considerations.TechnicalLimitations != nil && len(*tgt.ModelCard.Considerations.TechnicalLimitations) > 0 {
832835
return
833836
}
837+
// Validate data exists
834838
s := strings.TrimSpace(src.Readme.BiasRisksLimitations)
835839
if s == "" {
836840
return
837841
}
842+
// Only create structure when we have data
838843
vals := []string{s}
844+
cons := ensureConsiderations(tgt.ModelCard)
839845
cons.TechnicalLimitations = &vals
840846
logf(src.ModelID, "apply %s set=%s", ModelCardConsiderationsTechnicalLimitations, summarizeValue(s))
841847
},
@@ -881,10 +887,11 @@ func Registry() []FieldSpec {
881887
if tgt.ModelCard == nil || src.Readme == nil {
882888
return
883889
}
884-
cons := ensureConsiderations(tgt.ModelCard)
885-
if cons.EthicalConsiderations != nil && len(*cons.EthicalConsiderations) > 0 {
890+
// Check existing first
891+
if tgt.ModelCard.Considerations != nil && tgt.ModelCard.Considerations.EthicalConsiderations != nil && len(*tgt.ModelCard.Considerations.EthicalConsiderations) > 0 {
886892
return
887893
}
894+
// Validate data exists
888895
name := strings.TrimSpace(src.Readme.BiasRisksLimitations)
889896
mit := strings.TrimSpace(src.Readme.BiasRecommendations)
890897
if name == "" && mit == "" {
@@ -893,7 +900,9 @@ func Registry() []FieldSpec {
893900
if name == "" {
894901
name = "bias_risks_limitations"
895902
}
903+
// Only create structure when we have data
896904
ethics := []cdx.MLModelCardEthicalConsideration{{Name: name, MitigationStrategy: mit}}
905+
cons := ensureConsiderations(tgt.ModelCard)
897906
cons.EthicalConsiderations = &ethics
898907
logf(src.ModelID, "apply %s set=true", ModelCardConsiderationsEthicalConsiderations)
899908
},
@@ -967,10 +976,11 @@ func Registry() []FieldSpec {
967976
if tgt.ModelCard == nil || src.Readme == nil {
968977
return
969978
}
970-
qa := ensureQuantitativeAnalysis(tgt.ModelCard)
971-
if qa.PerformanceMetrics != nil && len(*qa.PerformanceMetrics) > 0 {
979+
// Check existing first
980+
if tgt.ModelCard.QuantitativeAnalysis != nil && tgt.ModelCard.QuantitativeAnalysis.PerformanceMetrics != nil && len(*tgt.ModelCard.QuantitativeAnalysis.PerformanceMetrics) > 0 {
972981
return
973982
}
983+
// Collect metrics data
974984
metrics := make([]cdx.MLPerformanceMetric, 0)
975985

976986
// 1) From model-index in README YAML (detailed metrics with values)
@@ -1030,6 +1040,8 @@ func Registry() []FieldSpec {
10301040
if len(metrics) == 0 {
10311041
return
10321042
}
1043+
// Only create structure when we have data
1044+
qa := ensureQuantitativeAnalysis(tgt.ModelCard)
10331045
qa.PerformanceMetrics = &metrics
10341046
logf(src.ModelID, "apply %s set=%s", ModelCardQuantitativeAnalysisPerformanceMetrics, summarizeValue(metrics))
10351047
},

0 commit comments

Comments
 (0)